-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort.coffee
82 lines (78 loc) · 2.33 KB
/
sort.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{isArray, clone, capitalize} = require("./_helpers")
sorting = (field, reverse, cast) ->
if cast
getter = (obj) -> cast(obj[field])
else
getter = (obj) -> obj[field]
return (a,b) ->
a = getter(a)
b = getter(b)
return reverse * ((a > b) - (b > a))
module.exports =
_name: "sort"
_v: 1
_mergers: [
require("./_merger").concat source: "sort"
]
_rebind: "$sort"
mixins: [
require "./computed"
]
methods:
$sort:
init: (o) ->
@$sort.__s ?= {}
@$sort.__s[o.name] = o
o.watcher = @$watch.path parent:o, name: "sortBy", value: o.sortBy
cSort = @$computed.init get: ->
fns = o.sortBy.map ([field,reverse,cast]) -> sorting(field,reverse,cast)
return null if fns.length == 0
return (a, b) ->
for fn in fns
result = fn(a, b)
return result if result != 0
return 0
@$computed.init path: "sort"+capitalize(o.name), get: ->
result = {}
for [field, dir] in o.sortBy
result[field] = dir
return result
@$computed.init path: "sort"+capitalize(o.name)+"Symbol", get: ->
result = {}
for [field, dir] in o.sortBy
result[field] = if dir > 0 then '▲' else '▼'
return result
@$computed.init path: "sorted"+capitalize(o.name), get: ->
tmp = @$path.resolveValue(o.name).slice()
sorter = cSort.getter()
if sorter
tmp.sort(sorter)
else
tmp
by: (o) ->
if (s = @$sort.__s?[o.target])?
sortBy = s.sortBy
f = o.field
for arr,i in sortBy
[field,direction] = arr
if field == f
arr[1] = -1 * direction
found = i
unless found?
if o.add
sortBy.push [f,1]
s.watcher.notify()
else
s.sortBy = [[f,1]]
else unless o.add
s.sortBy = sortBy.splice(found,1)
else
s.watcher.notify()
connectedCallback: ->
if @_isFirstConnect
for sort in @sort
for k,v of sort
v = [v] unless isArray(v[0])
@$sort.init name: k, sortBy: v
test module.exports, {}, (el) ->
it "should work", ->