d8b9584a5b31758293386293228d93c52cfcf597.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.grid.PropertyRecord A specific {@link Ext.data.Record} type that
  8. * represents a name/value pair and is made to work with the
  9. * {@link Ext.grid.PropertyGrid}. Typically, PropertyRecords do not need
  10. * to be created directly as they can be created implicitly by simply
  11. * using the appropriate data configs either via the
  12. * {@link Ext.grid.PropertyGrid#source} config property or by calling
  13. * {@link Ext.grid.PropertyGrid#setSource}. However, if the need arises,
  14. * these records can also be created explicitly as shwon below. Example
  15. * usage:
  16. *
  17. * <pre><code>
  18. * var rec = new Ext.grid.PropertyRecord({
  19. * name : 'Birthday',
  20. * value : new Date(Date.parse('05/26/1972'))
  21. * });
  22. * // Add record to an already populated grid
  23. * grid.store.addSorted(rec);
  24. * </code></pre>
  25. *
  26. * @constructor
  27. * @param {Object}
  28. * config A data object in the format: {name: [name], value:
  29. * [value]}. The specified value's type will be read automatically by
  30. * the grid to determine the type of editor to use when displaying
  31. * it.
  32. */
  33. Ext.grid.PropertyRecord = Ext.data.Record.create([{
  34. name : 'name',
  35. type : 'string'
  36. }, 'value']);
  37. /**
  38. * @class Ext.grid.PropertyStore
  39. * @extends Ext.util.Observable A custom wrapper for the
  40. * {@link Ext.grid.PropertyGrid}'s {@link Ext.data.Store}. This class
  41. * handles the mapping between the custom data source objects supported
  42. * by the grid and the {@link Ext.grid.PropertyRecord} format required
  43. * for compatibility with the underlying store. Generally this class
  44. * should not need to be used directly -- the grid's data should be
  45. * accessed from the underlying store via the {@link #store} property.
  46. * @constructor
  47. * @param {Ext.grid.Grid}
  48. * grid The grid this store will be bound to
  49. * @param {Object}
  50. * source The source data config object
  51. */
  52. Ext.grid.PropertyStore = function(grid, source) {
  53. this.grid = grid;
  54. this.store = new Ext.data.Store({
  55. recordType : Ext.grid.PropertyRecord
  56. });
  57. this.store.on('update', this.onUpdate, this);
  58. if (source) {
  59. this.setSource(source);
  60. }
  61. Ext.grid.PropertyStore.superclass.constructor.call(this);
  62. };
  63. Ext.extend(Ext.grid.PropertyStore, Ext.util.Observable, {
  64. // protected - should only be called by the grid. Use grid.setSource
  65. // instead.
  66. setSource : function(o) {
  67. this.source = o;
  68. this.store.removeAll();
  69. var data = [];
  70. for (var k in o) {
  71. if (this.isEditableValue(o[k])) {
  72. data.push(new Ext.grid.PropertyRecord({
  73. name : k,
  74. value : o[k]
  75. }, k));
  76. }
  77. }
  78. this.store.loadRecords({
  79. records : data
  80. }, {}, true);
  81. },
  82. // private
  83. onUpdate : function(ds, record, type) {
  84. if (type == Ext.data.Record.EDIT) {
  85. var v = record.data['value'];
  86. var oldValue = record.modified['value'];
  87. if (this.grid.fireEvent('beforepropertychange',
  88. this.source, record.id, v, oldValue) !== false) {
  89. this.source[record.id] = v;
  90. record.commit();
  91. this.grid.fireEvent('propertychange', this.source,
  92. record.id, v, oldValue);
  93. } else {
  94. record.reject();
  95. }
  96. }
  97. },
  98. // private
  99. getProperty : function(row) {
  100. return this.store.getAt(row);
  101. },
  102. // private
  103. isEditableValue : function(val) {
  104. if (val && val instanceof Date) {
  105. return true;
  106. } else if (typeof val == 'object' || typeof val == 'function') {
  107. return false;
  108. }
  109. return true;
  110. },
  111. // private
  112. setValue : function(prop, value) {
  113. this.source[prop] = value;
  114. this.store.getById(prop).set('value', value);
  115. },
  116. // protected - should only be called by the grid. Use grid.getSource
  117. // instead.
  118. getSource : function() {
  119. return this.source;
  120. }
  121. });
  122. /**
  123. * @class Ext.grid.PropertyColumnModel
  124. * @extends Ext.grid.ColumnModel A custom column model for the
  125. * {@link Ext.grid.PropertyGrid}. Generally it should not need to be
  126. * used directly.
  127. * @constructor
  128. * @param {Ext.grid.Grid}
  129. * grid The grid this store will be bound to
  130. * @param {Object}
  131. * source The source data config object
  132. */
  133. Ext.grid.PropertyColumnModel = function(grid, store) {
  134. this.grid = grid;
  135. var g = Ext.grid;
  136. g.PropertyColumnModel.superclass.constructor.call(this, [{
  137. header : this.nameText,
  138. width : 50,
  139. sortable : true,
  140. dataIndex : 'name',
  141. id : 'name'
  142. }, {
  143. header : this.valueText,
  144. width : 50,
  145. resizable : false,
  146. dataIndex : 'value',
  147. id : 'value'
  148. }]);
  149. this.store = store;
  150. this.bselect = Ext.DomHelper.append(document.body, {
  151. tag : 'select',
  152. cls : 'x-grid-editor x-hide-display',
  153. children : [{
  154. tag : 'option',
  155. value : 'true',
  156. html : 'true'
  157. }, {
  158. tag : 'option',
  159. value : 'false',
  160. html : 'false'
  161. }]
  162. });
  163. var f = Ext.form;
  164. var bfield = new f.Field({
  165. el : this.bselect,
  166. bselect : this.bselect,
  167. autoShow : true,
  168. getValue : function() {
  169. return this.bselect.value == 'true';
  170. }
  171. });
  172. this.editors = {
  173. 'date' : new g.GridEditor(new f.DateField({
  174. selectOnFocus : true
  175. })),
  176. 'string' : new g.GridEditor(new f.TextField({
  177. selectOnFocus : true
  178. })),
  179. 'number' : new g.GridEditor(new f.NumberField({
  180. selectOnFocus : true,
  181. style : 'text-align:left;'
  182. })),
  183. 'boolean' : new g.GridEditor(bfield)
  184. };
  185. this.renderCellDelegate = this.renderCell.createDelegate(this);
  186. this.renderPropDelegate = this.renderProp.createDelegate(this);
  187. };
  188. Ext.extend(Ext.grid.PropertyColumnModel, Ext.grid.ColumnModel, {
  189. // private - strings used for locale support
  190. nameText : 'Name',
  191. valueText : 'Value',
  192. dateFormat : 'm/j/Y',
  193. // private
  194. renderDate : function(dateVal) {
  195. return dateVal.dateFormat(this.dateFormat);
  196. },
  197. // private
  198. renderBool : function(bVal) {
  199. return bVal ? 'true' : 'false';
  200. },
  201. // private
  202. isCellEditable : function(colIndex, rowIndex) {
  203. return colIndex == 1;
  204. },
  205. // private
  206. getRenderer : function(col) {
  207. return col == 1
  208. ? this.renderCellDelegate
  209. : this.renderPropDelegate;
  210. },
  211. // private
  212. renderProp : function(v) {
  213. return this.getPropertyName(v);
  214. },
  215. // private
  216. renderCell : function(val) {
  217. var rv = val;
  218. if (val instanceof Date) {
  219. rv = this.renderDate(val);
  220. } else if (typeof val == 'boolean') {
  221. rv = this.renderBool(val);
  222. }
  223. return Ext.util.Format.htmlEncode(rv);
  224. },
  225. // private
  226. getPropertyName : function(name) {
  227. var pn = this.grid.propertyNames;
  228. return pn && pn[name] ? pn[name] : name;
  229. },
  230. // private
  231. getCellEditor : function(colIndex, rowIndex) {
  232. var p = this.store.getProperty(rowIndex);
  233. var n = p.data['name'], val = p.data['value'];
  234. if (this.grid.customEditors[n]) {
  235. return this.grid.customEditors[n];
  236. }
  237. if (val instanceof Date) {
  238. return this.editors['date'];
  239. } else if (typeof val == 'number') {
  240. return this.editors['number'];
  241. } else if (typeof val == 'boolean') {
  242. return this.editors['boolean'];
  243. } else {
  244. return this.editors['string'];
  245. }
  246. }
  247. });
  248. /**
  249. * @class Ext.grid.PropertyGrid
  250. * @extends Ext.grid.EditorGridPanel A specialized grid implementation intended
  251. * to mimic the traditional property grid as typically seen in
  252. * development IDEs. Each row in the grid represents a property of some
  253. * object, and the data is stored as a set of name/value pairs in
  254. * {@link Ext.grid.PropertyRecord}s. Example usage:
  255. *
  256. * <pre><code>
  257. * var grid = new Ext.grid.PropertyGrid({
  258. * title : 'Properties Grid',
  259. * autoHeight : true,
  260. * width : 300,
  261. * renderTo : 'grid-ct',
  262. * source : {
  263. * &quot;(name)&quot; : &quot;My Object&quot;,
  264. * &quot;Created&quot; : new Date(Date.parse('10/15/2006')),
  265. * &quot;Available&quot; : false,
  266. * &quot;Version&quot; : .01,
  267. * &quot;Description&quot; : &quot;A test object&quot;
  268. * }
  269. * });
  270. * </pre></code>
  271. * @constructor
  272. * @param {Object}
  273. * config The grid config object
  274. */
  275. Ext.grid.PropertyGrid = Ext.extend(Ext.grid.EditorGridPanel, {
  276. /**
  277. * @cfg {Object} source A data object to use as the data source of
  278. * the grid (see {@link #setSource} for details).
  279. */
  280. /**
  281. * @cfg {Object} customEditors An object containing name/value pairs
  282. * of custom editor type definitions that allow the grid to
  283. * support additional types of editable fields. By default, the
  284. * grid supports strongly-typed editing of strings, dates,
  285. * numbers and booleans using built-in form editors, but any
  286. * custom type can be supported and associated with a custom
  287. * input control by specifying a custom editor. The name of the
  288. * editor type should correspond with the name of the property
  289. * that will use the editor. Example usage:
  290. *
  291. * <pre><code>
  292. * var grid = new Ext.grid.PropertyGrid({
  293. * ...
  294. * customEditors: {
  295. * 'Start Time': new Ext.grid.GridEditor(new Ext.form.TimeField({selectOnFocus:true}))
  296. * },
  297. * source: {
  298. * 'Start Time': '10:00 AM'
  299. * }
  300. * });
  301. *
  302. * </code></pre>
  303. */
  304. // private config overrides
  305. enableColLock : false,
  306. enableColumnMove : false,
  307. stripeRows : false,
  308. trackMouseOver : false,
  309. clicksToEdit : 1,
  310. enableHdMenu : false,
  311. viewConfig : {
  312. forceFit : true
  313. },
  314. // private
  315. initComponent : function() {
  316. this.customEditors = this.customEditors || {};
  317. this.lastEditRow = null;
  318. var store = new Ext.grid.PropertyStore(this);
  319. this.propStore = store;
  320. var cm = new Ext.grid.PropertyColumnModel(this, store);
  321. store.store.sort('name', 'ASC');
  322. this.addEvents(
  323. /**
  324. * @event beforepropertychange Fires before a property
  325. * value changes. Handlers can return false to
  326. * cancel the property change (this will
  327. * internally call {@link Ext.data.Record#reject}
  328. * on the property's record).
  329. * @param {Object}
  330. * source The source data object for the grid
  331. * (corresponds to the same object passed in
  332. * as the {@link #source} config property).
  333. * @param {String}
  334. * recordId The record's id in the data store
  335. * @param {Mixed}
  336. * value The current edited property value
  337. * @param {Mixed}
  338. * oldValue The original property value prior
  339. * to editing
  340. */
  341. 'beforepropertychange',
  342. /**
  343. * @event propertychange Fires after a property value
  344. * has changed.
  345. * @param {Object}
  346. * source The source data object for the grid
  347. * (corresponds to the same object passed in
  348. * as the {@link #source} config property).
  349. * @param {String}
  350. * recordId The record's id in the data store
  351. * @param {Mixed}
  352. * value The current edited property value
  353. * @param {Mixed}
  354. * oldValue The original property value prior
  355. * to editing
  356. */
  357. 'propertychange');
  358. this.cm = cm;
  359. this.ds = store.store;
  360. Ext.grid.PropertyGrid.superclass.initComponent.call(this);
  361. this.selModel.on('beforecellselect', function(sm, rowIndex,
  362. colIndex) {
  363. if (colIndex === 0) {
  364. this.startEditing.defer(200, this,
  365. [rowIndex, 1]);
  366. return false;
  367. }
  368. }, this);
  369. },
  370. // private
  371. onRender : function() {
  372. Ext.grid.PropertyGrid.superclass.onRender
  373. .apply(this, arguments);
  374. this.getGridEl().addClass('x-props-grid');
  375. },
  376. // private
  377. afterRender : function() {
  378. Ext.grid.PropertyGrid.superclass.afterRender.apply(this,
  379. arguments);
  380. if (this.source) {
  381. this.setSource(this.source);
  382. }
  383. },
  384. /**
  385. * Sets the source data object containing the property data. The
  386. * data object can contain one or more name/value pairs representing
  387. * all of the properties of an object to display in the grid, and
  388. * this data will automatically be loaded into the grid's
  389. * {@link #store}. If the grid already contains data, this method
  390. * will replace any existing data. See also the {@link #source}
  391. * config value. Example usage:
  392. *
  393. * <pre><code>
  394. * grid.setSource({
  395. * &quot;(name)&quot; : &quot;My Object&quot;,
  396. * &quot;Created&quot; : new Date(Date.parse('10/15/2006')),
  397. * &quot;Available&quot; : false,
  398. * &quot;Version&quot; : .01,
  399. * &quot;Description&quot; : &quot;A test object&quot;
  400. * });
  401. * </code></pre>
  402. *
  403. * @param {Object}
  404. * source The data object
  405. */
  406. setSource : function(source) {
  407. this.propStore.setSource(source);
  408. },
  409. /**
  410. * Gets the source data object containing the property data. See
  411. * {@link #setSource} for details regarding the format of the data
  412. * object.
  413. *
  414. * @return {Object} The data object
  415. */
  416. getSource : function() {
  417. return this.propStore.getSource();
  418. }
  419. });