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.

332 lines
12 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. // A DropZone which cooperates with DragZones whose dragData contains
  16. // a "field" property representing a form Field. Fields may be dropped onto
  17. // grid data cells containing a matching data type.
  18. Ext.ux.CellFieldDropZone = Ext.extend(Ext.dd.DropZone, {
  19. constructor: function(){},
  20. // Call the DropZone constructor using the View's scrolling element
  21. // only after the grid has been rendered.
  22. init: function(grid) {
  23. if (grid.rendered) {
  24. this.grid = grid;
  25. this.view = grid.getView();
  26. this.store = grid.getStore();
  27. Ext.ux.CellFieldDropZone.superclass.constructor.call(this, this.view.scroller);
  28. } else {
  29. grid.on('render', this.init, this);
  30. }
  31. },
  32. // Scroll the main configured Element when we drag close to the edge
  33. containerScroll: true,
  34. getTargetFromEvent: function(e) {
  35. // Ascertain whether the mousemove is within a grid cell
  36. var t = e.getTarget(this.view.cellSelector);
  37. if (t) {
  38. // We *are* within a grid cell, so ask the View exactly which one,
  39. // Extract data from the Model to create a target object for
  40. // processing in subsequent onNodeXXXX methods. Note that the target does
  41. // not have to be a DOM element. It can be whatever the noNodeXXX methods are
  42. // programmed to expect.
  43. var rowIndex = this.view.findRowIndex(t);
  44. var columnIndex = this.view.findCellIndex(t);
  45. if ((rowIndex !== false) && (columnIndex !== false)) {
  46. return {
  47. node: t,
  48. record: this.store.getAt(rowIndex),
  49. fieldName: this.grid.getColumnModel().getDataIndex(columnIndex)
  50. };
  51. }
  52. }
  53. },
  54. // On Node enter, see if it is valid for us to drop the field on that type of column.
  55. onNodeEnter: function(target, dd, e, dragData) {
  56. delete this.dropOK;
  57. if (!target) {
  58. return;
  59. }
  60. // Check that a field is being dragged.
  61. var f = dragData.field;
  62. if (!f) {
  63. return;
  64. }
  65. // Check whether the data type of the column being dropped on accepts the
  66. // dragged field type. If so, set dropOK flag, and highlight the target node.
  67. var type = target.record.fields.get(target.fieldName).type,
  68. types = Ext.data.Types;
  69. switch(type){
  70. case types.FLOAT:
  71. case types.INT:
  72. if (!f.isXType('numberfield')) {
  73. return;
  74. }
  75. break;
  76. case types.DATE:
  77. if (!f.isXType('datefield')) {
  78. return;
  79. }
  80. break;
  81. case types.BOOL:
  82. if (!f.isXType('checkbox')) {
  83. return;
  84. }
  85. }
  86. this.dropOK = true;
  87. Ext.fly(target.node).addClass('x-drop-target-active');
  88. },
  89. // Return the class name to add to the drag proxy. This provides a visual indication
  90. // of drop allowed or not allowed.
  91. onNodeOver: function(target, dd, e, dragData) {
  92. return this.dropOK ? this.dropAllowed : this.dropNotAllowed;
  93. },
  94. // nhighlight the target node.
  95. onNodeOut: function(target, dd, e, dragData) {
  96. Ext.fly(target.node).removeClass('x-drop-target-active');
  97. },
  98. // Process the drop event if we have previously ascertained that a drop is OK.
  99. onNodeDrop: function(target, dd, e, dragData) {
  100. if (this.dropOK) {
  101. target.record.set(target.fieldName, dragData.field.getValue());
  102. return true;
  103. }
  104. }
  105. });
  106. // A class which makes Fields within a Panel draggable.
  107. // the dragData delivered to a coooperating DropZone's methods contains
  108. // the dragged Field in the property "field".
  109. Ext.ux.PanelFieldDragZone = Ext.extend(Ext.dd.DragZone, {
  110. constructor: function(){},
  111. // Call the DRagZone's constructor. The Panel must have been rendered.
  112. init: function(panel) {
  113. if (panel.nodeType) {
  114. Ext.ux.PanelFieldDragZone.superclass.init.apply(this, arguments);
  115. } else {
  116. if (panel.rendered) {
  117. Ext.ux.PanelFieldDragZone.superclass.constructor.call(this, panel.getEl());
  118. var i = Ext.fly(panel.getEl()).select('input');
  119. i.unselectable();
  120. } else {
  121. panel.on('afterlayout', this.init, this, {single: true});
  122. }
  123. }
  124. },
  125. scroll: false,
  126. // On mousedown, we ascertain whether it is on one of our draggable Fields.
  127. // If so, we collect data about the draggable object, and return a drag data
  128. // object which contains our own data, plus a "ddel" property which is a DOM
  129. // node which provides a "view" of the dragged data.
  130. getDragData: function(e) {
  131. var t = e.getTarget('input');
  132. if (t) {
  133. e.stopEvent();
  134. // Ugly code to "detach" the drag gesture from the input field.
  135. // Without this, Opera never changes the mouseover target from the input field
  136. // even when dragging outside of the field - it just keeps selecting.
  137. if (Ext.isOpera) {
  138. Ext.fly(t).on('mousemove', function(e1){
  139. t.style.visibility = 'hidden';
  140. (function(){
  141. t.style.visibility = '';
  142. }).defer(1);
  143. }, null, {single:true});
  144. }
  145. // Get the data we are dragging: the Field
  146. // create a ddel for the drag proxy to display
  147. var f = Ext.getCmp(t.id);
  148. var d = document.createElement('div');
  149. d.className = 'x-form-text';
  150. d.appendChild(document.createTextNode(t.value));
  151. Ext.fly(d).setWidth(f.getEl().getWidth());
  152. return {
  153. field: f,
  154. ddel: d
  155. };
  156. }
  157. },
  158. // The coordinates to slide the drag proxy back to on failed drop.
  159. getRepairXY: function() {
  160. return this.dragData.field.getEl().getXY();
  161. }
  162. });
  163. Ext.onReady(function(){
  164. var myData = [
  165. ['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
  166. ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],
  167. ['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
  168. ['American Express Company',52.55,0.01,0.02,'9/1 12:00am'],
  169. ['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],
  170. ['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'],
  171. ['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am'],
  172. ['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am'],
  173. ['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am'],
  174. ['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am'],
  175. ['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am'],
  176. ['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am'],
  177. ['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am'],
  178. ['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'],
  179. ['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am'],
  180. ['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am'],
  181. ['International Business Machines',81.41,0.44,0.54,'9/1 12:00am'],
  182. ['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am'],
  183. ['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am'],
  184. ['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am'],
  185. ['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am'],
  186. ['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am'],
  187. ['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am'],
  188. ['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am'],
  189. ['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am'],
  190. ['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am'],
  191. ['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am'],
  192. ['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am'],
  193. ['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am']
  194. ];
  195. // example of custom renderer function
  196. function change(val){
  197. if(val > 0){
  198. return '<span style="color:green;">' + val + '</span>';
  199. }else if(val < 0){
  200. return '<span style="color:red;">' + val + '</span>';
  201. }
  202. return val;
  203. }
  204. // example of custom renderer function
  205. function pctChange(val){
  206. if(val > 0){
  207. return '<span style="color:green;">' + val + '%</span>';
  208. }else if(val < 0){
  209. return '<span style="color:red;">' + val + '%</span>';
  210. }
  211. return val;
  212. }
  213. // create the data store
  214. var store = new Ext.data.ArrayStore({
  215. fields: [
  216. {name: 'company'},
  217. {name: 'price', type: 'float'},
  218. {name: 'change', type: 'float'},
  219. {name: 'pctChange', type: 'float'},
  220. {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
  221. ]
  222. });
  223. store.loadData(myData);
  224. var helpWindow = new Ext.Window({
  225. title: 'Source code',
  226. width: 920,
  227. height: 500,
  228. closeAction: 'hide',
  229. bodyCfg: {tag: 'textarea', readonly: true},
  230. bodyStyle: {
  231. backgroundColor: 'white',
  232. margin: '0px',
  233. border: '0px none'
  234. },
  235. listeners: {
  236. render: function(w) {
  237. Ext.Ajax.request({
  238. url: 'field-to-grid-dd.js',
  239. success: function(r) {
  240. w.body.dom.value = r.responseText;
  241. }
  242. });
  243. }
  244. }
  245. });
  246. // create the Grid
  247. var grid = new Ext.grid.GridPanel({
  248. store: store,
  249. columns: [
  250. {id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company'},
  251. {header: "Price", width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
  252. {header: "Change", width: 75, sortable: true, renderer: change, dataIndex: 'change'},
  253. {header: "% Change", width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},
  254. {header: "Last Updated", width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
  255. ],
  256. plugins: new Ext.ux.CellFieldDropZone(),
  257. stripeRows: true,
  258. autoExpandColumn: 'company',
  259. height:350,
  260. width:600,
  261. title:'Array Grid',
  262. bbar: new Ext.PagingToolbar({
  263. buttons: [{
  264. text: 'View Source',
  265. handler: function() {
  266. helpWindow.show();
  267. }
  268. }],
  269. store: store,
  270. pageSize: 25
  271. })
  272. });
  273. grid.render('grid-example');
  274. grid.getView().on('viewready', function() {
  275. grid.getSelectionModel().selectFirstRow();
  276. });
  277. var f = new Ext.Panel({
  278. frame: true,
  279. layout: 'form',
  280. width: 600,
  281. plugins: new Ext.ux.PanelFieldDragZone(),
  282. style: {
  283. 'margin-top': '10px'
  284. },
  285. labelWidth: 150,
  286. items: [{
  287. xtype: 'textfield',
  288. fieldLabel: 'Drag this text',
  289. value: 'test'
  290. },{
  291. xtype: 'numberfield',
  292. fieldLabel: 'Drag this number',
  293. value: '1.2'
  294. },{
  295. xtype: 'datefield',
  296. fieldLabel: 'Drag this date',
  297. value: new Date()
  298. }],
  299. renderTo: Ext.getBody()
  300. });
  301. });