1 Ext.Loader.setConfig({enabled: true});
3 Ext.Loader.setPath('Ext.ux', '../ux/');
10 'Ext.ux.ToolbarDroppable',
14 Ext.onReady(function() {
15 //The following functions are used to get the sorting data from the toolbar and apply it to the store
17 * Tells the store to sort itself according to our sort data
20 store.sort(getSorters());
24 * Callback handler used when a sorter button is clicked or reordered
25 * @param {Ext.Button} button The button that was clicked
26 * @param {Boolean} changeDirection True to change direction (default). Set to false for reorder
27 * operations as we wish to preserve ordering there
29 function changeSortDirection(button, changeDirection) {
30 var sortData = button.sortData,
31 iconCls = button.iconCls;
34 if (changeDirection !== false) {
35 button.sortData.direction = Ext.String.toggle(button.sortData.direction, "ASC", "DESC");
36 button.setIconCls(Ext.String.toggle(iconCls, "sort-asc", "sort-desc"));
44 * Returns an array of sortData from the sorter buttons
45 * @return {Array} Ordered sort data from each of the sorter buttons
47 function getSorters() {
50 Ext.each(tbar.query('button'), function(button) {
51 sorters.push(button.sortData);
58 * Convenience function for creating Toolbar Buttons that are tied to sorters
59 * @param {Object} config Optional config object
60 * @return {Object} The new Button configuration
62 function createSorterButtonConfig(config) {
63 config = config || {};
66 click: function(button, e) {
67 changeSortDirection(button, true);
70 iconCls: 'sort-' + config.sortData.direction.toLowerCase(),
78 * Returns an array of fake data
79 * @param {Number} count The number of fake rows to create data for
80 * @return {Array} The fake record data, suitable for usage with an ArrayReader
82 function createFakeData(count) {
83 var firstNames = ['Ed', 'Tommy', 'Aaron', 'Abe', 'Jamie', 'Adam', 'Dave', 'David', 'Jay', 'Nicolas', 'Nige'],
84 lastNames = ['Spencer', 'Maintz', 'Conran', 'Elias', 'Avins', 'Mishcon', 'Kaneda', 'Davis', 'Robinson', 'Ferrero', 'White'],
85 ratings = [1, 2, 3, 4, 5],
86 salaries = [100, 400, 900, 1500, 1000000];
89 for (var i = 0; i < (count || 25); i++) {
90 var ratingId = Math.floor(Math.random() * ratings.length),
91 salaryId = Math.floor(Math.random() * salaries.length),
92 firstNameId = Math.floor(Math.random() * firstNames.length),
93 lastNameId = Math.floor(Math.random() * lastNames.length),
95 rating = ratings[ratingId],
96 salary = salaries[salaryId],
97 name = Ext.String.format("{0} {1}", firstNames[firstNameId], lastNames[lastNameId]);
99 data.push([rating, salary, name]);
104 // create the data store
105 Ext.define('Employee', {
106 extend: 'Ext.data.Model',
108 {name: 'rating', type: 'int'},
109 {name: 'salary', type: 'float'},
114 var store = Ext.create('Ext.data.Store', {
118 data: createFakeData(25),
126 var reorderer = Ext.create('Ext.ux.BoxReorderer', {
129 Drop: function(r, c, button) { //update sort direction when button is dropped
130 changeSortDirection(button, false);
135 var droppable = Ext.create('Ext.ux.ToolbarDroppable', {
137 * Creates the new toolbar item from the drop event
139 createItem: function(data) {
140 var header = data.header,
141 headerCt = header.ownerCt,
142 reorderer = headerCt.reorderer;
144 // Hide the drop indicators of the standard HeaderDropZone
145 // in case user had a pending valid drop in
147 reorderer.dropZone.invalidateDrop();
150 return createSorterButtonConfig({
153 property: header.dataIndex,
160 * Custom canDrop implementation which returns true if a column can be added to the toolbar
161 * @param {Object} data Arbitrary data from the drag source. For a HeaderContainer, it will
162 * contain a header property which is the Header being dragged.
163 * @return {Boolean} True if the drop is allowed
165 canDrop: function(dragSource, event, data) {
166 var sorters = getSorters(),
167 header = data.header,
168 length = sorters.length,
169 entryIndex = this.calculateEntryIndex(event),
170 targetItem = this.toolbar.getComponent(entryIndex),
173 // Group columns have no dataIndex and therefore cannot be sorted
174 // If target isn't reorderable it could not be replaced
175 if (!header.dataIndex || (targetItem && targetItem.reorderable === false)) {
179 for (i = 0; i < length; i++) {
180 if (sorters[i].property == header.dataIndex) {
190 //create the toolbar with the 2 plugins
191 var tbar = Ext.create('Ext.toolbar.Toolbar', {
194 text: 'Sorting order:',
197 plugins: [reorderer, droppable]
200 tbar.add(createSorterButtonConfig({
208 tbar.add(createSorterButtonConfig({
217 var grid = Ext.create('Ext.grid.Panel', {
237 renderer: Ext.util.Format.usMoney
243 title : 'Array Grid',
244 renderTo: 'grid-example',
247 //here we tell the toolbar's droppable plugin that it can accept items from the columns' dragdrop group
248 afterrender: function(grid) {
249 var headerCt = grid.child("headercontainer"),
250 //the plugin position depends on browser see Ext.grid.header.Container sources
251 dragProxy = headerCt.plugins[0].dragZone || headerCt.plugins[1].dragZone;
253 droppable.addDDGroup(dragProxy.ddGroup);