forked from PrestaShop/PrestaShop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhookLiveEdit.js
272 lines (265 loc) · 9 KB
/
hookLiveEdit.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
var has_been_moved = false;
var modules_list = new Array();
var hooks_list = new Array();
var hookable_list = new Array();
var timer;
$(document).ready(function() {
// do some place for submit button
$('body').css('margin-top', '45px');
// this is the result box, which will "pop up" the succeed or fail result
$('#fancy').fancybox({
autoDimensions: true,
autoScale: true,
width: 300,
height: 300,
padding: 0,
hideOnOverlayClick: false,
hideOnContentClick: false,
showCloseButton: false
});
$('#live_edit_feedback_str').html('');
// add liveToken in each link to navigate into the shop and keeping the liveedit mode
$('a').each(function() {
var href = this.href;
var search = this.search;
var hrefAdd = 'live_edit&liveToken=' + get('liveToken') + '&ad=' + get('ad') + '&id_shop=' + get('id_shop') + '&id_employee=' + get('id_employee');
if (href != undefined && href != '#' && href.substr(0, baseDir.length) == baseDir)
{
if (search.length == 0)
this.search = hrefAdd;
else
this.search += '&' + hrefAdd;
}
});
// populate
getHookableList();
$('.unregisterHook').unbind('click').click(function()
{
id = $(this).attr('id');
$(this).parent().parent().parent().fadeOut('slow', function() {
$(this).remove();
});
return false;
});
$('#cancelMove').unbind('click').click(function()
{
$('#' + cancelMove + '').sortable('cancel');
return false;
});
$('#saveLiveEdit').unbind('click').click(function()
{
saveModulePosition();
return false;
});
$('#closeLiveEdit').unbind('click').click(function()
{
if (!has_been_moved)
closeLiveEdit();
else
{
$("#live_edit_feedback_str").html('<div style="padding:10px;"><p style="margin-bottom:10px;">' + confirmClose + '</p><p style="height:1.6em;display:block"><a style="margin:auto;float:left" class="button" href="#" onclick="closeLiveEdit();">' + confirm + '</a><a style="margin:auto;float:right;" class="button" href="#" onclick="closeFancybox();">' + cancel + '</a></p></div>');
$("#fancy").attr('href', '#live_edit_feedback');
$("#fancy").trigger("click");
}
return false;
});
$('.add_module_live_edit').unbind('click').click(function()
{
$("#live_edit_feedback_str").html('<div style="text-align:center; padding: 30px;"><img src="' + baseDir + 'img/loadingAnimation.gif"></div>');
$("#fancy").attr('href', '#live_edit_feedback');
$("#fancy").trigger("click");
var id = $(this).attr('id');
getHookableModuleList(id.substr(4, id.length));
return false;
});
$('.dndHook').each(function() {
var id_hook = $(this).attr('id');
var new_target_id = '';
var old_target = '';
var cancel = false;
$('#' + id_hook + '').sortable({
opacity: 0.5,
cursor: 'move',
connectWith: '.dndHook',
receive: function(event, ui) {
if (new_target_id == '') {
new_target_id = event.target.id;
}
has_been_moved = true;
},
start: function(event, ui) {
new_target_id = ui.item[0].parentNode.id;
},
stop: function(event, ui) {
if (cancel)
{
$(this).sortable('cancel');
}
else
{
old_target = event.target.id;
cancelMove = old_target;
if (new_target_id == '')
new_target_id = old_target;
ids = $(ui.item[0]).attr('id').split('_');
newHookId = $("input[value="+new_target_id+"]").attr('name').substr(10);
newHookId = newHookId.substr(0, newHookId.length -1);
new_id = ids[0] + "_" + newHookId + "_" + ids[2] + "_" + ids[3] + "_" + ids[4] + "_" + ids[5] ;
$(ui.item[0]).attr('id', new_id);
}
},
change: function(evartent, ui) {
new_target_id = $(ui.placeholder).parent().attr('id');
ids = ui.item[0].id.split('_');
if ($.inArray(ids[5], hookable_list[new_target_id]) != -1) {
cancel = false;
ui.placeholder.css({
visibility: 'visible',
border: '1px solid #72CB67',
background: '#DFFAD3'
});
}
else
{
ui.placeholder.css({
visibility: 'visible',
border: '1px solid #EC9B9B',
background: '#FAE2E3'
});
cancel = true;
}
}
});
$('#' + id_hook + '').disableSelection();
});
});
// init hookable_list
function getHookableList() {
hooks_list = new Array();
$("input[name^=hook_list]}").each(function(e){
hooks_list.push($(this).val());
});
$.ajax({
type: 'POST',
url: baseDir + ad + '/index.php',
async: true,
dataType: 'json',
data: {
action: 'getHookableList',
tab: 'AdminModulesPositions',
ajax:1,
hooks_list: hooks_list,
modules_list: modules_list,
id_shop: get('id_shop'),
token: get('liveToken')
},
success: function(jsonData) {
if (jsonData.hasError) {
var errors = '';
for (error in jsonData.errors) //IE6 bug fix
if (error != 'indexOf')
errors += $('<div />').html(jsonData.errors[error]).text() + "\n";
alert(errors);
}
else
hookable_list = jsonData;// create and fill input array
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('#live_edit_feedback_str').html('<div class="live_edit_feed_back_ko" style="text-align:center; padding-top: 33px;"><img src="' + baseDir + 'img/admin/error.png"><h3>TECHNICAL ERROR:</h3>' + loadFail + '<br><br><a style="margin:auto" class="button" href="#" onclick="closeFancybox();">' + close + '</a></div>');
$("#fancy").attr('href', '#live_edit_feedback');
$("#fancy").trigger("click");
}
});
}
function getHookableModuleList(hook) {
$.ajax({
type: 'GET',
url: baseDir + ad + '/index.php',
async: true,
dataType: 'json',
data: {
ajax:1,
tab: 'AdminModulesPositions',
action:'getHookableModuleList',
hook: hook,
id_shop: get('id_shop'),
token: get('liveToken')
},
success: function(jsonData) {
var select = '<select id="select_module">';
for (var i = 0; i < jsonData.length; i++) {
select += '<option value="' + jsonData[i].id + '">' + jsonData[i].name + '</option>';
}
select += '</select>';
$("#live_edit_feedback_str").html('<div style="padding:10px">' + select + '<br><br><a style="margin:auto" class="button" href="#" >' + add + '</a><br><br><a style="margin:auto" class="button" href="#" onclick="closeFancybox();">' + cancel + '</a>');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("TECHNICAL ERROR: unable to unregister hook \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
}
});
}
function saveModulePosition()
{
$("input.dynamic-input-save-position").remove();
$("#live_edit_feedback_str").html('<div style="text-align:center; padding: 30px;"><img src="' + baseDir + 'img/loadingAnimation.gif"></div>');
$("#fancy").attr('href', '#live_edit_feedback');
$("#fancy").trigger("click");
var str = '';
for (var i = 0; i < hooks_list.length; i++) {
str += '&' + hooks_list[i] + '=';
$('#' + hooks_list[i] + ' > .dndModule').each(function() {
ids = $(this).attr('id').split('_');
$("#liveEdit-action-form")
.append('<input class="dynamic-input-save-position" type="hidden" name="hook['+ids[1]+'][]" value="'+ids[3]+'" />');
});
}
$("#liveEdit-action-form")
.append('<input class="dynamic-input-save-position" type="hidden" name="saveHook" value="1" />');
datas = $("#liveEdit-action-form").serializeArray();
$.ajax({
type: 'POST',
url: baseDir + ad + "/index.php",
async: true,
dataType: 'json',
data: datas,
success: function(jsonData) {
$('#live_edit_feedback_str').html('<div class="live_edit_feed_back_ok" style="text-align:center; padding-top: 33px;"><img src="' + baseDir + 'img/admin/ok2.png"><h3>' + saveOK + '</h3><a style="margin:auto" class="exclusive" href="#" onclick="closeFancybox();">' + close + '</a></div>');
timer = setTimeout("hideFeedback()", 3000);
has_been_moved = false;
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('#live_edit_feedback_str').html('<div class="live_edit_feed_back_ko" style="text-align:center; padding-top: 33px;"><img src="' + baseDir + 'img/admin/error.png"><h3>TECHNICAL ERROR:</h3>' + unableToSaveModulePosition + '<br><br><a style="margin:auto" class="button" href="#" onclick="closeFancybox();">' + close + '</a></div>');
}
}
)
return true;
}
function closeFancybox()
{
clearTimeout(timer);
$.fancybox.close();
$('#live_edit_feedback_str').html('');
}
function closeLiveEdit()
{
window.location.href = window.location.protocol+'//'+window.location.host+window.location.pathname;
}
function hideFeedback()
{
$('#live_edit_feed_back').fadeOut('slow', function() {
$.fancybox.close();
$('#live_edit_feedback_str').html('');
});
};
function get(name)
{
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) {
return "";
}
else {
return results[1];
}
}