1/* global LANG */
2
3window.edittable = window.edittable || {};
4
5(function (edittable) {
6    'use strict';
7    /**
8     * create an iterable array of selected cells from the selection object
9     *
10     * @param {object} selection the selection object
11     *
12     * @returns {Array} an array of the rows/columns of the cells in the selection
13     */
14    edittable.cellArray = function (selection) {
15        var selectionArray = [];
16        for (var currentRow = selection.start.row; currentRow <= selection.end.row; currentRow += 1) {
17            for (var currentCol = selection.start.col; currentCol <= selection.end.col; currentCol += 1) {
18                selectionArray.push({row: currentRow, col: currentCol});
19            }
20        }
21        return selectionArray;
22    };
23
24    /**
25     * Defines our own contextMenu with custom callbacks
26     *
27     * @param {function} getData get the current data array
28     * @param {function} getMeta get the current meta array
29     * @returns {object} the context menu object
30     */
31    edittable.getEditTableContextMenu = function (getData, getMeta) {
32        return {
33            items: {
34                toggle_header: {
35                    name: LANG.plugins.edittable.toggle_header,
36                    callback: function (key, selection) {
37                        var meta = getMeta();
38                        jQuery.each(edittable.cellArray(selection), function (index, cell) {
39                            var col = cell.col;
40                            var row = cell.row;
41
42                            if (meta[row][col].tag && meta[row][col].tag === 'th') {
43                                meta[row][col].tag = 'td';
44                            } else {
45                                meta[row][col].tag = 'th';
46                            }
47                        });
48                        this.render();
49                    }
50                },
51                align_left: {
52                    name: LANG.plugins.edittable.align_left,
53                    callback: function (key, selection) {
54                        var meta = getMeta();
55                        jQuery.each(edittable.cellArray(selection), function (index, cell) {
56                            var col = cell.col;
57                            var row = cell.row;
58                            meta[row][col].align = 'left';
59                        });
60                        this.render();
61                    },
62                    disabled: function () {
63                        var meta = getMeta();
64                        var selection = this.getSelected();
65                        var row = selection[0];
66                        var col = selection[1];
67                        return (!meta[row][col].align || meta[row][col].align === 'left');
68                    }
69                },
70                align_center: {
71                    name: LANG.plugins.edittable.align_center,
72                    callback: function (key, selection) {
73                        var meta = getMeta();
74                        jQuery.each(edittable.cellArray(selection), function (index, cell) {
75                            var col = cell.col;
76                            var row = cell.row;
77                            meta[row][col].align = 'center';
78                        });
79                        this.render();
80                    },
81                    disabled: function () {
82                        var meta = getMeta();
83                        var selection = this.getSelected();
84                        var row = selection[0];
85                        var col = selection[1];
86                        return (meta[row][col].align && meta[row][col].align === 'center');
87                    }
88                },
89                align_right: {
90                    name: LANG.plugins.edittable.align_right,
91                    callback: function (key, selection) {
92                        var meta = getMeta();
93                        jQuery.each(edittable.cellArray(selection), function (index, cell) {
94                            var col = cell.col;
95                            var row = cell.row;
96                            meta[row][col].align = 'right';
97                        });
98                        this.render();
99                    },
100                    disabled: function () {
101                        var meta = getMeta();
102                        var selection = this.getSelected();
103                        var row = selection[0];
104                        var col = selection[1];
105                        return (meta[row][col].align && meta[row][col].align === 'right');
106                    }
107                },
108                hsep1: '---------',
109                row_above: {
110                    name: LANG.plugins.edittable.row_above
111                },
112                remove_row: {
113                    name: LANG.plugins.edittable.remove_row,
114                    /**
115                     * The same as the default action, but with confirmation
116                     *
117                     * @param {string} key key of the menu item
118                     * @param {object} selection the selection object
119                     *
120                     * @return {void}
121                     */
122                    callback: function (key, selection) {
123                        if (window.confirm(LANG.plugins.edittable.confirmdeleterow)) {
124                            var amount = selection.end.row - selection.start.row + 1;
125                            this.alter('remove_row', selection.start.row, amount);
126                        }
127                    },
128                    /**
129                     * do not show when this is the last row
130                     *
131                     * @return {boolean} true if the entry is to be disabled, false otherwise
132                     */
133                    disabled: function () {
134                        var rowsInTable = this.countRows();
135                        var firstSelectedRow = this.getSelected()[0];
136                        var lastSelectedRow = this.getSelected()[2]; // fix magic number with destructuring once we drop IE11
137                        var allRowsSelected = firstSelectedRow === 0 && lastSelectedRow === rowsInTable - 1;
138                        return (rowsInTable <= 1 || allRowsSelected);
139                    }
140                },
141                row_below: {
142                    name: LANG.plugins.edittable.row_below
143                },
144                hsep2: '---------',
145                col_left: {
146                    name: LANG.plugins.edittable.col_left
147                },
148                remove_col: {
149                    name: LANG.plugins.edittable.remove_col,
150                    /**
151                     * The same as the default action, but with confirmation
152                     *
153                     * @param {string} key key of the menu item
154                     * @param {object} selection the selection object
155                     *
156                     * @return {void}
157                     */
158                    callback: function (key, selection) {
159                        if (window.confirm(LANG.plugins.edittable.confirmdeletecol)) {
160                            var amount = selection.end.col - selection.start.col + 1;
161                            this.alter('remove_col', selection.start.col, amount);
162                        }
163                    },
164                    /**
165                     * do not show when this is the last row
166                     *
167                     * @return {boolean} true if the entry is to be disabled, false otherwise
168                     */
169                    disabled: function () {
170                        var colsInTable = this.countCols();
171                        var firstSelectedColumn = this.getSelected()[1];
172                        var lastSelectedColumn = this.getSelected()[3]; // fix magic number with destructuring once we drop IE11
173                        var allColsSelected = firstSelectedColumn === 0 && lastSelectedColumn === colsInTable - 1;
174                        return (colsInTable <= 1 || allColsSelected);
175                    }
176                },
177                col_right: {
178                    name: LANG.plugins.edittable.col_right
179                },
180                hsep3: '---------',
181                mergeCells: {
182                    name: function () {
183                        var sel = this.getSelected();
184                        var info = this.mergeCells.mergedCellInfoCollection.getInfo(sel[0], sel[1]);
185                        if (info) {
186                            return '<div class="unmerge">' + LANG.plugins.edittable.unmerge_cells + '</div>';
187                        } else {
188                            return '<div class="merge">' + LANG.plugins.edittable.merge_cells + '</div>';
189                        }
190                    },
191
192                    /**
193                     * disable if only one cell is selected
194                     *
195                     * @return {boolean} true if the entry is to be disabled, false otherwise
196                     */
197                    disabled: function () {
198                        var selection = this.getSelected();
199                        var startRow = selection[0];
200                        var startCol = selection[1];
201                        var endRow = selection[2];
202                        var endCol = selection[3];
203                        return startRow === endRow && startCol === endCol;
204                    }
205
206                }
207            }
208        };
209    };
210}(window.edittable));
211