| 1 | define(["dojo/_base/declare", "dojo/_base/array", "dojo/_base/html", "dojo/_base/lang", "dojo/dom-class", |
| 2 | "dojo/Stateful", "dojo/when"], |
| 3 | function(declare, arr, html, lang, domClass, Stateful, when){ |
| 4 | |
| 5 | return declare("dojox.calendar.StoreMixin", Stateful, { |
| 6 | |
| 7 | // summary: |
| 8 | // This mixin contains the store management. |
| 9 | |
| 10 | // store: dojo.store.Store |
| 11 | // The store that contains the events to display. |
| 12 | store: null, |
| 13 | |
| 14 | // query: Object |
| 15 | // A query that can be passed to when querying the store. |
| 16 | query: {}, |
| 17 | |
| 18 | // queryOptions: dojo/store/api/Store.QueryOptions? |
| 19 | // Options to be applied when querying the store. |
| 20 | queryOptions: null, |
| 21 | |
| 22 | // startTimeAttr: String |
| 23 | // The attribute of the store item that contains the start time of |
| 24 | // the events represented by this item. Default is "startTime". |
| 25 | startTimeAttr: "startTime", |
| 26 | |
| 27 | // endTimeAttr: String |
| 28 | // The attribute of the store item that contains the end time of |
| 29 | // the events represented by this item. Default is "endTime". |
| 30 | endTimeAttr: "endTime", |
| 31 | |
| 32 | // summaryAttr: String |
| 33 | // The attribute of the store item that contains the summary of |
| 34 | // the events represented by this item. Default is "summary". |
| 35 | summaryAttr: "summary", |
| 36 | |
| 37 | // allDayAttr: String |
| 38 | // The attribute of the store item that contains the all day state of |
| 39 | // the events represented by this item. Default is "allDay". |
| 40 | allDayAttr: "allDay", |
| 41 | |
| 42 | // subColumnAttr: String |
| 43 | // The attribute of the store item that contains the sub column name of |
| 44 | // the events represented by this item. Default is "calendar". |
| 45 | subColumnAttr: "calendar", |
| 46 | |
| 47 | // cssClassFunc: Function |
| 48 | // Optional function that returns a css class name to apply to item renderers that are displaying the specified item in parameter. |
| 49 | cssClassFunc: null, |
| 50 | |
| 51 | // decodeDate: Function? |
| 52 | // An optional function to transform store date into Date objects. Default is null. |
| 53 | decodeDate: null, |
| 54 | |
| 55 | // encodeDate: Function? |
| 56 | // An optional function to transform Date objects into store date. Default is null. |
| 57 | encodeDate: null, |
| 58 | |
| 59 | // displayedItemsInvalidated: Boolean |
| 60 | // Whether the data items displayed must be recomputed, usually after the displayed |
| 61 | // time range has changed. |
| 62 | // tags: |
| 63 | // protected |
| 64 | displayedItemsInvalidated: false, |
| 65 | |
| 66 | itemToRenderItem: function(item, store){ |
| 67 | // summary: |
| 68 | // Creates the render item based on the dojo.store item. It must be of the form: |
| 69 | // | { |
| 70 | // | id: Object, |
| 71 | // | startTime: Date, |
| 72 | // | endTime: Date, |
| 73 | // | summary: String |
| 74 | // | } |
| 75 | // By default it is building an object using the store id, the summaryAttr, |
| 76 | // startTimeAttr and endTimeAttr properties as well as decodeDate property if not null. |
| 77 | // Other fields or way to query fields can be used if needed. |
| 78 | // item: Object |
| 79 | // The store item. |
| 80 | // store: dojo.store.api.Store |
| 81 | // The store. |
| 82 | // returns: Object |
| 83 | if(this.owner){ |
| 84 | return this.owner.itemToRenderItem(item, store); |
| 85 | } |
| 86 | return { |
| 87 | id: store.getIdentity(item), |
| 88 | summary: item[this.summaryAttr], |
| 89 | startTime: (this.decodeDate && this.decodeDate(item[this.startTimeAttr])) || this.newDate(item[this.startTimeAttr], this.dateClassObj), |
| 90 | endTime: (this.decodeDate && this.decodeDate(item[this.endTimeAttr])) || this.newDate(item[this.endTimeAttr], this.dateClassObj), |
| 91 | allDay: item[this.allDayAttr] != null ? item[this.allDayAttr] : false, |
| 92 | subColumn: item[this.subColumnAttr], |
| 93 | cssClass: this.cssClassFunc ? this.cssClassFunc(item) : null |
| 94 | }; |
| 95 | }, |
| 96 | |
| 97 | renderItemToItem: function(/*Object*/ renderItem, /*dojo.store.api.Store*/ store){ |
| 98 | // summary: |
| 99 | // Create a store item based on the render item. It must be of the form: |
| 100 | // | { |
| 101 | // | id: Object |
| 102 | // | startTime: Date, |
| 103 | // | endTime: Date, |
| 104 | // | summary: String |
| 105 | // | } |
| 106 | // By default it is building an object using the summaryAttr, startTimeAttr and endTimeAttr properties |
| 107 | // and encodeDate property if not null. If the encodeDate property is null a Date object will be set in the start and end time. |
| 108 | // When using a JsonRest store, for example, it is recommended to transfer dates using the ISO format (see dojo.date.stamp). |
| 109 | // In that case, provide a custom function to the encodeDate property that is using the date ISO encoding provided by Dojo. |
| 110 | // renderItem: Object |
| 111 | // The render item. |
| 112 | // store: dojo.store.api.Store |
| 113 | // The store. |
| 114 | // returns:Object |
| 115 | if(this.owner){ |
| 116 | return this.owner.renderItemToItem(renderItem, store); |
| 117 | } |
| 118 | var item = {}; |
| 119 | item[store.idProperty] = renderItem.id; |
| 120 | item[this.summaryAttr] = renderItem.summary; |
| 121 | item[this.startTimeAttr] = (this.encodeDate && this.encodeDate(renderItem.startTime)) || renderItem.startTime; |
| 122 | item[this.endTimeAttr] = (this.encodeDate && this.encodeDate(renderItem.endTime)) || renderItem.endTime; |
| 123 | if(renderItem.subColumn){ |
| 124 | item[this.subColumnAttr] = renderItem.subColumn; |
| 125 | } |
| 126 | return this.getItemStoreState(renderItem) === "unstored" ? item : lang.mixin(renderItem._item, item); |
| 127 | }, |
| 128 | |
| 129 | _computeVisibleItems: function(renderData){ |
| 130 | // summary: |
| 131 | // Computes the data items that are in the displayed interval. |
| 132 | // renderData: Object |
| 133 | // The renderData that contains the start and end time of the displayed interval. |
| 134 | // tags: |
| 135 | // protected |
| 136 | |
| 137 | if(this.owner){ |
| 138 | return this.owner._computeVisibleItems(renderData); |
| 139 | } |
| 140 | renderData.items = this.storeManager._computeVisibleItems(renderData); |
| 141 | }, |
| 142 | |
| 143 | _initItems: function(items){ |
| 144 | // tags: |
| 145 | // private |
| 146 | this.set("items", items); |
| 147 | return items; |
| 148 | }, |
| 149 | |
| 150 | _refreshItemsRendering: function(renderData){ |
| 151 | }, |
| 152 | |
| 153 | _setStoreAttr: function(value){ |
| 154 | this.store = value; |
| 155 | return this.storeManager.set("store", value); |
| 156 | }, |
| 157 | |
| 158 | _getItemStoreStateObj: function(/*Object*/item){ |
| 159 | // tags |
| 160 | // private |
| 161 | return this.storeManager._getItemStoreStateObj(item); |
| 162 | }, |
| 163 | |
| 164 | getItemStoreState: function(item){ |
| 165 | // summary: |
| 166 | // Returns the creation state of an item. |
| 167 | // This state is changing during the interactive creation of an item. |
| 168 | // Valid values are: |
| 169 | // - "unstored": The event is being interactively created. It is not in the store yet. |
| 170 | // - "storing": The creation gesture has ended, the event is being added to the store. |
| 171 | // - "stored": The event is not in the two previous states, and is assumed to be in the store |
| 172 | // (not checking because of performance reasons, use store API for testing existence in store). |
| 173 | // item: Object |
| 174 | // The item. |
| 175 | // returns: String |
| 176 | |
| 177 | return this.storeManager.getItemStoreState(item); |
| 178 | }, |
| 179 | |
| 180 | _cleanItemStoreState: function(id){ |
| 181 | this.storeManager._cleanItemStoreState(id); |
| 182 | }, |
| 183 | |
| 184 | _setItemStoreState: function(/*Object*/item, /*String*/state){ |
| 185 | // tags |
| 186 | // private |
| 187 | this.storeManager._setItemStoreState(item, state); |
| 188 | } |
| 189 | |
| 190 | }); |
| 191 | |
| 192 | }); |