forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-dashboard.js
316 lines (294 loc) · 9.53 KB
/
admin-dashboard.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
// This variables are defined in the dashboard view.tpl
// dashboard_ajax_url
// adminstats_ajax_url
// no_results_translation
// dashboard_use_push
// read_more
function refreshDashboard(module_name, use_push, extra) {
var module_list = [];
this.getWidget = function(module_id) {
$.ajax({
url : dashboard_ajax_url,
data : {
ajax: true,
action:'refreshDashboard',
module: module_list[module_id],
dashboard_use_push: Number(use_push),
extra: extra
},
global: false,
dataType: 'json',
success : function(widgets){
for (var widget_name in widgets) {
for (var data_type in widgets[widget_name]) {
window[data_type](widget_name, widgets[widget_name][data_type]);
}
}
if (parseInt(dashboard_use_push) === 1) {
refreshDashboard(false, true);
}
},
contentType: 'application/json'
});
};
if (module_name === false) {
$('.widget').each( function () {
module_list.push($(this).attr('id'));
if (!use_push) {
$(this).addClass('loading');
}
});
}
else {
module_list.push(module_name);
if (!use_push) {
$('#'+module_name+' section').each( function (){
$(this).addClass('loading');
});
}
}
for (var module_id in module_list) {
if (use_push && !$('#'+module_list[module_id]).hasClass('allow_push')) {
continue;
}
this.getWidget(module_id);
}
}
function setDashboardDateRange(action) {
$('#datepickerFrom, #datepickerTo').parent('.input-group').removeClass('has-error');
var data = 'ajax=true&action=setDashboardDateRange&submitDatePicker=true&'+$('#calendar_form').serialize()+'&'+action+'=1';
$.ajax({
url : adminstats_ajax_url,
data : data,
dataType: 'json',
type: 'POST',
success : function(jsonData){
if (!jsonData.has_errors) {
refreshDashboard(false, false);
$('#datepickerFrom').val(jsonData.date_from);
$('#datepickerTo').val(jsonData.date_to);
}
else {
$('#datepickerFrom, #datepickerTo').parent('.input-group').addClass('has-error');
}
}
});
}
function data_value(widget_name, data) {
for (var data_id in data) {
$('#'+data_id+' ').html(data[data_id]);
$('#'+data_id+', #'+widget_name).closest('section').removeClass('loading');
}
}
function data_trends(widget_name, data) {
for (var data_id in data) {
this.el = $('#'+data_id);
this.el.html(data[data_id].value);
if (data[data_id].way === 'up') {
this.el.parent().removeClass('dash_trend_down').removeClass('dash_trend_right').addClass('dash_trend_up');
}
else if (data[data_id].way === 'down') {
this.el.parent().removeClass('dash_trend_up').removeClass('dash_trend_right').addClass('dash_trend_down');
}
else {
this.el.parent().removeClass('dash_trend_down').removeClass('dash_trend_up').addClass('dash_trend_right');
}
this.el.closest('section').removeClass('loading');
}
}
function data_table(widget_name, data) {
for (var data_id in data) {
//fill header
var tr = '<tr>';
for (var header in data[data_id].header) {
var head = data[data_id].header[header];
var th = '<th '+ (head.class ? ' class="'+head.class+'" ' : '' )+ ' '+(head.id ? ' id="'+head.id+'" ' : '' )+'>';
th += (head.wrapper_start ? ' '+head.wrapper_start+' ' : '' );
th += head.title;
th += (head.wrapper_stop ? ' '+head.wrapper_stop+' ' : '' );
th += '</th>';
tr += th;
}
tr += '</tr>';
$('#'+data_id+' thead').html(tr);
//fill body
$('#'+data_id+' tbody').html('');
if(typeof data[data_id].body === 'string') {
$('#'+data_id+' tbody').html('<tr><td class="text-center" colspan="'+data[data_id].header.length+'"><br/>'+data[data_id].body+'</td></tr>');
}
else if (data[data_id].body.length) {
for (var body_content_id in data[data_id].body) {
tr = '<tr>';
for (var body_content in data[data_id].body[body_content_id]) {
var body = data[data_id].body[body_content_id][body_content];
var td = '<td '+ (body.class ? ' class="'+body.class+'" ' : '' )+ ' '+(body.id ? ' id="'+body.id+'" ' : '' )+'>';
td += (body.wrapper_start ? ' '+body.wrapper_start+' ' : '' );
td += body.value;
td += (body.wrapper_stop ? ' '+body.wrapper_stop+' ' : '' );
td += '</td>';
tr += td;
}
tr += '</tr>';
$('#'+data_id+' tbody').append(tr);
}
}
else {
$('#'+data_id+' tbody').html('<tr><td class="text-center" colspan="'+data[data_id].header.length+'">'+no_results_translation+'</td></tr>');
}
}
}
function data_chart(widget_name, charts) {
for (var chart_id in charts) {
window[charts[chart_id].chart_type](widget_name, charts[chart_id]);
}
}
function data_list_small(widget_name, data) {
for (var data_id in data)
{
$('#'+data_id).html('');
for (var item in data[data_id]) {
$('#'+data_id).append('<li><span class="data_label">'+item+'</span><span class="data_value size_s">'+data[data_id][item]+'</span></li>');
}
$('#'+data_id+', #'+widget_name).closest('section').removeClass('loading');
}
}
function getBlogRss() {
$.ajax({
url : dashboard_ajax_url,
data : {
ajax:true,
action:'getBlogRss'
},
dataType: 'json',
success : function(jsonData) {
if (typeof jsonData !== 'undefined' && jsonData !== null && !jsonData.has_errors) {
for (var article in jsonData.rss) {
var article_html = '<article><h4><a href="'+jsonData.rss[article].link+'">'+jsonData.rss[article].title+'</a></h4><span class="dash-news-date text-muted">'+jsonData.rss[article].date+'</span><p>'+jsonData.rss[article].short_desc+' <a href="'+jsonData.rss[article].link+'">'+read_more+'</a><p></article><hr/>';
$('.dash_news .dash_news_content').append(article_html);
}
}
else {
$('.dash_news').hide();
}
}
});
}
function toggleDashConfig(widget) {
var func_name = widget + '_toggleDashConfig';
if ($('#'+widget+' section.dash_config').hasClass('hide'))
{
$('#'+widget+' section').not('.dash_config').slideUp(500, function () {
$('#'+widget+' section.dash_config').fadeIn(500).removeClass('hide');
if (window[func_name] != undefined)
window[func_name]();
});
}
else
{
$('#'+widget+' section.dash_config').slideUp(500, function () {
$('#'+widget+' section').not('.dash_config').slideDown(500).removeClass('hide');
$('#'+widget+' section.dash_config').addClass('hide');
if (window[func_name] != undefined)
window[func_name]();
});
}
}
function bindSubmitDashConfig() {
$('.submit_dash_config').on('click', function () {
saveDashConfig($(this).closest('section.widget').attr('id'));
return false;
});
}
function bindCancelDashConfig() {
$('.cancel_dash_config').on('click', function () {
toggleDashConfig($(this).closest('section.widget').attr('id'));
return false;
});
}
function saveDashConfig(widget_name) {
$('section#'+widget_name+' .form-group').removeClass('has-error');
$('#'+widget_name+'_errors').remove();
configs = '';
$('#'+widget_name+' form input, #'+widget_name+' form textarea , #'+widget_name+' form select').each( function () {
if ($(this).attr('type') === 'radio' && !$(this).attr('checked')) {
return;
}
configs += '&configs['+$(this).attr('name')+']='+$(this).val();
});
data = 'ajax=true&action=saveDashConfig&module='+widget_name+configs+'&hook='+$('#'+widget_name).closest('[id^=hook]').attr('id');
$.ajax({
url : dashboard_ajax_url,
data : data,
dataType: 'json',
error: function(XMLHttpRequest, textStatus, errorThrown) {
jAlert("TECHNICAL ERROR: \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
},
success : function(jsonData){
if (!jsonData.has_errors)
{
$('#'+widget_name).find('section').not('.dash_config').remove();
$('#'+widget_name).append($(jsonData.widget_html).find('section').not('.dash_config'));
refreshDashboard(widget_name);
toggleDashConfig(widget_name);
}
else
{
errors_str = '<div class="alert alert-danger" id="'+widget_name+'_errors">';
for (error in jsonData.errors)
{
errors_str += jsonData.errors[error]+'<br/>';
$('#'+error).closest('.form-group').addClass('has-error');
}
errors_str += '</div>';
$('section#'+widget_name+'_config header').after(errors_str);
errors_str += '</div>';
}
}
});
}
$(document).ready( function () {
$('#calendar_form input[type="submit"]').on('click', function(elt) {
elt.preventDefault();
setDashboardDateRange(elt.currentTarget.name);
});
refreshDashboard(false, false);
getBlogRss();
bindSubmitDashConfig();
bindCancelDashConfig();
$('.ps_dashboard_simulation').change(function(e) {
$.ajax({
url : dashboard_ajax_url,
data : {
ajax:true,
action:'setSimulationMode',
PS_DASHBOARD_SIMULATION: $(this).val()
},
success : function(result) {
refreshDashboard(false, false);
}
});
});
});