summaryrefslogtreecommitdiffstats
path: root/lib/extensions/jquery.jtable.record-actions.js
blob: cc9da7f43e0035fab8f2516f8991a87d62350003 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
/************************************************************************
* RECORD-ACTIONS extension for jTable                                          *
*************************************************************************/
(function ($) {

    //Reference to base object members
    var base = {
        _initializeFields: $.hik.jtable.prototype._initializeFields,
        _onRecordsLoaded: $.hik.jtable.prototype._onRecordsLoaded
    };

    //extension members
    $.extend(true, $.hik.jtable.prototype, {

        /************************************************************************
        * OVERRIDED METHODS                                                     *
        *************************************************************************/

        /* Overrides base method to create record-actions field type.
        *************************************************************************/
        _initializeFields: function () {
            base._initializeFields.apply(this, arguments);
            
            var self = this;

            self._extraFieldTypes.push({
                type:'record-actions',
                creator: function(record, field){
                    return self._createRecordActionsDropdown(record, field);
                }
            });
        },

        /* Overrides base method to handle dropdown menu overflow.
        *************************************************************************/
        _onRecordsLoaded: function () {
            base._onRecordsLoaded.apply(this, arguments);

            var self = this;
            self._$tableBody.find('div.dropdown').on('show.bs.dropdown', function (e) {
                    var $this = $(this);

                    if (!$this.data('_tether')) {
                        var $dropdownButton = $this.find('.dropdown-toggle');
                        var $dropdownMenu = $this.find('.dropdown-menu');

                        $dropdownMenu.css({
                            'display': 'block'
                        });

                        $this.data('_tether', new Tether({
                            element: $dropdownMenu[0],
                            target: $dropdownButton[0],
                            attachment: 'top left',
                            targetAttachment: 'bottom left',
                            constraints: [{
                                to: 'window',
                                attachment: 'together',
                                pin: true
                            }]
                        }));
                    }

                    var $dropdownMenu = $($this.data('_tether').element);
                    $dropdownMenu.css({
                        'display': 'block'
                    });
                }).on('hidden.bs.dropdown', function (e) {
                    var $this = $(this);
                    var $dropdownMenu = $($this.data('_tether').element);
                    $dropdownMenu.css({
                        'display': 'none'
                    });
                });
        },
        
        /************************************************************************
        * PRIVATE METHODS                                                       *
        *************************************************************************/

        /* Builds the dropdown actions button according to field definition
        *************************************************************************/
        _createRecordActionsDropdown: function(record, field){
            var self = this;
            var $dropdownContainer = $('<div></div>')
                                        .addClass('btn-group')
                                        .addClass('dropdown');
    
            var $dropdownButton = $('<button></button>')
                                        .html(field.text)
                                        .addClass('dropdown-toggle')
                                        .attr('data-toggle','dropdown')
                                        .attr('aria-haspopup','true')
                                        .attr('aria-expanded','true');
                                        
            if(field.cssClass){
                $dropdownButton.addClass(field.cssClass);
            }

            var $dropdownItemsContainer = $('<ul></ul>').addClass('dropdown-menu');
            for (var i = 0; i < field.items.length; i++) {
                var fieldItem = field.items[i];
                
                if(fieldItem.visibility && !fieldItem.visibility(record)){
                    continue;    
                }

                var $dropdownItem = self._createDropdownItem(record, field.items[i]);
                $dropdownItem.appendTo($dropdownItemsContainer);
            }

            if($dropdownItemsContainer.find('li').length > 0){
                $dropdownItemsContainer.appendTo($dropdownContainer);
                $dropdownButton.appendTo($dropdownContainer);
            }
            
            return $dropdownContainer;
        },

        _createDropdownItem: function(record, item){
            var $li = $('<li></li>');
            var $a = $('<a></a>');

            if(item.text){
                $a.html(item.text);
            }

            if(item.action){
                $a.click(function(){
                    item.action(record, $li.closest('tr'));
                });
            }

            $a.appendTo($li);
            return $li;
        }

    });

})(jQuery);