Module: Netzke::GridPanelJs::ClassMethods

Defined in:
lib/netzke/grid_panel_js.rb

Instance Method Summary collapse

Instance Method Details

#js_base_classObject



17
18
19
# File 'lib/netzke/grid_panel_js.rb', line 17

def js_base_class 
  'Ext.grid.EditorGridPanel'
end

#js_extend_propertiesObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/netzke/grid_panel_js.rb', line 246

def js_extend_properties
  res = super
  
  # Defaults
  res.merge!(
  {
    :track_mouse_over => true,
    :load_mask        => true,
    :auto_scroll      => true,

    :default_column_config => config_columns.inject({}){ |r, c| c.is_a?(Hash) ? r.merge(c[:name] => c[:default]) : r },
    
    :init_component => js_init_component.l,
    
    :load_exception_handler => <<-END_OF_JAVASCRIPT.l,
  
    :update => <<-END_OF_JAVASCRIPT.l,
  
    :load_store_data => <<-END_OF_JAVASCRIPT.l,
  
    :add => <<-END_OF_JAVASCRIPT.l,
  
    :edit => <<-END_OF_JAVASCRIPT.l,
      function(){
        var row = this.getSelectionModel().getSelected();
        if (row){
          this.tryStartEditing(this.store.indexOf(row));
        }
      }
    END_OF_JAVASCRIPT
  
    # try editing the first editable (i.e. not hidden, not read-only) sell
    :try_start_editing => <<-END_OF_JAVASCRIPT.l,

    :del => <<-END_OF_JAVASCRIPT.l,
      function() {
        if (this.getSelectionModel().hasSelection()){
          Ext.Msg.confirm('Confirm', 'Are you sure?', function(btn){
            if (btn == 'yes') {
              var records = [];
              this.getSelectionModel().each(function(r){
                if (r.isNew) {
                  // this record is not know to server - simply remove from store
                  this.store.remove(r);
                } else {
                  records.push(r.get('id'));
                }
              }, this);

              if (records.length > 0){
                // call API
                this.deleteData({records: Ext.encode(records)});
              }
            }
          }, this);
        }
      }
    END_OF_JAVASCRIPT

    # Called by the server side to update newly created records
    :update_new_records => <<-END_OF_JAVASCRIPT.l,
      function(records){
        this.updateRecords(records);
      }
    END_OF_JAVASCRIPT
    
    # Called by the server side to update modified records
    :update_mod_records => <<-END_OF_JAVASCRIPT.l,
      function(records){
        this.updateRecords(records, true);
      }
    END_OF_JAVASCRIPT
    
    # Updates modified or newly created records
    # Example of the records argument:
    #   {1098 => [1, 'value1', 'value2'], 1099 => [2, 'value1', 'value2']}
    :update_records => <<-END_OF_JAVASCRIPT.l,
    
    :default_get_row_class => <<-END_OF_JAVASCRIPT.l,
    
    :apply => <<-END_OF_JAVASCRIPT.l,
 
    :select_first_row => <<-END_OF_JAVASCRIPT.l,
    
    :refresh => <<-END_OF_JAVASCRIPT.l,
    
    :on_column_resize => <<-END_OF_JAVASCRIPT.l,
    
    :on_column_hidden_change => <<-END_OF_JAVASCRIPT.l,
      function(cm, index, hidden){
        this.hideColumn({
          index:index,
          hidden:hidden
        });
      }
    END_OF_JAVASCRIPT
    
    # :reorder_columns => <<-END_OF_JAVASCRIPT.l,
    #   function(columns){
    #     columnsInNewShipment = [];
    #     Ext.each(columns, function(c){
    #       columnsInNewShipment.push({name:c});
    #     });
    #     newRecordType = Ext.data.Record.create(columnsInNewShipment);
    #     this.store.reader.recordType = newRecordType; // yes, recordType is a protected property, but that's the only way we can do it, and it seems to work for now
    #   }
    # END_OF_JAVASCRIPT
    
    :on_column_move => <<-END_OF_JAVASCRIPT.l,
    
    :on_row_context_menu => <<-END_OF_JAVASCRIPT.l,
    
    :on_after_row_move => <<-END_OF_JAVASCRIPT.l,
      function(dt, oldIndex, newIndex, records){
    		var ids = [];
    		// collect records ids
    		Ext.each(records, function(r){ids.push(r.get('id'))});
    		// call GridPanel's API
    		this.moveRows({ids:Ext.encode(ids), new_index: newIndex});
      }
    END_OF_JAVASCRIPT
  }
  )
  
  # Edit in form
  res.merge!(
  {
    :on_successfull_record_creation => <<-END_OF_JAVASCRIPT.l,

    :on_successfull_edit => <<-END_OF_JAVASCRIPT.l,

    :edit_in_form => <<-END_OF_JAVASCRIPT.l,

    :add_in_form => <<-END_OF_JAVASCRIPT.l,
      function(){
        if (!this.formWindow) {
          this.formWindow = new Ext.Window({
            title:'Add',
            layout: 'fit',
            modal: true,
            width: 400,
            height: Ext.lib.Dom.getViewHeight() *0.9,
            closeAction: 'hide',
            buttons:[{
              text: 'OK',
              handler: function(){
                this.ownerCt.getWidget().apply();
              }
            },{
              text:'Cancel',
              handler:function(){
                this.ownerCt.close();
              }
            }]
          });
        }

        this.formWindow.show(null, function(){
          this.formWindow.closeRes = 'cancel';
          if (!this.formWindow.getWidget()){
            this.loadAggregatee({id:"newRecordForm", container:this.formWindow.id});
          }
        }, this);

      }
    END_OF_JAVASCRIPT
    
  }
  ) if config[:edit_in_form_available]
  
  # Extended search
  res.merge!(
  {
    :search => <<-END_OF_JAVASCRIPT.l,
      function(){
        if (!this.searchWindow){
          this.searchWindow = new Ext.Window({
            title:'Advanced search',
            layout:'fit',
            modal: true,
            width: 400,
            height: Ext.lib.Dom.getViewHeight() *0.9,
            closeAction:'hide',
            buttons:[{
              text: 'OK',
              handler: function(){
                this.ownerCt.closePositively();
              }
            },{
              text:'Cancel',
              handler:function(){
                this.ownerCt.closeNegatively();
              }
            }],
            closePositively : function(){
              this.conditions = this.getWidget().getForm().getValues();
              this.closeRes = 'OK'; 
              this.hide();
            },
            closeNegatively: function(){
              this.closeRes = 'cancel'; 
              this.hide();
            }

          });

          this.searchWindow.on('hide', function(){
            if (this.searchWindow.closeRes == 'OK'){
              this.getStore().baseParams = {extra_conditions: Ext.encode(this.searchWindow.conditions)};
              this.getStore().reload();
            }
          }, this);

          this.searchWindow.on('add', function(container, searchPanel){
            searchPanel.on('apply', function(widget){
              this.searchWindow.closePositively();
              return false; // stop the event
            }, this);
          }, this);
        }

        this.searchWindow.show(null, function(){
          this.searchWindow.closeRes = 'cancel';
          if (!this.searchWindow.getWidget()){
            this.loadAggregatee({id:"searchPanel", container:this.searchWindow.id});
          }
        }, this);

      }
    END_OF_JAVASCRIPT
    
  }
  ) if config[:extended_search_available]
  
  res
