Special XML conversion code for Firefox >= 20.0
[infodrom/rico3] / minsrc / ricoLiveGridMenu.js
1 /*
2  *  (c) 2005-2011 Richard Cowin (http://openrico.org)
3  *  (c) 2005-2011 Matt Brown (http://dowdybrown.com)
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  *  file except in compliance with the License. You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software distributed under the
11  *  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12  *  either express or implied. See the License for the specific language governing permissions
13  *  and limitations under the License.
14  */
15
16 /**
17  * Standard menu for LiveGrid
18  */
19 Rico.GridMenu = function(options) {
20   this.initialize(options);
21 };
22
23 Rico.GridMenu.prototype = {
24
25 initialize: function(options) {
26   this.options = {
27     width           : '18em',
28     dataMenuHandler : null          // put custom items on the menu
29   };
30   Rico.extend(this.options, options || {});
31   Rico.extend(this, new Rico.Menu(this.options));
32   this.sortmenu = new Rico.Menu({ width: '15em' });
33   this.filtermenu = new Rico.Menu({ width: '22em' });
34   this.exportmenu = new Rico.Menu({ width: '24em' });
35   this.hideshowmenu = new Rico.Menu({ width: '22em' });
36   this.createDiv();
37   this.sortmenu.createDiv();
38   this.filtermenu.createDiv();
39   this.exportmenu.createDiv();
40   this.hideshowmenu.createDiv();
41 },
42
43 // Build context menu for grid
44 buildGridMenu: function(r,c) {
45   this.clearMenu();
46   var livegrid=this.liveGrid;
47   var buffer=livegrid.buffer;
48   var totrows=buffer.totalRows;
49   var maxprint=livegrid.options.maxPrint;
50   var onBlankRow=(r >= totrows);
51   var column=livegrid.columns[c];
52   if (this.options.dataMenuHandler) {
53      var showDefaultMenu=this.options.dataMenuHandler(livegrid,r,c,onBlankRow);
54      if (!showDefaultMenu) return (this.itemCount > 0);
55   }
56
57   // menu items for sorting
58   if (column.sortable && totrows>0) {
59     this.sortmenu.clearMenu();
60     this.addSubMenuItem(Rico.getPhraseById("gridmenuSortBy",column.displayName), this.sortmenu, false);
61     this.sortmenu.addMenuItemId("gridmenuSortAsc", function() { column.sortAsc(); }, true);
62     this.sortmenu.addMenuItemId("gridmenuSortDesc", function() { column.sortDesc(); }, true);
63   }
64
65   // menu items for filtering
66   this.filtermenu.clearMenu();
67   if (column.canFilter()) {
68     this.addSubMenuItem(Rico.getPhraseById("gridmenuFilterBy",column.displayName), this.filtermenu, false);
69     if (!column.format.filterUI && (!onBlankRow || column.filterType == Rico.ColumnConst.USERFILTER)) {
70       column.userFilter=column.getValue(r);
71       if (column.filterType == Rico.ColumnConst.USERFILTER) {
72         this.filtermenu.addMenuItemId("gridmenuRemoveFilter", function() { column.setUnfiltered(false); }, true);
73         if (column.filterOp=='LIKE')
74           this.filtermenu.addMenuItemId("gridmenuChgKeyword", function() { livegrid.openKeyword(c); }, true);
75         if (column.filterOp=='NE' && !onBlankRow)
76           this.filtermenu.addMenuItemId("gridmenuExcludeAlso", function() { column.addFilterNE(); }, true);
77       } else if (!onBlankRow) {
78         this.filtermenu.addMenuItemId("gridmenuInclude", function() { column.setFilterEQ(); }, true);
79         this.filtermenu.addMenuItemId("gridmenuGreaterThan", function() { column.setFilterGE(); }, column.userFilter!='');
80         this.filtermenu.addMenuItemId("gridmenuLessThan", function() { column.setFilterLE(); }, column.userFilter!='');
81         if (column.isText)
82           this.filtermenu.addMenuItemId("gridmenuContains", function() { livegrid.openKeyword(c); }, true);
83         this.filtermenu.addMenuItemId("gridmenuExclude", function() { column.setFilterNE(); }, true);
84       }
85     }
86     if (livegrid.filterCount() > 0) {
87       this.filtermenu.addMenuItemId("gridmenuRemoveAll", function() { livegrid.clearFilters(); }, true);
88     }
89     if (buffer.options.canRefresh) this.filtermenu.addMenuItemId("gridmenuRefresh", function() { livegrid.filterHandler(); }, true);
90   }
91
92   // menu items for Print/Export
93   this.exportmenu.clearMenu();
94   if (maxprint > 0) {
95     this.addSubMenuItem(Rico.getPhraseById('gridmenuExport'),this.exportmenu,false);
96     if (buffer.printVisibleSQL && typeof(buffer.dataSource)=='string') {
97       // SQL buffer
98       this.exportmenu.addMenuItemId("gridmenuExportVis2Web", function() { buffer.printVisibleSQL('html'); });
99       this.exportmenu.addMenuItemId("gridmenuExportAll2Web", function() { buffer.printAllSQL('html'); }, buffer.totalRows <= maxprint);
100       this.exportmenu.addMenuBreak();
101       this.exportmenu.addMenuItemId("gridmenuExportVis2SS", function() { buffer.printVisibleSQL('xl'); });
102       this.exportmenu.addMenuItemId("gridmenuExportAll2SS", function() { buffer.printAllSQL('xl'); }, buffer.totalRows <= maxprint);
103     } else {
104       // any other buffer
105       this.exportmenu.addMenuItemId("gridmenuExportVis2Web", function() { buffer.printVisible(); });
106       this.exportmenu.addMenuItemId("gridmenuExportAll2Web", function() { buffer.printAll(); }, buffer.totalRows <= maxprint);
107     }
108   }
109
110   // menu items for hide/unhide
111   var hiddenCols=livegrid.listInvisible();
112   for (var showableCnt=0,x=0; x<hiddenCols.length; x++) {
113     if (hiddenCols[x].canHideShow()) showableCnt++;
114   }
115   if (showableCnt > 0 || column.canHideShow()) {
116     this.hideshowmenu.clearMenu();
117     this.addSubMenuItem(Rico.getPhraseById('gridmenuHideShow'),this.hideshowmenu,false);
118     this.hideshowmenu.addMenuItemId('gridmenuChooseCols', function() { livegrid.chooseColumns(); },true,false);
119     var visibleCnt=livegrid.columns.length-hiddenCols.length;
120     var enabled=(visibleCnt>1 && column.visible && column.canHideShow());
121     this.hideshowmenu.addMenuItem(Rico.getPhraseById('gridmenuHide',column.displayName), function() { column.hideColumn(); }, enabled);
122     if (hiddenCols.length > 1)
123       this.hideshowmenu.addMenuItemId('gridmenuShowAll', function() { livegrid.showAll(); });
124   }
125   return true;
126 }
127
128 }