| 1 | // datasource base class, the "M" in MVC |
|---|
| 2 | // subclass this and override theData to provide your data |
|---|
| 3 | |
|---|
| 4 | DataSource = new Class(Object, { |
|---|
| 5 | |
|---|
| 6 | init: function (initialData) { |
|---|
| 7 | DataSource.superClass.init.apply(this, arguments); |
|---|
| 8 | this.watchers = []; |
|---|
| 9 | this.theData = defined(initialData) ? initialData : []; |
|---|
| 10 | this.sortField = ""; |
|---|
| 11 | this.sortType = ""; |
|---|
| 12 | this.sortDesc = false; |
|---|
| 13 | }, |
|---|
| 14 | |
|---|
| 15 | addWatcher: function (callback) { |
|---|
| 16 | this.watchers.add(callback); |
|---|
| 17 | }, |
|---|
| 18 | |
|---|
| 19 | removeWatcher: function (callback) { |
|---|
| 20 | this.watchers.remove(callback); |
|---|
| 21 | }, |
|---|
| 22 | |
|---|
| 23 | // call this if updating data and not using _setData |
|---|
| 24 | _updated: function () { |
|---|
| 25 | this.callWatchers(); |
|---|
| 26 | }, |
|---|
| 27 | |
|---|
| 28 | callWatchers: function () { |
|---|
| 29 | for (var i = 0; i < this.watchers.length; i++) |
|---|
| 30 | this.watchers[i].apply(this, [this.data()]); |
|---|
| 31 | }, |
|---|
| 32 | |
|---|
| 33 | setData: function (theData) { |
|---|
| 34 | this.theData = theData; |
|---|
| 35 | |
|---|
| 36 | if (this.sortField) |
|---|
| 37 | this.sortDataBy(this.sortField, this.sortType, this.sortDesc); |
|---|
| 38 | |
|---|
| 39 | this._setData(theData); |
|---|
| 40 | }, |
|---|
| 41 | |
|---|
| 42 | _setData: function (theData) { |
|---|
| 43 | this.theData = theData; |
|---|
| 44 | this.callWatchers(); |
|---|
| 45 | return theData; |
|---|
| 46 | }, |
|---|
| 47 | |
|---|
| 48 | data: function () { |
|---|
| 49 | return this.theData; |
|---|
| 50 | }, |
|---|
| 51 | |
|---|
| 52 | sortBy: function () { |
|---|
| 53 | return this.sortField; |
|---|
| 54 | }, |
|---|
| 55 | |
|---|
| 56 | sortInverted: function () { |
|---|
| 57 | return this.sortDesc; |
|---|
| 58 | }, |
|---|
| 59 | |
|---|
| 60 | // mimic some array functionality |
|---|
| 61 | push: function (data) { |
|---|
| 62 | this.theData.push(data); |
|---|
| 63 | this.callWatchers(); |
|---|
| 64 | }, |
|---|
| 65 | |
|---|
| 66 | pop: function () { |
|---|
| 67 | var val = this.theData.pop(); |
|---|
| 68 | this.callWatchers(); |
|---|
| 69 | return val; |
|---|
| 70 | }, |
|---|
| 71 | |
|---|
| 72 | indexOf: function (value) { |
|---|
| 73 | return this.theData.indexOf(value); |
|---|
| 74 | }, |
|---|
| 75 | |
|---|
| 76 | remove: function (value) { |
|---|
| 77 | this.theData.remove(value); |
|---|
| 78 | this.callWatchers(); |
|---|
| 79 | }, |
|---|
| 80 | |
|---|
| 81 | empty: function () { |
|---|
| 82 | this.theData = []; |
|---|
| 83 | this.callWatchers(); |
|---|
| 84 | }, |
|---|
| 85 | |
|---|
| 86 | length: function () { |
|---|
| 87 | return this.theData.length; |
|---|
| 88 | }, |
|---|
| 89 | |
|---|
| 90 | totalLength: function () { |
|---|
| 91 | return this.allData().length; |
|---|
| 92 | }, |
|---|
| 93 | |
|---|
| 94 | allData: function () { |
|---|
| 95 | var theData = this.theData; |
|---|
| 96 | |
|---|
| 97 | if (this.dataField && theData) |
|---|
| 98 | theData = theData[this.dataField]; |
|---|
| 99 | |
|---|
| 100 | return theData; |
|---|
| 101 | }, |
|---|
| 102 | |
|---|
| 103 | sortDataBy: function (field, type, invert) { |
|---|
| 104 | this.sortField = field; |
|---|
| 105 | this.sortDesc = invert; |
|---|
| 106 | this.sortType = type; |
|---|
| 107 | |
|---|
| 108 | if (!field || !this.theData || !this.theData.sort) |
|---|
| 109 | return; |
|---|
| 110 | |
|---|
| 111 | var sorted = this.theData.sort(function (a, b) { |
|---|
| 112 | var ad = a[""+field], bd = b[""+field]; |
|---|
| 113 | ad = ad ? ad : ""; |
|---|
| 114 | bd = bd ? bd : ""; |
|---|
| 115 | |
|---|
| 116 | switch(type) { |
|---|
| 117 | |
|---|
| 118 | case "string": |
|---|
| 119 | var aname = ad.toUpperCase(), bname = bd.toUpperCase(); |
|---|
| 120 | |
|---|
| 121 | if (aname < bname) |
|---|
| 122 | return -1; |
|---|
| 123 | else if (aname > bname) |
|---|
| 124 | return 1; |
|---|
| 125 | else |
|---|
| 126 | return 0; |
|---|
| 127 | |
|---|
| 128 | case "isodate": |
|---|
| 129 | var datA = Date.fromISOString(ad) || new Date(0); |
|---|
| 130 | var datB = Date.fromISOString(bd) || new Date(0); |
|---|
| 131 | |
|---|
| 132 | return ((datA - datB) || 0); |
|---|
| 133 | |
|---|
| 134 | default: |
|---|
| 135 | case "numeric": |
|---|
| 136 | return ad - bd; |
|---|
| 137 | |
|---|
| 138 | } |
|---|
| 139 | }); |
|---|
| 140 | |
|---|
| 141 | if (invert) |
|---|
| 142 | sorted.reverse(); |
|---|
| 143 | |
|---|
| 144 | this._setData(sorted); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | }); |
|---|