]> git.proxmox.com Git - extjs.git/blame - extjs/modern/modern/src/dataview/component/DataItem.js
add extjs 6.0.1 sources
[extjs.git] / extjs / modern / modern / src / dataview / component / DataItem.js
CommitLineData
6527f429
DM
1/**\r
2 * A DataItem is a container for records inside of {@link Ext.dataview.DataView} with useComponents: true.\r
3 * It ties together {@link Ext.data.Model records} to its contained Components. Consider the following example:\r
4 *\r
5 * @example phone portrait preview\r
6 * // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! MODEL\r
7 *\r
8 * Ext.define('TestModel', {\r
9 * extend: 'Ext.data.Model',\r
10 * config: {\r
11 * fields: [{\r
12 * name: 'val1'\r
13 * }, {\r
14 * name: 'val2'\r
15 * }]\r
16 * }\r
17 * });\r
18 *\r
19 * // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! STORE\r
20 *\r
21 * Ext.define('TestStore', {\r
22 * extend: 'Ext.data.Store',\r
23 * config: {\r
24 * data: [{\r
25 * val1: 'A Button',\r
26 * val2: 'with text'\r
27 * }, {\r
28 * val1: 'The Button',\r
29 * val2: 'more text'\r
30 * }, {\r
31 * val1: 'My Button',\r
32 * val2: 'My Text'\r
33 * }],\r
34 * model: 'TestModel',\r
35 * storeId: 'TestStore'\r
36 * }\r
37 * });\r
38 *\r
39 * // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DATA ITEM\r
40 *\r
41 * Ext.define('MyDataItem', {\r
42 * extend: 'Ext.dataview.component.DataItem',\r
43 * alias: 'widget.mydataitem',\r
44 * config: {\r
45 * padding: 10,\r
46 * layout: {\r
47 * type: 'hbox'\r
48 * },\r
49 * defaults: {\r
50 * margin: 5\r
51 * },\r
52 * items: [{\r
53 * xtype: 'button',\r
54 * text: 'Val1'\r
55 * }, {\r
56 * xtype: 'component',\r
57 * flex: 1,\r
58 * html: 'val2',\r
59 * itemId: 'textCmp'\r
60 * }]\r
61 * },\r
62 * updateRecord: function(record) {\r
63 * var me = this;\r
64 *\r
65 * me.down('button').setText(record.get('val1'));\r
66 * me.down('#textCmp').setHtml(record.get('val2'));\r
67 *\r
68 * me.callParent(arguments);\r
69 * }\r
70 * });\r
71 *\r
72 * // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DATA VIEW\r
73 *\r
74 * Ext.define('MyDataView', {\r
75 * extend: 'Ext.dataview.DataView',\r
76 * config: {\r
77 * defaultType: 'mydataitem',\r
78 * useComponents: true\r
79 * }\r
80 * });\r
81 *\r
82 * // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RUN\r
83 *\r
84 * Ext.create('MyDataView', {\r
85 * fullscreen: true,\r
86 * store: Ext.create('TestStore')\r
87 * });\r
88 *\r
89 * Another way to accomplish this is via a {@link #dataMap dataMap} configuration.\r
90 *\r
91 * For example, lets say you have a `text` configuration which, when applied, gets turned into an instance of an\r
92 * Ext.Component. We want to update the {@link #html} of a sub-component when the 'text' field of the record gets\r
93 * changed.\r
94 *\r
95 * As you can see below, it is simply a matter of setting the key of the object to be the getter of the config\r
96 * (getText), and then give that property a value of an object, which then has 'setHtml' (the html setter) as the key,\r
97 * and 'text' (the field name) as the value. You can continue this for a as many sub-components as you wish.\r
98 *\r
99 * dataMap: {\r
100 * // When the record is updated, get the text configuration, and\r
101 * // call {@link #setHtml} with the 'text' field of the record.\r
102 * getText: {\r
103 * setHtml: 'text'\r
104 * },\r
105 *\r
106 * // When the record is updated, get the userName configuration, and\r
107 * // call {@link #setHtml} with the 'from_user' field of the record.\r
108 * getUserName: {\r
109 * setHtml: 'from_user'\r
110 * },\r
111 *\r
112 * // When the record is updated, get the avatar configuration, and\r
113 * // call `setSrc` with the 'profile_image_url' field of the record.\r
114 * getAvatar: {\r
115 * setSrc: 'profile_image_url'\r
116 * }\r
117 * }\r
118 */\r
119\r
120Ext.define('Ext.dataview.component.DataItem', {\r
121 extend: 'Ext.Container',\r
122 xtype : 'dataitem',\r
123\r
124 config: {\r
125 baseCls: Ext.baseCSSPrefix + 'data-item',\r
126\r
127 defaultType: 'component',\r
128\r
129 /**\r
130 * @cfg {Ext.data.Model} record The model instance of this DataItem. It is controlled by the Component DataView.\r
131 * @accessor\r
132 */\r
133 record: null,\r
134\r
135 /**\r
136 * @cfg {String} itemCls\r
137 * An additional CSS class to apply to items within the DataView.\r
138 * @accessor\r
139 */\r
140 itemCls: null,\r
141\r
142 /**\r
143 * @cfg dataMap\r
144 * The dataMap allows you to map {@link #record} fields to specific configurations in this component.\r
145 *\r
146 * For example, lets say you have a `text` configuration which, when applied, gets turned into an instance of an Ext.Component.\r
147 * We want to update the {@link #html} of this component when the 'text' field of the record gets changed.\r
148 * For example:\r
149 *\r
150 * dataMap: {\r
151 * getText: {\r
152 * setHtml: 'text'\r
153 * }\r
154 * }\r
155 *\r
156 * In this example, it is simply a matter of setting the key of the object to be the getter of the config (`getText`), and then give that\r
157 * property a value of an object, which then has `setHtml` (the html setter) as the key, and `text` (the field name) as the value.\r
158 */\r
159 dataMap: {},\r
160\r
161 /**\r
162 * @private dataview\r
163 */\r
164 dataview: null,\r
165\r
166 width: '100%',\r
167\r
168 items: [{\r
169 xtype: 'component'\r
170 }]\r
171 },\r
172\r
173 updateBaseCls: function(newBaseCls, oldBaseCls) {\r
174 var me = this;\r
175\r
176 me.callParent(arguments);\r
177 },\r
178\r
179 updateItemCls: function(newCls, oldCls) {\r
180 if (oldCls) {\r
181 this.removeCls(oldCls);\r
182 }\r
183 if (newCls) {\r
184 this.addCls(newCls);\r
185 }\r
186 },\r
187\r
188 doMapData: function(dataMap, data, item) {\r
189 var componentName, component, setterMap, setterName;\r
190\r
191 for (componentName in dataMap) {\r
192 setterMap = dataMap[componentName];\r
193 component = this[componentName]();\r
194 if (component) {\r
195 for (setterName in setterMap) {\r
196 if (data && component[setterName] && data[setterMap[setterName]] !== undefined && data[setterMap[setterName]] !== null) {\r
197 component[setterName](data[setterMap[setterName]]);\r
198 }\r
199 }\r
200 }\r
201 }\r
202\r
203 if (item) {\r
204 // Bypassing setter because sometimes we pass the same object (different properties)\r
205 item.updateData(data);\r
206 }\r
207 },\r
208\r
209 /**\r
210 * Updates this container's child items, passing through the `dataMap`.\r
211 * @param {Ext.data.Model} newRecord\r
212 * @private\r
213 */\r
214 updateRecord: function(newRecord) {\r
215 if (!newRecord) {\r
216 return;\r
217 }\r
218 this._record = newRecord;\r
219\r
220 var me = this,\r
221 dataview = me.dataview || this.getDataview(),\r
222 data = dataview.prepareData(newRecord.getData(true), dataview.getStore().indexOf(newRecord), newRecord),\r
223 items = me.getItems(),\r
224 item = items.first(),\r
225 dataMap = me.getDataMap();\r
226\r
227 if (!item) {\r
228 return;\r
229 }\r
230 if (dataMap) {\r
231 this.doMapData(dataMap, data, item);\r
232 }\r
233\r
234 /**\r
235 * @event updatedata\r
236 * Fires whenever the data of the DataItem is updated.\r
237 * @param {Ext.dataview.component.DataItem} this The DataItem instance.\r
238 * @param {Object} newData The new data.\r
239 */\r
240 me.fireEvent('updatedata', me, data);\r
241 }\r
242});\r