/
help.js
executable file
·363 lines (342 loc) · 11.7 KB
/
help.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
*
* 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 license@prestashop.com 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 <contact@prestashop.com>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
$(function() {
var storage = getStorageAvailable();
initHelp = function(){
$('#main').addClass('helpOpen');
//first time only
if( $('#help-container').length === 0) {
//add css
$('head').append('<link href="//help.prestashop.com/css/help.css" rel="stylesheet">');
//add container
$('#main').after('<div id="help-container"></div>');
}
//init help (it use a global javascript variable to get actual controller)
pushContent(help_class_name);
$('#help-container').on('click', '.popup', function(e){
e.preventDefault();
if (storage)
storage.setItem('helpOpen', false);
$('.toolbarBox a.btn-help').trigger('click');
var helpWindow = window.open("index.php?controller=" + help_class_name + "?token=" + token + "&ajax=1&action=OpenHelp", "helpWindow", "width=450, height=650, scrollbars=yes");
});
};
//init
$('.toolbarBox a.btn-help').on('click', function(e) {
e.preventDefault();
if( !$('#main').hasClass('helpOpen') && document.body.clientWidth > 1200) {
if (storage)
storage.setItem('helpOpen', true);
$('.toolbarBox a.btn-help i').removeClass('process-icon-help').addClass('process-icon-loading');
initHelp();
} else if(!$('#main').hasClass('helpOpen') && document.body.clientWidth < 1200){
var helpWindow = window.open("index.php?controller=" + help_class_name + "?token=" + token + "&ajax=1&action=OpenHelp", "helpWindow", "width=450, height=650, scrollbars=yes");
} else {
$('#main').removeClass('helpOpen');
$('#help-container').html('');
$('.toolbarBox a.btn-help i').removeClass('process-icon-close').addClass('process-icon-help');
if (storage)
storage.setItem('helpOpen', false);
}
});
// Help persistency
if (storage && storage.getItem('helpOpen') == "true") {
$('a.btn-help').trigger('click');
}
//switch home
var language = iso_user;
var home;
switch(language) {
case 'en':
home = '19726802';
break;
case 'fr':
home = '20840479';
break;
default:
language = 'en';
home = '19726802';
}
//feedback
var arr_feedback = {};
arr_feedback.page = 'page';
arr_feedback.helpful = 'helpful';
//toc
var toc = [];
var lang = [
['en','19726802'],
['fr','20840479']
];
function getStorageAvailable() {
test = 'foo';
storage = window.localStorage || window.sessionStorage;
try {
storage.setItem(test, test);
storage.removeItem(test);
//open help if localstorage helpOpen = true;
return storage;
}
catch (error) {
return null;
}
}
// change help icon
function iconCloseHelp(){
$('.toolbarBox a.btn-help i').removeClass('process-icon-loading').addClass('process-icon-close');
}
//get content
function getHelp(pageController) {
var request = encodeURIComponent("getHelp=" + pageController + "&version=1.6&language=" + iso_user);
var d = new $.Deferred();
$.ajax( {
url: "//help.prestashop.com/api/?request=" + request,
jsonp:"callback",
dataType:"jsonp",
success: function(data) {
if (isCleanHtml(data))
{
$('#help-container').html(data);
d.resolve();
}
}
});
return d.promise();
}
//update content
function pushContent(target) {
$('#help-container').removeClass('openHelpNav');
$('#help-container').html('');
//@todo: track event
getHelp(target)
.then(iconCloseHelp)
.then(initToc)
.then(initNavigation)
.then(initSearch)
.then(initFeedback);
}
//build navigation
function initNavigation() {
var d = new $.Deferred();
var request = encodeURIComponent("api/content/" + home + "/child?expand=page");
$.ajax( {
url: "//help.prestashop.com/api/?request=" + request,
jsonp: "callback",
dataType: "jsonp",
success: function(data) {
for (var i = 0 ; i < data.page.results.length ; i++){
if (isCleanHtml(data.page.results[i].id + data.page.results[i].title))
$("#help-container #main-nav").append('<a href="//help.prestashop.com/' + data.page.results[i].id + '?version=1.6" data-target="' + data.page.results[i].id + '">' + data.page.results[i].title + '</a>');
}
$("#help-container #main-nav a").on('click',function(e){
e.preventDefault();
pushContent($(this).data('target'));
});
$('#help-container .open-menu').on('click', function(e){
e.preventDefault();
$('#help-container').addClass('openHelpNav');
});
$('#help-container .close-menu').on('click', function(e){
e.preventDefault();
$('#help-container').removeClass('openHelpNav');
});
d.resolve();
}
});
return d.promise();
}
//build toc getting children from home page recursively
function initToc() {
var getLinksByLang = function(item) {
var d = new $.Deferred();
var request = encodeURIComponent("api/content/" + item[1] + "/child/page?expand=children.page&limit=100");
$.ajax( {
url: "//help.prestashop.com/api/?request=" + request,
jsonp : "callback",
dataType:"jsonp",
success: function(data) {
toc[item[0]] = {
'title': 'Home ' + item[0],
'lang': item[0],
'id': item[1],
'children': []
};
data.results.map(function(page,j){
var children = [];
page.children.page.results.map(function(child,i) {
children[i] = {
'title': child.title,
'link': child._links.webui,
'id': child.id,
'lang' : item[0]
};
});
toc[item[0]].children[j] = {
'title' : page.title,
'link' : page._links.webui,
'id': page.id,
'children' : children,
'lang' : item[0]
};
});
d.resolve();
},
});
return d.promise();
};
return $.when.apply(null, lang.map(getLinksByLang)).then(function () {
//build mapping
var mapping = {};
$.each(toc[language].children,function(i,section){
mapping[section.link] = [section.id,section.title,section.lang];
if (typeof section.children !== 'undefined') {
$.each(section.children,function(i,section){
mapping[section.link] = [section.id,section.title,section.lang];
});
}
});
//remap links
$( "#help-container a[href^='/display/']" ).on('click', function(e){
e.preventDefault();
var href = $(this).attr('href');
var target = mapping[href][0];
pushContent(target);
});
$( "#help-container a[href^='/pages/viewpage.action?pageId=']" ).on('click', function(e){
e.preventDefault();
var pageId = $(this).attr('href').match(/\d+$/);
if (pageId) {
pushContent(pageId[0]);
}
});
// rewrite url ? -> "//help.prestashop.com/" + mapping[href][0] + '?version=1.6&language=' + mapping[href][2];
//home link
$('#help-container a.home').attr('href', '//help.prestashop.com/'+toc[language].id+'?version=1.6').on('click',function(e){
e.preventDefault();
pushContent(toc[language].id);
});
//target _blank external link
$( "#help-container a[href^='http://']" ).attr( "href", function() {
$(this).attr('target','_blank').append(' <i class="fa fa-external-link"></i>');
});
//add class anchor to link from table of content
$('#help-container .toc-indentation a').addClass('anchor');
});
}
//search
function initSearch() {
//replace tag from confluence search api
function strongify(str) {
return str.replace(/@@@hl@@@/g, '<strong>').replace(/@@@endhl@@@/g, '</strong>');
}
$("#help-container #search-box").on("submit",function(e) {
e.preventDefault();
$("#help-container #search-results").html('');
var searchUrl = encodeURIComponent("searchv3/1.0/search?where=PS16&type=page&queryString=");
var searchTerm = encodeURIComponent($('input[name="search"]').val());
$.ajax( {
url: "//help.prestashop.com/api/?request=" + searchUrl + searchTerm,
jsonp: "callback",
dataType: "jsonp",
success: function(data) {
if (data.results.length === 0) {
$("#search-results").addClass('hide');
}
for (var i = 0 ; i < data.results.length ; i++) {
if (isCleanHtml(data.results[i].id + data.results[i].title + data.results[i].bodyTextHighlights)) {
$("#search-results").removeClass('hide')
.append( '<div class="result-item"><i class="fa fa-file-o"></i> <a href="//help.prestashop.com/' + data.results[i].id + '?version=1.6" data-target="' + data.results[i].id + '">' + strongify(data.results[i].title) + '</a><p>' + strongify(data.results[i].bodyTextHighlights) + '</p></div>');
}
}
$("#search-results a").on('click',function(e) {
e.preventDefault();
pushContent($(this).data('target'));
});
}
});
});
$('#help-container').on('click','.search',function(e) {
e.preventDefault();
$('#help-container #search-box').removeClass('hide');
$('#help-container .header-navigation').addClass('hide');
$('#search-box input[name=search]').focus();
});
$('#help-container').on('click','.close-search',function(){
$('#help-container #search-box').addClass('hide');
$('#help-container .header-navigation').removeClass('hide');
});
}
//feedback
function initFeedback() {
var arr_feedback = {
controller: help_class_name,
language: iso_user,
helpful: null,
reason: null,
comment: null
};
$('#help-container .helpful-labels li').on('click', function(){
var percentageMap = {0:'Not at all', 25:'Not very', 50:'Somewhat', 75:'Very', 100:'Extremely'};
var percentage = parseInt($(this).data('percentage'));
arr_feedback.helpful = percentageMap[percentage];
$('#help-container .slider-cursor').removeClass('hide');
$('#help-container .helpful-labels li').removeClass('active');
$('#help-container .slider-cursor').css('left',percentage+'%');
$('#help-container .helpful-labels li').addClass('disabled').off();
$(this).removeClass('disabled').addClass('active');
if (percentage <= 25) {
$('#help-container .feedback-reason').show();
} else if (percentage > 25) {
submitFeedback(arr_feedback);
}
});
$('#help-container .feedback-reason .radio label').on('click', function() {
var reasonMap = {1:'Not related', 2:'Too complicated', 3:'Too much', 4:'Incorrect', 5:'Unclear', 6:'Incomplete'};
arr_feedback.reason = reasonMap[$('input[name=lowrating-reason]:checked').val()];
});
$('#help-container .feedback-submit').on('click', function(e) {
e.preventDefault();
arr_feedback.comment = $('textarea[name=feedback-detail]').val();
submitFeedback(arr_feedback);
});
}
function submitFeedback(arr_feedback) {
var feedback = '?';
var keys = Object.keys(arr_feedback);
for (var i = 0; i < keys.length; i++) {
if (i > 0){
feedback += '&';
}
feedback += keys[i] + '=' + arr_feedback[keys[i]];
}
$.ajax( {
url: "//help.prestashop.com/api/feedback/" + feedback,
dataType: 'jsonp',
jsonp: "callback",
success: function(){
$('#help-container #helpful-feedback').hide();
$('#help-container .thanks').removeClass('hide');
}
});
}
});