Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
16 KiB

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