Merge branch 'master' of github.com:radekstepan/burnchart.io

This commit is contained in:
Radek Stepan 2014-10-20 19:38:11 -07:00
commit 5d6bed2759
2 changed files with 51 additions and 2 deletions

View File

@ -4,12 +4,16 @@ config = require '../models/config.coffee'
mediator = require '../modules/mediator.coffee' mediator = require '../modules/mediator.coffee'
Model = require '../utils/model.coffee' Model = require '../utils/model.coffee'
date = require '../utils/date.coffee' date = require '../utils/date.coffee'
search = require '../utils/search.coffee'
user = require './user.coffee' user = require './user.coffee'
module.exports = new Model module.exports = new Model
'name': 'models/projects' 'name': 'models/projects'
'data':
'sortKey': 'priority'
find: (project) -> find: (project) ->
_.find @data.list, project _.find @data.list, project
@ -32,6 +36,19 @@ module.exports = new Model
clear: -> clear: ->
@set 'list', [] @set 'list', []
# Sort an already sorted index.
sort: ->
# Get or initialize the index.
index = @data.index or []
for p in @data.list
for m in p.milestones
# Run a comparator here inserting into index.
@data.sortKey
# Save the index.
@set 'index', index
onconstruct: -> onconstruct: ->
mediator.on '!projects/add', _.bind @add, @ mediator.on '!projects/add', _.bind @add, @
mediator.on '!projects/clear', _.bind @clear, @ mediator.on '!projects/clear', _.bind @clear, @
@ -40,7 +57,16 @@ module.exports = new Model
# Init the projects. # Init the projects.
@set 'list', lscache.get('projects') or [] @set 'list', lscache.get('projects') or []
# Persist projects in local storage (sans milestones).
@observe 'list', (projects) -> @observe 'list', (projects) ->
# Persist projects in local storage (sans milestones).
lscache.set 'projects', _.pluckMany projects, [ 'owner', 'name' ] lscache.set 'projects', _.pluckMany projects, [ 'owner', 'name' ]
, 'init': no # Update the index.
do @sort
, 'init': no
# Reset our index and re-sort.
@observe 'sortKey', ->
# Use pop as Ractive is glitchy.
@pop 'index' while @data.index.length
# Run the sort again.
do @sort

23
src/utils/search.coffee Normal file
View File

@ -0,0 +1,23 @@
# Binary search implementation with a custom comparator function.
module.exports = (arr, item, comparator) ->
# Numeric comparator.
comparator ?= (a, b) ->
switch
when a < b then -1
when a > b then +1
else 0
minIndex = 0
maxIndex = arr.length - 1
while minIndex <= maxIndex
index = (minIndex + maxIndex) / 2 | 0
existing = arr[index]
res = comparator existing, item
switch
when result < 0 then minIndex = index + 1
when result > 0 then maxIndex = index - 1
else return index
-1