slickgrid added to third-party
[myslice.git] / third-party / slickgrid-2.1 / slick.groupitemmetadataprovider.js
1 (function ($) {
2   $.extend(true, window, {
3     Slick: {
4       Data: {
5         GroupItemMetadataProvider: GroupItemMetadataProvider
6       }
7     }
8   });
9
10
11   /***
12    * Provides item metadata for group (Slick.Group) and totals (Slick.Totals) rows produced by the DataView.
13    * This metadata overrides the default behavior and formatting of those rows so that they appear and function
14    * correctly when processed by the grid.
15    *
16    * This class also acts as a grid plugin providing event handlers to expand & collapse groups.
17    * If "grid.registerPlugin(...)" is not called, expand & collapse will not work.
18    *
19    * @class GroupItemMetadataProvider
20    * @module Data
21    * @namespace Slick.Data
22    * @constructor
23    * @param options
24    */
25   function GroupItemMetadataProvider(options) {
26     var _grid;
27     var _defaults = {
28       groupCssClass: "slick-group",
29       groupTitleCssClass: "slick-group-title",
30       totalsCssClass: "slick-group-totals",
31       groupFocusable: true,
32       totalsFocusable: false,
33       toggleCssClass: "slick-group-toggle",
34       toggleExpandedCssClass: "expanded",
35       toggleCollapsedCssClass: "collapsed",
36       enableExpandCollapse: true
37     };
38
39     options = $.extend(true, {}, _defaults, options);
40
41
42     function defaultGroupCellFormatter(row, cell, value, columnDef, item) {
43       if (!options.enableExpandCollapse) {
44         return item.title;
45       }
46
47       var indentation = item.level * 15 + "px";
48
49       return "<span class='" + options.toggleCssClass + " " +
50           (item.collapsed ? options.toggleCollapsedCssClass : options.toggleExpandedCssClass) +
51           "' style='margin-left:" + indentation +"'>" +
52           "</span>" +
53           "<span class='" + options.groupTitleCssClass + "' level='" + item.level + "'>" +
54             item.title +
55           "</span>";
56     }
57
58     function defaultTotalsCellFormatter(row, cell, value, columnDef, item) {
59       return (columnDef.groupTotalsFormatter && columnDef.groupTotalsFormatter(item, columnDef)) || "";
60     }
61
62
63     function init(grid) {
64       _grid = grid;
65       _grid.onClick.subscribe(handleGridClick);
66       _grid.onKeyDown.subscribe(handleGridKeyDown);
67
68     }
69
70     function destroy() {
71       if (_grid) {
72         _grid.onClick.unsubscribe(handleGridClick);
73         _grid.onKeyDown.unsubscribe(handleGridKeyDown);
74       }
75     }
76
77     function handleGridClick(e, args) {
78       var item = this.getDataItem(args.row);
79       if (item && item instanceof Slick.Group && $(e.target).hasClass(options.toggleCssClass)) {
80         if (item.collapsed) {
81           this.getData().expandGroup(item.groupingKey);
82         } else {
83           this.getData().collapseGroup(item.groupingKey);
84         }
85
86         e.stopImmediatePropagation();
87         e.preventDefault();
88       }
89     }
90
91     // TODO:  add -/+ handling
92     function handleGridKeyDown(e, args) {
93       if (options.enableExpandCollapse && (e.which == $.ui.keyCode.SPACE)) {
94         var activeCell = this.getActiveCell();
95         if (activeCell) {
96           var item = this.getDataItem(activeCell.row);
97           if (item && item instanceof Slick.Group) {
98             if (item.collapsed) {
99               this.getData().expandGroup(item.groupingKey);
100             } else {
101               this.getData().collapseGroup(item.groupingKey);
102             }
103
104             e.stopImmediatePropagation();
105             e.preventDefault();
106           }
107         }
108       }
109     }
110
111     function getGroupRowMetadata(item) {
112       return {
113         selectable: false,
114         focusable: options.groupFocusable,
115         cssClasses: options.groupCssClass,
116         columns: {
117           0: {
118             colspan: "*",
119             formatter: defaultGroupCellFormatter,
120             editor: null
121           }
122         }
123       };
124     }
125
126     function getTotalsRowMetadata(item) {
127       return {
128         selectable: false,
129         focusable: options.totalsFocusable,
130         cssClasses: options.totalsCssClass,
131         formatter: defaultTotalsCellFormatter,
132         editor: null
133       };
134     }
135
136
137     return {
138       "init": init,
139       "destroy": destroy,
140       "getGroupRowMetadata": getGroupRowMetadata,
141       "getTotalsRowMetadata": getTotalsRowMetadata
142     };
143   }
144 })(jQuery);