Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                .search-bar
    input
        width: 300px
        padding: 3px
        border: 1px solid #000
        margin: 0 0 10px 0
    button
        padding: 2px 20px
        border: 1px solid #000
        border-left: 0
        cursor: pointer

.song-list
    p
        margin: 3px 0
    .song
        padding: 10px 0
        border-bottom: 1px dashed #CCC
        cursor: pointer
        &:hover
            background: #CCC
        .title
            font-weight: bold
        .artist
            color: #999
            font-size: 12px
        .listen
            color: #F00
            font-size: 12px
            &:before
                content: "-"
                margin: 0 10px

#player
    margin: 10px 0

              
            
!

JS

              
                ###* @jsx React.DOM ###

NhacCuaTui = ->
    _apiSettings =
        apiURL: "http://m.nhaccuatui.com/tim-kiem/bai-hat?q="
        endpointURL: "https://query.yahooapis.com/v1/public/yql?q="

    _yqlExecuteQuery = (yqlStatement) ->
        yqlResult = {}
        $.ajax
            url: "#{_apiSettings.endpointURL}#{encodeURIComponent yqlStatement}&format=json"
            cache: no
            async: no
            success: (response) ->
                yqlResult = response
            error: (xmlHttpReq, ajaxOpts, error) ->
                console.log error

        yqlResult

    @search = (keyword) ->
        # Mã hóa từ khóa sang dạng URI #
        keyword = encodeURIComponent keyword.trim()
        # Object chứa status và mảng object kết quả #
        result =
            status: false
            items: []

        if keyword isnt ""
            yqlStatement = "SELECT * FROM html WHERE url='#{_apiSettings.apiURL + keyword}' AND xpath='//div[contains(@class, \"bgmusic\")]'"
            yqlResult = _yqlExecuteQuery yqlStatement

            if yqlResult.query.count > 0
                result.status = true
                $.each yqlResult.query.results.div, (idx, item) ->
                    result.items.push
                        title: item.h3.a.title
                        link: item.h3.a.href
                        artist: item.p.content
                        listen: item.p.span.content

            result

    @get = (link) ->
        result =
            status: false
            link: ""

        if link.trim() isnt ""
            yqlStatement = "SELECT * FROM html WHERE url='#{link}' AND xpath='//div[@class=\"download\"]//a'"
            yqlResult = _yqlExecuteQuery yqlStatement
            if yqlResult.query.count > 0
                result =
                    status: true
                    link: yqlResult.query.results.a.href

        result

    return

  
SearchBar = React.createClass
    _onClickSearchButtonHandler: ->
        keyword = @refs.keyword.getDOMNode().value.trim()

        if keyword isnt ""
            @props.searchSong keyword
        else
            alert "Please enter keyword."

    render: ->
        `(
            <div className="search-bar">
                <input type="text" placeholder="Song name, artist, user, ..." ref="keyword" />
                <button onClick={this._onClickSearchButtonHandler}>Search</button>
            </div>
        )`

SongList = React.createClass
    render: ->
        songList = @props.songList || []

        SongElement = `(<div className="song">
                            <p className="title">(no data)</p>
                        </div>)`

        if songList.length > 0
            SongElement = songList.map ((song, idx) ->
                `(<Song songInfo={song} getSong={this.props.getSong} />)`
            ).bind @
        `(
            <div className="song-list">
                {SongElement}
            </div>
        )`

Song = React.createClass
    _onClickSongHandler: (link) ->
        @props.getSong link

    render: ->
        songInfo = @props.songInfo

        `(
            <div className="song" onClick={this._onClickSongHandler.bind(null, songInfo.link)}>
                <p className="title">{songInfo.title}</p>
                <p>
                    <span className="artist">{songInfo.artist}</span>
                    <span className="listen">{songInfo.listen}</span>
                </p>
            </div>
        )`

Player = React.createClass
    componentDidUpdate: (prevProps, prevState) ->
        videoElement = @refs.videoElement.getDOMNode()
        videoElement.load()
        videoElement.play()

    render: ->
        songLink = @props.link || ""

        `(
            <div id="player">
                <audio controls autoplay ref="videoElement">
                    <source src={songLink} type="audio/mpeg" />
                    Your browser does not support the audio element.
                </audio>
            </div>
        )`

App = React.createClass
    getInitialState: ->
        songList: []
        songLink: ""

    componentWillMount: ->
        @nhacCuaTui = new NhacCuaTui()

    _searchSong: (keyword) ->
        songList = @nhacCuaTui.search keyword

        if songList.status
            @setState
                songList: songList.items

    _getSong: (link) ->
        songLink = @nhacCuaTui.get link

        if songLink.status
            @setState
                songLink: songLink.link

    render: ->
        `(
            <div id="wrapper">
                <SearchBar searchSong={this._searchSong} />
                <SongList songList={this.state.songList} getSong={this._getSong} />
                <Player link={this.state.songLink} />
            </div>
        )`

React.renderComponent `<App />`, document.body
              
            
!
999px

Console