end

#js_init_componentObject

Ext.Component#initComponent, built up from pices (dependent on class configuration)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/netzke/grid_panel_js.rb', line 22

def js_init_component
  
  # Optional "edit in form"-related events
  edit_in_form_events = <<-END_OF_JAVASCRIPT if config[:edit_in_form_available]
    if (this.enableEditInForm) {
      this.getSelectionModel().on('selectionchange', function(selModel){
        // Disable "edit in form" button if new record is present in selection
        var disabled = !selModel.each(function(r){
          if (r.isNew) { return false; }
        });
        this.actions.editInForm.setDisabled(disabled);
      }, this);
    }
  END_OF_JAVASCRIPT
  
  # Result
  <<-END_OF_JAVASCRIPT
    function(){
      if (!this.clmns) {this.feedback('No columns defined for grid '+this.id);}

      /* Process columns - all in sake of creating the column model */
      // Normalize columns passed in the config
      var normClmns = [];
      Ext.each(this.clmns, function(c){
        if (!c.excluded) {
          // normalize columns
          if (typeof c == 'string') {
            normClmns.push({name:c});
          } else {
            normClmns.push(c);
          }
        }
      });

      delete this.clmns; // we don't need them anymore
      
      var cmConfig = []; // column model config - we'll use it later to create the ColumnModel
      this.plugins = []; // checkbox colums is a special case, being a plugin

      var filters = [];
      
      // Run through columns
      Ext.each(normClmns, function(c){
        // Apply default column config
        Ext.applyIf(c, this.defaultColumnConfig);

        // setting dataIndex separately
        c.dataIndex = c.name;

        // Automatically calculated default values
        if (!c.header) {c.header = c.name.humanize()}

        // normalize editor
        if (c.editor) {
          c.editor = Netzke.isObject(c.editor) ? c.editor : {xtype:c.editor};
        } else {
          c.editor = {xtype: 'textfield'}
        }
        
        // collect filters
        if (c.withFilters){
          filters.push({type:Ext.netzke.filterMap[c.editor.xtype], dataIndex:c.name});
        }

        if (c.editor && c.editor.xtype == 'checkbox') {
          // Special case of checkbox column
          var plugin = new Ext.grid.CheckColumn(c);
          this.plugins.push(plugin);
          cmConfig.push(plugin);
        } else {
          // "normal" column, not a plugin
          if (!c.readOnly && !this.prohibitUpdate) {
            // c.editor contains complete config of the editor
            var xtype = c.editor.xtype;
            c.editor = Ext.ComponentMgr.create(Ext.apply({
              parentId:this.id,
              name: c.name,
              selectOnFocus:true
            }, c.editor));
          } else {
            c.editor = null;
          }

          // set the renderer
          c.renderer = Ext.netzke.normalizedRenderer(c.renderer);
          
          // add to the list
          cmConfig.push(c);
        }

      }, this);

      // Finally, create the ColumnModel based on processed columns
      this.cm = new Ext.grid.ColumnModel(cmConfig);
      this.cm.on('hiddenchange', this.onColumnHiddenChange, this);

      /* ... and done with columns */
      
      // Filters
      if (this.enableColumnFilters) {
        this.plugins.push(new Ext.grid.GridFilters({filters:filters}));
      }
      
      // Create Ext.data.Record constructor specific for our particular column configuration
      this.recordConfig = [];
      Ext.each(normClmns, function(column){this.recordConfig.push({name:column.name});}, this);
      this.Row = Ext.data.Record.create(this.recordConfig);

      // Drag'n'Drop
      if (this.enableRowsReordering){
        this.ddPlugin = new Ext.ux.dd.GridDragDropRowOrder({
            scrollable: true // enable scrolling support (default is false)
        });
        this.plugins.push(this.ddPlugin);
      }

      // Explicitely create the connection to get grid's data, 
      // because we don't want the app-wide Ext.Ajax to be used,
      // as we are going to subscribe to its events
      var connection = new Ext.data.Connection({
        url:this.id+"__get_data",
        extraParams : {
          authenticity_token : Ext.authenticityToken
        },

        // inform Ext.Ajax about our events
        listeners: {
          beforerequest: function(){
            Ext.Ajax.fireEvent('beforerequest', arguments);
          },
          requestexception: function(){
            Ext.Ajax.fireEvent('requestexception', arguments);
          },
          requestcomplete: function(){
            Ext.Ajax.fireEvent('requestcomplete', arguments);
          }
        }
      });

      // besides getting data into the store, we may also get commands to execute
      connection.on('requestcomplete', function(conn, r){
        var response = Ext.decode(r.responseText);

        // delete data-related properties
        Ext.each(['data', 'total', 'success'], function(property){delete response[property];});
        this.bulkExecute(response);
      }, this);

      // HttpProxy that uses our custom connection
      var httpProxy = new Ext.data.HttpProxy(connection);

      // Data store
      this.store = new Ext.data.Store({
          proxy: this.proxy = httpProxy,
          reader: new Ext.data.ArrayReader({root: "data", totalProperty: "total", successProperty: "success", id:0}, this.Row),
          remoteSort: true,
          listeners:{'loadexception':{
            fn:this.loadExceptionHandler,
            scope:this
          }}
      });

      // Normalize bottom bar
      this.bbar = (this.enablePagination) ? new Ext.PagingToolbar({
        pageSize : this.rowsPerPage,
        items : this.bbar ? ["-"].concat(this.bbar) : [],
        store : this.store, 
        emptyMsg: 'Empty'
      }) : this.bbar;
      
      // Selection model
      this.sm = new Ext.grid.RowSelectionModel();
      
      // Now let Ext.grid.EditorGridPanel do the rest
      // Original initComponent
      Ext.netzke.cache.GridPanel.superclass.initComponent.call(this);
      
      // Set the events
      this.on('columnresize', this.onColumnResize, this);
      this.on('columnmove', this.onColumnMove, this);
      
      // Context menu
      if (this.enableContextMenu) {
        this.on('rowcontextmenu', this.onRowContextMenu, this);
      }
      
      // Load data AFTER the toolbar is bound to the store, which will provide for correct page number
      if (this.loadInlineData) {
        this.getStore().loadData(this.inlineData);

        // If rows per page specified, fake store.lastOptions as if the data was loaded
        // by PagingToolbar (for correct functionning of refresh tool and extended search)
        if (this.rowsPerPage) {
          this.getStore().lastOptions = {params:{limit:this.rowsPerPage, start:0}}; // this is how PagingToolbar does it...
        }
        
        // inlineData may also contain commands (TODO: make it DRY)
        // delete data-related properties
        Ext.each(['data', 'total', 'success'], function(property){delete this.inlineData[property];}, this);
        this.bulkExecute(this.inlineData);
      }
      
      // Process selectionchange event
      this.getSelectionModel().on('selectionchange', function(selModel){
        // enable/disable actions
        this.actions.del.setDisabled(!selModel.hasSelection() || this.prohibitDelete);
        this.actions.edit.setDisabled(selModel.getCount() != 1 || this.prohibitUpdate);
      }, this);
      
      // Drag n Drop event
      if (this.enableRowsReordering){
        this.ddPlugin.on('afterrowmove', this.onAfterRowMove, this);
      }
      
      // GridView
      this.getView().getRowClass = this.defaultGetRowClass;
      
      #{edit_in_form_events}
    }
    
  END_OF_JAVASCRIPT
  
end