This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RockTabulator.js
152 lines (130 loc) · 3.58 KB
/
RockTabulator.js
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
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
/**
* Main RockTabulator class
*/
if(ProcessWire.config.sandbox) {
// we are in the sandbox so we create a global grid variable
var _grid;
}
// main object
function RockTabulator() {
/**
* Array of all grids
*/
this.grids = {};
/**
* AJAX endpoint url
*/
this.url = ProcessWire.config.urls.root+'rocktabulator/';
/**
* Init plugins
*/
/**
* Populate all properties of the config to this Tabulator
*/
var config = ProcessWire.config.RockTabulator;
for (var prop in config) {
if (Object.prototype.hasOwnProperty.call(config, prop)) {
this[prop] = config[prop];
}
}
};
/**
* Init a grid when the dom element was loaded
* This does NOT init the tabulator. This is necessary so that tabulators
* can manually be initialized (eg when loading data from another grid).
*/
RockTabulator.prototype.initGrid = function(el, data) {
// get jquery and dom object of element
var $el = el;
if(!el.jquery) $el = $(el);
// make sure it is the inputfield li element
$el = $el.closest('li.Inputfield');
if(!$el.length) {
alert('RockTabulator must be initialized inside of an Inputfield');
return;
}
el = $el[0];
// show header
$el.find(".RockTabulatorWrapper > .top").removeClass('uk-hidden');
// save it to the grid
var name = el.id.replace('Inputfield_', '');
var grid = RockTabulator.getGrid(name);
if(grid) return grid;
else return this.addGrid(name, data);
}
/**
* Send AJAX request to get data
*/
RockTabulator.prototype.post = function(obj) {
var doneCallback = obj.done || function(){};
var errorCallback = obj.error || function(){};
var alwaysCallback = obj.always || function(){};
// prepare lang
var lang = null;
if(ProcessWire.config.LanguageSupport) {
lang = ProcessWire.config.LanguageSupport.language.id;
}
// setup payload
var payload = obj.payload || {}
$.extend(payload, {
name: obj.name,
lang: lang,
});
// send post request
var url = obj.url || RockTabulator.url;
$.post(url, payload)
.done(doneCallback)
.error(errorCallback)
.always(alwaysCallback);
}
/**
* Add grid
*/
RockTabulator.prototype.addGrid = function(name, data) {
var grid = new RockTabulatorGrid(name);
$.extend(grid, data); // load data from php json string
this.grids[name] = grid; // push grid to array
$(document).trigger('gridReady.RT', [grid]); // trigger event
// save table instance globally when we are in the sandbox
if(ProcessWire.config.sandbox) {
_grid = grid;
console.log("_grid", _grid);
}
};
/**
* Return grid by name or dom element
*/
RockTabulator.prototype.getGrid = function(name) {
if(typeof name == 'string') {
var grid = this.grids[name];
if(grid) return grid;
}
// if the grid was not found yet try to find it via jquery
var el = name;
var $li = $(el).closest('.Inputfield');
if(!$li.length) return;
return this.getGrid($li.data('name'));
}
/**
* currency.js proxy function with custom defaults
*/
RockTabulator.prototype.currency = function(val, options) {
var options = $.extend({
separator: '.',
decimal: ',',
}, options);
return currency(val, options).format();
}
/**
* Return the translated string of requested property
*/
RockTabulator.prototype._ = function(name) {
if(typeof this.langs == 'undefined') return name;
var langs = this.langs[this.locale];
return langs[name] || '';
}
// ######################### init RockTabulator #########################
var RockTabulator = new RockTabulator();
$(document).trigger('ready.RT');
// ######################################################################