Skip to content

Commit

Permalink
Merge pull request boblail#7 from boblail/0.5
Browse files Browse the repository at this point in the history
0.5
  • Loading branch information
boblail committed Apr 5, 2016
2 parents ad94a9c + 2cff830 commit 3ff8684
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 182 deletions.
6 changes: 5 additions & 1 deletion vendor/assets/javascripts/neat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
//= require ./neat/lib/inflect
//= require ./neat/lib/observer
//= require ./neat/lib/paginated_list
//= require ./neat/lib/delayed_action
//= require ./neat/lib/jquery_extensions
//= require ./neat/lib/scroll_handler
//= require ./neat/renderer/basic
//= require ./neat/renderer/paginated
//= require ./neat/renderer/infinite_reveal
//= require ./neat/collection_editor
//= require ./neat/model_editor

window.Neat = window.Neat || {}
window.Neat.Renderer = window.Neat.Renderer || {}
window.Neat.debug = true;
window.Neat.logger = {
log: function() {
Expand Down
150 changes: 22 additions & 128 deletions vendor/assets/javascripts/neat/collection_editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ KEYS = {
# viewPath - by default this is set to resource
#
class window.Neat.CollectionEditor extends Backbone.View
sortOrder: 'asc'
sortAliases: {}
templateOptions: {}
pageSize: 30
useKeyboardToChangeRows: true
cancelOnEsc: true
saveOnEnter: true
alternateRows: true
renderer: "Basic"
rendererOptions: {}

initialize: ->
@keyDownHandlers = {}
Expand All @@ -37,41 +35,15 @@ class window.Neat.CollectionEditor extends Backbone.View
@debug "looking for template at #{"#{@viewPath}/index"}"
@template = @template ? Neat.template["#{@viewPath}/index"]

@paginator = new window.Lail.PaginatedList [],
page_size: if window.Neat.forPrint then Infinity else @pageSize
always_show: false
@paginator.onPageChange _.bind(@renderPage, @)

@collection.bind 'reset', @__reset, @
@collection.bind 'add', @__add, @
@collection.bind 'remove', @__remove, @

# We need to rerender the page if a model's attributes
# have changed just in case this would affect how the
# models are sorted.
#
# !todo: perhaps we can be smarter here and only listen
# for changes to the attribute the view is sorted on.
#
# We don't want to redraw the page every time a model
# has changed. If an old transaction is deleted, a very
# large number of more recent transactions' running
# balances could be updated very rapidly. Redrawing the
# page will slow things down dramatically.
#
# Instead, redraw the page 500ms after a model has changed.
#
# This allows us to wait for activity to die down
# and to redraw the page when it's more likely the system
# has settled into new state.
#
@delayedRerender = new Lail.DelayedAction(_.bind(@rerenderPage, @), delay: 500)
@collection.bind 'change', =>
@delayedRerender.trigger() if @sortedBy

# If the view's headers are 'a' tags, this view will try
# to sort the collection using the header tags.
$(@el).delegate '.header a', 'click', _.bind(@sort, @)
# For backwards-compatibility
@rendererOptions.pageSize = @pageSize if @pageSize?

rendererKlass = @renderer
rendererKlass = Neat.Renderer[rendererKlass] if _.isString(rendererKlass)
@_renderer = new rendererKlass(@, @collection, @rendererOptions)

@_renderer.afterRender _.bind(@afterRender, @)

$(@el).delegate '.editor', 'keydown', _.bind(@onKeyDown, @)

if @cancelOnEsc
Expand All @@ -84,37 +56,19 @@ class window.Neat.CollectionEditor extends Backbone.View
@keyDownHandlers['UP'] = -> @edit @prevView()
@keyDownHandlers['DOWN'] = -> @edit @nextView()

@views = []
@viewInEdit = null
@templateOptions = {}

repaginate: ->
@rerenderPage(1)

rerenderPage: (page)->
page = @paginator.getCurrentPage() unless _.isNumber(page)
if @sortedBy
sortField = @sortField(@sortedBy)
items = @collection.sortBy (model)->
val = model.get(sortField) || ''
if _.isString(val) then val.toLowerCase() else val
items.reverse() if @sortOrder == 'desc'
else
items = @collection.toArray()
@paginator.init items, page


render: ->
$el = $(@el)
$el.html @template(@context())
$el.cssHover '.neat-row.neat-interactive'
@$ul = $(@el).find("##{@resource}")

@afterRender()

@updateSortStyle() if @sortedBy

@paginator.renderPaginationIn($el.find('.pagination'))
@repaginate()
@_renderer.renderTo @$ul
@

context: ->
Expand All @@ -123,26 +77,6 @@ class window.Neat.CollectionEditor extends Backbone.View
afterRender: ->
@

renderPage: ->
alt = false
@$ul = $(@el).find("##{@resource}").empty() # e.g. $('#calendars')
@views = []

$(@el).find('.extended-pagination').html(@paginator.renderExtendedPagination())

for model in @paginator.getCurrentSet()
$el = @appendViewFor(model)
$el.toggleClass 'alt', !(alt = !alt) if @alternateRows
@

appendViewFor: (model) ->
view = @buildViewFor(model)
@views.push(view)

$el = $(view.render().el)
@$ul.append $el
$el

buildViewFor: (model) ->
self = @
view = @constructModelView
Expand All @@ -155,7 +89,9 @@ class window.Neat.CollectionEditor extends Backbone.View
view

constructModelView: (options) ->
new @modelView options
viewClass = @modelView
viewClass = viewClass(options.model) if _.isFunction(viewClass) and !(viewClass.prototype instanceof Neat.ModelEditor)
new viewClass(options)


beforeEdit: (view)->
Expand All @@ -170,33 +106,6 @@ class window.Neat.CollectionEditor extends Backbone.View



sort: (e)->
e.preventDefault()
e.stopImmediatePropagation()
sortBy = $(e.target).closest('a').attr('class').substring(@singular.length + 1)
@log "sort by #{sortBy} [#{@sortField(sortBy)}]"
if @sortedBy == sortBy
@sortOrder = if @sortOrder == 'asc' then 'desc' else 'asc'
else
@removeSortStyle @sortedBy
@sortedBy = sortBy
@repaginate()
@updateSortStyle()
false

removeSortStyle: (field)->

updateSortStyle: ()->
@removeSortStyle @sortedBy

getHeader: (field)->
$(@el).find(".header > .#{@singular}-#{field}")

sortField: (field)->
@sortAliases[field] ? field



onKeyDown: (e)->
keyName = @identifyKey(e.keyCode)
handler = @keyDownHandlers[keyName]
Expand All @@ -211,20 +120,21 @@ class window.Neat.CollectionEditor extends Backbone.View
# i.e. return true if target is in a dropdown control like Chosen
false


views: ->
@_renderer.views

nextView: ->
@views[@indexOfViewInEdit() + 1]
@views()[@indexOfViewInEdit() + 1]

prevView: ->
@views[@indexOfViewInEdit() - 1]
@views()[@indexOfViewInEdit() - 1]

indexOfViewInEdit: ->
_.indexOf @views, @viewInEdit
_.indexOf @views(), @viewInEdit

edit: (view)->
if view instanceof Backbone.Model
view = @__findViewForModel(view)
view = @_renderer.findViewForModel(view)
if view
@viewInEdit?.save()
view.edit()
Expand All @@ -234,22 +144,6 @@ class window.Neat.CollectionEditor extends Backbone.View
@viewInEdit?.cancelEdit()



__reset: ->
@render()

__add: ->
@rerenderPage()

__remove: ->
@rerenderPage()



__findViewForModel: (model)->
_.find @views, (view)-> view.model.cid is model.cid


debug: (o...)->
@log(o...) if Neat.debug

Expand Down
49 changes: 0 additions & 49 deletions vendor/assets/javascripts/neat/lib/delayed_action.js

This file was deleted.

12 changes: 10 additions & 2 deletions vendor/assets/javascripts/neat/lib/jquery_extensions.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$.fn.extend
cssHover: (selector)->
cssHover: (selector) ->
if arguments.length == 0
@hover(
-> $(@).addClass('hovered'),
Expand All @@ -11,5 +11,13 @@ $.fn.extend
else
$(@).removeClass('hovered')

isIn: (selector)->
isIn: (selector) ->
@is(selector) or (@parents(selector).length > 0)

insertBeforeChildOrAppendTo: (parent, selector) ->
$parent = $ parent
$insertionPoint = $parent.children(selector)
if $insertionPoint.length > 0
$insertionPoint.before @
else
$parent.append @
33 changes: 33 additions & 0 deletions vendor/assets/javascripts/neat/lib/scroll_handler.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class @Neat.ScrollHandler
_.extend @prototype, Backbone.Events

constructor: (options={})->
@$viewPort = $(options.viewPort ? window)
@$document = $(document)
@$viewPort.scroll _.bind(@_windowWasScrolled, @)

_viewportHeight: ->
@$viewPort.height()

_documentHeight: ->
if $.isWindow @$viewPort[0]
@$document.height()
else
@$viewPort[0].scrollHeight

_windowWasScrolled: ->
viewportHeight = @_viewportHeight()
documentHeight = @_documentHeight()

scrollTop = @$viewPort.scrollTop()
scrollMax = documentHeight - viewportHeight
progress = scrollTop / scrollMax
distanceFromBottom = scrollTop - scrollMax

return if scrollMax <= 0

@trigger "scroll",
scrollTop: scrollTop
scrollMax: scrollMax
progress: progress
distanceFromBottom: distanceFromBottom
4 changes: 2 additions & 2 deletions vendor/assets/javascripts/neat/model_editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ class window.Neat.ModelEditor extends Backbone.View

@model.destroy
wait: true
success: => @onDestroySuccess
error: _.bind(@onSaveError, @)
success: _.bind(@onDestroySuccess, @)
error: _.bind(@onDestroyError, @)
@endEdit()

confirmDestroy: (resource, callback)->
Expand Down
Loading

0 comments on commit 3ff8684

Please sign in to comment.