Import plugins/php/dbClass2.php from OpenRico 2.1
[misc/kostenrechnung] / lib / rico / ricoLiveGridMenu.js
1 if(typeof Rico=='undefined')
2   throw("GridMenu requires the Rico JavaScript framework");
3
4
5 /**
6  * Standard menu for LiveGrid
7  */
8 Rico.GridMenu = Class.create();
9
10 Rico.GridMenu.prototype = {
11
12 initialize: function(options) {
13   this.options = {
14     width           : '18em',
15     dataMenuHandler : null          // put custom items on the menu
16   };
17   Object.extend(this.options, options || {});
18   Object.extend(this, new Rico.Menu(this.options));
19   this.sortmenu = new Rico.Menu({ width: '15em' });
20   this.filtermenu = new Rico.Menu({ width: '22em' });
21   this.exportmenu = new Rico.Menu({ width: '24em' });
22   this.hideshowmenu = new Rico.Menu({ width: '22em' });
23   this.createDiv();
24   this.sortmenu.createDiv();
25   this.filtermenu.createDiv();
26   this.exportmenu.createDiv();
27   this.hideshowmenu.createDiv();
28 },
29
30 // Build context menu for grid
31 buildGridMenu: function(r,c) {
32   this.clearMenu();
33   var totrows=this.liveGrid.buffer.totalRows;
34   var onBlankRow=r >= totrows;
35   var column=this.liveGrid.columns[c];
36   if (this.options.dataMenuHandler) {
37      var showDefaultMenu=this.options.dataMenuHandler(this.liveGrid,r,c,onBlankRow);
38      if (!showDefaultMenu) return (this.itemCount > 0);
39   }
40
41   // menu items for sorting
42   if (column.sortable && totrows>0) {
43     this.sortmenu.clearMenu();
44     this.addSubMenuItem(RicoTranslate.getPhraseById("gridmenuSortBy",column.displayName), this.sortmenu, false);
45     this.sortmenu.addMenuItemId("gridmenuSortAsc", column.sortAsc.bind(column), true);
46     this.sortmenu.addMenuItemId("gridmenuSortDesc", column.sortDesc.bind(column), true);
47   }
48
49   // menu items for filtering
50   this.filtermenu.clearMenu();
51   if (column.canFilter() && !column.format.filterUI && (!onBlankRow || column.filterType == Rico.TableColumn.USERFILTER)) {
52     this.addSubMenuItem(RicoTranslate.getPhraseById("gridmenuFilterBy",column.displayName), this.filtermenu, false);
53     column.userFilter=column.getValue(r);
54     if (column.filterType == Rico.TableColumn.USERFILTER) {
55       this.filtermenu.addMenuItemId("gridmenuRemoveFilter", column.setUnfiltered.bind(column,false), true);
56       if (column.filterOp=='LIKE')
57         this.filtermenu.addMenuItemId("gridmenuChgKeyword", this.liveGrid.openKeyword.bind(this.liveGrid,c), true);
58       if (column.filterOp=='NE' && !onBlankRow)
59         this.filtermenu.addMenuItemId("gridmenuExcludeAlso", column.addFilterNE.bind(column), true);
60     } else if (!onBlankRow) {
61       this.filtermenu.addMenuItemId("gridmenuInclude", column.setFilterEQ.bind(column), true);
62       this.filtermenu.addMenuItemId("gridmenuGreaterThan", column.setFilterGE.bind(column), column.userFilter!='');
63       this.filtermenu.addMenuItemId("gridmenuLessThan", column.setFilterLE.bind(column), column.userFilter!='');
64       if (column.isText)
65         this.filtermenu.addMenuItemId("gridmenuContains", this.liveGrid.openKeyword.bind(this.liveGrid,c), true);
66       this.filtermenu.addMenuItemId("gridmenuExclude", column.setFilterNE.bind(column), true);
67     }
68     if (this.liveGrid.filterCount() > 0) {
69       this.filtermenu.addMenuItemId("gridmenuRefresh", this.liveGrid.filterHandler.bind(this.liveGrid), true);
70       this.filtermenu.addMenuItemId("gridmenuRemoveAll", this.liveGrid.clearFilters.bind(this.liveGrid), true);
71     }
72   } else if (this.liveGrid.filterCount() > 0) {
73     this.addSubMenuItem(RicoTranslate.getPhraseById("gridmenuFilterBy",column.displayName), this.filtermenu, false);
74     this.filtermenu.addMenuItemId("gridmenuRemoveAll", this.liveGrid.clearFilters.bind(this.liveGrid), true);
75   }
76
77   // menu items for Print/Export
78   if (this.liveGrid.options.maxPrint > 0 && totrows>0) {
79     this.exportmenu.clearMenu();
80     this.addSubMenuItem(RicoTranslate.getPhraseById('gridmenuExport'),this.exportmenu,false);
81     this.exportmenu.addMenuItemId("gridmenuExportVis2Web", this.liveGrid.printVisible.bind(this.liveGrid,'plain'));
82     this.exportmenu.addMenuItemId("gridmenuExportAll2Web", this.liveGrid.printAll.bind(this.liveGrid,'plain'), this.liveGrid.buffer.totalRows <= this.liveGrid.options.maxPrint);
83     if (Prototype.Browser.IE) {
84       this.exportmenu.addMenuBreak();
85       this.exportmenu.addMenuItemId("gridmenuExportVis2SS", this.liveGrid.printVisible.bind(this.liveGrid,'owc'));
86       this.exportmenu.addMenuItemId("gridmenuExportAll2SS", this.liveGrid.printAll.bind(this.liveGrid,'owc'), this.liveGrid.buffer.totalRows <= this.liveGrid.options.maxPrint);
87     }
88   }
89
90   // menu items for hide/unhide
91   var hiddenCols=this.liveGrid.listInvisible();
92   for (var showableCnt=0,x=0; x<hiddenCols.length; x++) {
93     if (hiddenCols[x].canHideShow()) showableCnt++;
94   }
95   if (showableCnt > 0 || column.canHideShow()) {
96     this.hideshowmenu.clearMenu();
97     this.addSubMenuItem(RicoTranslate.getPhraseById('gridmenuHideShow'),this.hideshowmenu,false);
98     this.hideshowmenu.addMenuItemId('gridmenuChooseCols', this.liveGrid.chooseColumns.bindAsEventListener(this.liveGrid));
99     var visibleCnt=this.liveGrid.columns.length-hiddenCols.length;
100     var enabled=(visibleCnt>1 && column.visible && column.canHideShow());
101     this.hideshowmenu.addMenuItem(RicoTranslate.getPhraseById('gridmenuHide',column.displayName), column.hideColumn.bind(column), enabled, false);
102     for (var cnt=0,x=0; x<hiddenCols.length; x++) {
103       if (hiddenCols[x].canHideShow()) {
104         if (cnt++==0) this.hideshowmenu.addMenuBreak();
105         this.hideshowmenu.addMenuItem(RicoTranslate.getPhraseById('gridmenuShow',hiddenCols[x].displayName), hiddenCols[x].showColumn.bind(hiddenCols[x]), true, false);
106       }
107     }
108     if (hiddenCols.length > 1)
109       this.hideshowmenu.addMenuItemId('gridmenuShowAll', this.liveGrid.showAll.bind(this.liveGrid));
110   }
111   return true;
112 }
113
114 }
115
116 Rico.includeLoaded('ricoLiveGridMenu.js');