Created
December 1, 2016 20:33
-
-
Save willmanduffy/ea6f583cff3fbc308e9105e62ee4c24c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| root = this | |
| module = window.Sorta ||= {} | |
| $ = jQuery | |
| root.Sorta = do ( module, $ ) -> | |
| self = module.lists = module.lists || {} | |
| self.index = -> | |
| Sorta.timeago.callTimeAgo() | |
| self.show = -> | |
| bindDestroyItem() | |
| bindHideSearch() | |
| bindItemSearch() | |
| bindShowSearch() | |
| Sorta.favorites.bindFavorites() | |
| Sorta.sortable.bindSortable() | |
| Sorta.timeago.callTimeAgo() | |
| # private | |
| AddResultBindings = -> | |
| $( '.js-add-item' ).bind 'click', ( e ) -> | |
| e.preventDefault() | |
| $parentLink = $( @ ).children( 'a' ) | |
| $list = $( '.js-list' ) | |
| creator = $parentLink.data('creator') | |
| date = $parentLink.data('date') | |
| externalId = $parentLink.data('external-id') | |
| imdbId = $parentLink.data('imdb-db') | |
| itemName = $parentLink.text() | |
| place = $list.children().length | |
| posterPath = $parentLink.data('poster-path') | |
| field = generateField(creator, itemName) | |
| $.ajax | |
| url: "#{listId()}/items" | |
| type: 'POST' | |
| data: { item: { name: itemName, place: place }, itemable: { creator: creator, date: date, imdb_id: imdbId, kind: kind(), poster_path: posterPath, external_id: externalId } } | |
| success: ( data ) -> | |
| $list.append " | |
| <li class='list-item' data-item-id='#{data.id}'> | |
| <span class='place'></span>#{field} | |
| <span class='remove'> | |
| <a class='js-remove'>×</a> | |
| </span> | |
| </li> | |
| " | |
| bindClearInputField() | |
| bindDestroyItem() | |
| Sorta.sortable.bindSortable() | |
| Sorta.timeago.updateLastUpdated() | |
| bindDestroyItem = -> | |
| $( '.js-remove' ).bind 'click', ( e ) -> | |
| e.preventDefault() | |
| $parentLi = $( @ ).parents('li') | |
| itemId = $parentLi.data('item-id') | |
| $.ajax | |
| url: "#{listId()}/items/#{itemId}" | |
| type: 'DELETE' | |
| success: -> | |
| $parentLi.remove() | |
| bindHideSearch = -> | |
| $( document ).on 'click', ( e ) -> | |
| targetHidden = $('.js-search-results').hasClass 'hidden' | |
| if !$(e.target).closest( '.js-input-container' ).length && !targetHidden | |
| hideResults() | |
| bindItemSearch = -> | |
| $( '.js-new-item' ).on 'input', ( e ) -> | |
| $el = $( @ ) | |
| initialVal = $el.val() | |
| setTimeout -> | |
| query = $el.val() | |
| # Compare value from before timeout to prevent remote calls from happening too quickly | |
| return if initialVal != query | |
| $target = emptyResults() | |
| return if query == '' | |
| $.getJSON "/scans/#{query}?kind=#{kind()}", ( data ) -> | |
| $.each data, ( key, val ) -> | |
| if val.creator | |
| creatorSpan = "<span class='creator'> by #{val.creator}</span>" | |
| else | |
| creatorSpan = "" | |
| if val.date | |
| dateSpan = "<span>(#{val.date})</span>" | |
| else | |
| dateSpan = "" | |
| $target.append " | |
| <li class='js-add-item search-result'> | |
| <a class='add-item' data-creator='#{val.creator}' data-date='#{val.date}' data-external-id='#{val.id}' data-poster-path='#{val.poster_path}' data-imdb-id='#{val.imdb_id}'>#{val.title}</a> | |
| #{creatorSpan} | |
| #{dateSpan} | |
| </li> | |
| " | |
| AddResultBindings() | |
| showResults() | |
| , 500 | |
| bindShowSearch = -> | |
| $( '.js-new-item' ).on 'click', -> | |
| $el = $( '.js-search-results' ) | |
| if $el.children() && $el.hasClass 'hidden' | |
| showResults() | |
| bindClearInputField = -> | |
| emptyResults() | |
| $( '.js-new-item' ).val('').focus() | |
| emptyResults = -> | |
| $( '.js-search-results' ).empty() | |
| generateField = ( creator, name ) -> | |
| return name if creator == 'undefined' | |
| [creator, name].join(' - ') | |
| hideResults = -> | |
| $( '.js-search-results' ).addClass 'hidden' | |
| $( '.js-new-item' ).removeClass 'searched' | |
| kind = -> | |
| $( '.js-list-identifier' ).data('kind') | |
| listId = -> | |
| $( '.js-list-identifier' ).data('id') | |
| showResults = -> | |
| $( '.js-search-results' ).removeClass 'hidden' | |
| $( '.js-new-item' ).addClass 'searched' | |
| module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment