slickgrid added to third-party
[myslice.git] / third-party / slickgrid-2.1 / plugins / slick.autotooltips.js
1 (function ($) {
2   // Register namespace
3   $.extend(true, window, {
4     "Slick": {
5       "AutoTooltips": AutoTooltips
6     }
7   });
8
9   /**
10    * AutoTooltips plugin to show/hide tooltips when columns are too narrow to fit content.
11    * @constructor
12    * @param {boolean} [options.enableForCells=true]        - Enable tooltip for grid cells
13    * @param {boolean} [options.enableForHeaderCells=false] - Enable tooltip for header cells
14    * @param {number}  [options.maxToolTipLength=null]      - The maximum length for a tooltip
15    */
16   function AutoTooltips(options) {
17     var _grid;
18     var _self = this;
19     var _defaults = {
20       enableForCells: true,
21       enableForHeaderCells: false,
22       maxToolTipLength: null
23     };
24     
25     /**
26      * Initialize plugin.
27      */
28     function init(grid) {
29       options = $.extend(true, {}, _defaults, options);
30       _grid = grid;
31       if (options.enableForCells) _grid.onMouseEnter.subscribe(handleMouseEnter);
32       if (options.enableForHeaderCells) _grid.onHeaderMouseEnter.subscribe(handleHeaderMouseEnter);
33     }
34     
35     /**
36      * Destroy plugin.
37      */
38     function destroy() {
39       if (options.enableForCells) _grid.onMouseEnter.unsubscribe(handleMouseEnter);
40       if (options.enableForHeaderCells) _grid.onHeaderMouseEnter.unsubscribe(handleHeaderMouseEnter);
41     }
42     
43     /**
44      * Handle mouse entering grid cell to add/remove tooltip.
45      * @param {jQuery.Event} e - The event
46      */
47     function handleMouseEnter(e) {
48       var cell = _grid.getCellFromEvent(e);
49       if (cell) {
50         var $node = $(_grid.getCellNode(cell.row, cell.cell));
51         var text;
52         if ($node.innerWidth() < $node[0].scrollWidth) {
53           text = $.trim($node.text());
54           if (options.maxToolTipLength && text.length > options.maxToolTipLength) {
55             text = text.substr(0, options.maxToolTipLength - 3) + "...";
56           }
57         } else {
58           text = "";
59         }
60         $node.attr("title", text);
61       }
62     }
63     
64     /**
65      * Handle mouse entering header cell to add/remove tooltip.
66      * @param {jQuery.Event} e     - The event
67      * @param {object} args.column - The column definition
68      */
69     function handleHeaderMouseEnter(e, args) {
70       var column = args.column,
71           $node = $(e.target).closest(".slick-header-column");
72       if (!column.toolTip) {
73         $node.attr("title", ($node.innerWidth() < $node[0].scrollWidth) ? column.name : "");
74       }
75     }
76     
77     // Public API
78     $.extend(this, {
79       "init": init,
80       "destroy": destroy
81     });
82   }
83 })(jQuery);