Newer
Older
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
/*+**********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
************************************************************************************/
window.app = (function () {
// Protected Globals
var _controller = null;
// Copy meta to avoid runtime tampering.
var _module = _META.module;
var _view = _META.view;
var _parentModule = _META.parent;
var _app = _META.app;
function Request() {
this._requestPjax = function (params) {
var aDeferred = jQuery.Deferred();
if (typeof params.url === 'undefined') {
params.url = 'index.php';
}
if (typeof params.container === 'undefined') {
params.container = '#pjaxContainer';
params.defaultContainer = true;
}
var pjaxContainer = jQuery('#pjaxContainer');
//Clear contents existing before
if (params.container === '#pjaxContainer') {
pjaxContainer.html('');
}
jQuery(document).on('pjax:success', function (event, data, status, jqXHR) {
if (typeof data == 'object') {
if (!data['success'] && data['error']['message']) {

Satish
committed
aDeferred.resolve(new VtError(data['error']));
} else if (data['result'] && data['result'] !== null) {
data = data['result'];
}
}
if (params.defaultContainer) {
//remove the data in default container since it will be having duplicate data which will be problem espcially with id selector
pjaxContainer.html('');
}
aDeferred.resolve(null, data);
});
jQuery(document).on('pjax:error', function (event, jqXHR, textStatus, errorThrown) {

Satish
committed
aDeferred.resolve(new VtError(errorThrown));
});
jQuery.pjax(params);
return aDeferred.promise();
},
this._request = function (params) {
var aDeferred = jQuery.Deferred();
if (typeof params.url === 'undefined') {
params.url = 'index.php';
}
var ajaxParams = {
method: params.type,
url: params.url,
data: params.data,
success: function (response) {
if (typeof response == 'object') {
if (!response['success'] && response['error']['message']) {

Satish
committed
aDeferred.resolve(new VtError(response['error']));
} else if (response['result'] && response['result'] !== null) {
response = response['result'];
}
}
aDeferred.resolve(null, response);
},
error: function (xhr, ajaxOptions, err) {

Satish
committed
aDeferred.resolve(new VtError(err));
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
}
};
if (typeof params.contentType != 'undefined') {
ajaxParams['contentType'] = params.contentType;
}
if (typeof params.processData != 'undefined') {
ajaxParams['processData'] = params.processData;
}
jQuery.ajax(ajaxParams);
return aDeferred.promise();
},
this.get = function (params) {
var aDeferred = jQuery.Deferred();
params.type = 'GET';
this._request(params).then(function (err, data) {
return aDeferred.resolve(err, data);
});
return aDeferred.promise();
},
this.post = function (params) {
var aDeferred = jQuery.Deferred();
params.type = 'POST';
//caller has send only url so we need to break it to data
if (typeof params.data == 'undefined') {
params.data = app.convertUrlToDataParams(params.url);
delete params.url;
}
/**
* Safari 11.1 - ajax/XHR form submission will fail if input[type=file] is empty in formData.
*/
var isIOSDevice = navigator.userAgent.match(/iPhone|iPod|iPad/i) != null;
if ((!!window.safari === true || isIOSDevice)
&& params.data instanceof FormData && typeof params.data.getAll == 'function') {
var fileNameAttribute = (params.data.getAll("filename[]").length > 0) ? 'filename[]' : 'file[]';
var fileNames = params.data.getAll(fileNameAttribute);
params.data.delete(fileNameAttribute);
jQuery.each(fileNames, function (key, fileNameObject) {
if (fileNameObject.name) {
params.data.append(fileNameAttribute, fileNameObject);
}
});
}
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
this._request(params).then(function (err, data) {
return aDeferred.resolve(err, data);
});
return aDeferred.promise();
},
this.pjax = function (params) {
var aDeferred = jQuery.Deferred();
params.type = 'GET';
this._requestPjax(params).then(function (err, data) {
return aDeferred.resolve(err, data);
});
return aDeferred.promise();
}
}
function Event() {
this.el = jQuery({});
this.trigger = function (/* eventName, arg1, arg2... */) {
var args = Array.prototype.slice.call(arguments, 1);
this.el.trigger(arguments[0], args);
}
this.on = function (/* eventName, fn */) {
this.el.on.apply(this.el, arguments);
}
this.one = function (/* eventName, fn */) {
this.el.one.apply(this.el, arguments);
}
this.off = function (/* eventName, fn */) {
this.el.off.apply(this.el, arguments);
}
}
return {
storage: {
NSKey: function (key) {
return 'vtiger6.'+key; // retain the existing cached data
},
get: function (key, defvalue) {
key = this.NSKey(key);
return jQuery.jStorage.get(key, defvalue);
},
set: function (key, value) {
key = this.NSKey(key);
jQuery.jStorage.set(key, value);
},
delete: function (key) {
key = this.NSKey(key);
jQuery.jStorage.deleteKey(key);
},
flush: function () {
jQuery.jStorage.flush();
}
},
request: new Request(),
event: new Event(),
helper: new Vtiger_Helper_Js(),
/**
* Function to decode the encoded htmlentities values
*/
getDecodedValue: function (value) {
return jQuery('<div></div>').html(value).text();
},
/**
* To Convert give URL to POST params
* @param {type} url
* @returns {Array}
*/
convertUrlToDataParams: function (url) {
var params = {};
if (typeof url !== 'undefined' && url.indexOf('?') !== -1) {
var urlSplit = url.split('?');
url = urlSplit[1];
}
var queryParameters = url.split('&');
for (var index = 0; index < queryParameters.length; index++) {
var queryParam = queryParameters[index];
var queryParamComponents = queryParam.split('=');
if (queryParamComponents[0] in params) params[queryParamComponents[0]] += '&' + queryParamComponents[0] + '=' + queryParamComponents[1];
else params[queryParamComponents[0]] = queryParamComponents[1];
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
}
return params;
},
module: function () {
return _module;
},
view: function () {
return _view;
},
/**
* Function returns the current Module name
*/
getModuleName: function () {
return _module;
},
getExtensionModuleName: function () {
var extensionModuleName = '';
if (typeof _EXTENSIONMETA !== 'undefined' && _EXTENSIONMETA.hasOwnProperty('module')) {
extensionModuleName = _EXTENSIONMETA.module;
}
return extensionModuleName;
},
getAppName: function () {
return _app;
},
/**
* Function returns the application name
* @returns {META.parent}
*/
getParentModuleName: function () {
return _parentModule;
},
getDecimalSeparator: function () {
return jQuery('body').data('user-decimalseparator');
},
getGroupingSeparator: function () {
return jQuery('body').data('user-groupingseparator');
},
getNumberOfDecimals: function () {
return jQuery('body').data('user-numberofdecimals');
},
getDateFormat: function () {
return jQuery('body').data('user-dateformat');
},
getHourFormat: function () {
return jQuery('body').data('user-hourformat');
},
getActivityReminderInterval: function () {
return jQuery('body').data('userCalendarReminderInterval');
},
getUserId: function () {
if (_USERMETA)
return _USERMETA.id;
},
getMenuStatus: function () {
if (_USERMETA)
return _USERMETA.menustatus;
},
getRecordId: function () {
var record = jQuery('#recordId')
if (record.length) {
return record.val();
}
return false;
},
getModuleSpecificViewClass: function (view, module, parentModule) {
if (typeof module == 'undefined') {
module = _module;
}
if (typeof parentModule == 'undefined') {
parentModule = this.getParentModuleName();
}
if (view == '') {
view = 'Index';
}
var controllerClass = null;
var handlerClass = module+'_'+view+'_Js';
if (parentModule == 'Settings') {
handlerClass = parentModule+'_'+module+'_'+view+'_Js';
if (typeof window[handlerClass] == 'undefined') {
handlerClass = parentModule+'_Vtiger_'+view+'_Js';
}
}
if (typeof window[handlerClass] != 'undefined') {
controllerClass = handlerClass;
} else {
handlerClass = 'Vtiger_'+view+'_Js';
if (typeof window[handlerClass] != 'undefined') {
controllerClass = handlerClass;
}
}
return controllerClass;
},
controller: function () {
if (_controller == null) {
var controllerClass = this.getModuleSpecificViewClass(_view);
if (controllerClass) {
_controller = new window[controllerClass]();
_controller.intializeComponents();
_controller.registerEvents();
}
}
return _controller;
},
htmlEncode: function (value) {
if (value) {
return jQuery('<div />').text(value).html();
} else {
return '';
}
},
htmlDecode: function (value) {
if (value) {
return $('<div />').html(value).text();
} else {
return '';
}
},
changeURL : function(url){
if (typeof history.pushState !== "undefined") {
history.pushState({}, "", url);
} else {
console.log("Browser does not support HTML5.");
}
},
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/**
* Function returns translated string
*/
languageString: [],
vtranslate: function (key) {
//convert arguments in to proper array
var params = [].slice.apply(arguments);
params.shift();
function __sprintf(translatedString, params) {
if (params.length > 0) {
var replaceRegex = new RegExp('(%s)', 'g');
var paramsPointer = 0;
translatedString = translatedString.replace(replaceRegex, function () {
var string = params[paramsPointer];
paramsPointer++;
return string;
});
}
return translatedString;
}
var translatedString = key;
if (app.languageString[key] !== undefined) {
translatedString = app.languageString[key];
} else {
var strings = jQuery('#js_strings').text();
if (strings != '') {
app.languageString = JSON.parse(strings);
if (key in app.languageString) {
translatedString = app.languageString[key];
}
}
}
if (translatedString !== null) {
return __sprintf(translatedString, params);
}
return key;
},
/**
* Function will return the current users layout+skin path
* @param <string> img - image name
* @return <string>
*/
vimage_path: function (img) {
return jQuery('body').data('skinpath')+'/images/'+img;
},
/**
* Function to get the select2 element from the raw select element
* @params: select element
* @return : select2Element - corresponding select2 element
*/
getSelect2ElementFromSelect: function (selectElement) {
var selectId = selectElement.attr('id');
//since select2 will add s2id_ to the id of select element
var select2EleId = 's2id_'+selectId;
return jQuery('#'+select2EleId);
},
getUserLanguage: function () {
return jQuery('body').data('language');
},
convertTojQueryDatePickerFormat: function (dateFormat) {
var i = 0;
var splitDateFormat = dateFormat.split('-');
for (var i in splitDateFormat) {
var sectionDate = splitDateFormat[i];
var sectionCount = sectionDate.length;
if (sectionCount == 4) {
var strippedString = sectionDate.substring(0, 2);
splitDateFormat[i] = strippedString;
}
}
var joinedDateFormat = splitDateFormat.join('-');
return joinedDateFormat;
},
getDateInVtigerFormat: function (dateFormat, dateObject) {
var finalFormat = app.convertTojQueryDatePickerFormat(dateFormat);
var date = jQuery.datepicker.formatDate(finalFormat, dateObject);
return date;
},
getUserCurrencySymbol: function () {
return _USERMETA.currency;
},
getUserCurrencySymbolPlacement: function () {
return _USERMETA.currencySymbolPlacement;
},
appendUserCurrencySymbol: function (value) {
var userCurrencySymbol = app.getUserCurrencySymbol();
var userCurrencySymbolPlacement = app.getUserCurrencySymbolPlacement();
var appendedValue = value;
if (userCurrencySymbolPlacement === '1.0$') {
appendedValue = value+userCurrencySymbol;
} else {
appendedValue = userCurrencySymbol+value;
}
return appendedValue;
},
convertCurrencyToUserFormat: function (value, appendCurrencySymbol) {
var displayValue;
var isNegative = false;
value = value.toString();
if (parseFloat(value) < 0) {
isNegative = true;
value = value.replace('-', '');
}
var groupingPattern = _USERMETA.currencyGroupingPattern;
var numberOfDecimals = app.getNumberOfDecimals();
var decimalSeparator = app.getDecimalSeparator();
var groupingSeparator = app.getGroupingSeparator();
value = parseFloat(value).toFixed(parseInt(numberOfDecimals));
value = value.toString();
var valueParts = value.split('.');
var wholePart = valueParts[0];
var decimalPart = valueParts[1];
var truncateTrailingZeros = _USERMETA.truncateTrailingZeros;
var finalWholePart;
var ignoreDecimal = false;
if (truncateTrailingZeros == '1' && parseInt(decimalPart) === 0) {
ignoreDecimal = true;
}
if (groupingPattern == '123456789') {
finalWholePart = wholePart;
} else if (groupingPattern == '123456,789') {
if (wholePart.length > 3) {
var wholeFirstPart = wholePart.substr(0, (wholePart.length - 3));
}
var wholeLastPart = wholePart.substr(wholePart.length - 3);
if (wholeFirstPart) {
wholePart = wholeFirstPart+groupingSeparator+wholeLastPart;
}
finalWholePart = wholePart;
} else if (groupingPattern == '123,456,789') {
var wholeParts = wholePart.toString().split('').reverse().join('').match(/.{1,3}/g).reverse();
for (var i = 0; i < wholeParts.length; i++) {
wholeParts[i] = wholeParts[i].toString().split('').reverse().join('');
}
finalWholePart = wholeParts.join(groupingSeparator);
} else if (groupingPattern == '12,34,56,789') {
if (wholePart.length > 3) {
var wholeFirstPart = wholePart.substr(0, (wholePart.length - 3));
}
var wholeLastPart = wholePart.substr(wholePart.length - 3);
if (wholeFirstPart) {
wholeLastPart = groupingSeparator+wholeLastPart;
var wholeFirstParts = wholeFirstPart.toString().split('').reverse().join('').match(/.{1,2}/g).reverse();
for (var i = 0; i < wholeFirstParts.length; i++) {
wholeFirstParts[i] = wholeFirstParts[i].toString().split('').reverse().join('');
}
wholeFirstPart = wholeFirstParts.join(groupingSeparator);
finalWholePart = wholeFirstPart+wholeLastPart;
} else {
finalWholePart = wholeLastPart;
}
}
if (ignoreDecimal) {
displayValue = finalWholePart;
} else {
displayValue = finalWholePart+decimalSeparator+decimalPart;
}
if (isNegative) {
displayValue = '-'+displayValue;
}
if (appendCurrencySymbol) {
displayValue = app.appendUserCurrencySymbol(displayValue);
}
return displayValue;
}
}
})();
/* Turn-off jQuery Migrate warnings */
jQuery.migrateMute = true;
jQuery(function () {
vtUtils.applyFieldElementsView(jQuery('body'));
window.app.controller();
String.prototype.toCamelCase = function () {
var value = this.valueOf();
return value.charAt(0).toUpperCase()+value.slice(1).toLowerCase()
}
/* To push focus on CKEditor Popup when shown with Bootstrap modal */
/* ref https://stackoverflow.com/a/23667151 */
jQuery.fn.modal.Constructor.prototype.enforceFocus = function() {
modal_this = this
jQuery(document).on('focusin.modal', function (e) {
if (modal_this.$element[0] !== e.target && !modal_this.$element.has(e.target).length
&& !jQuery(e.target.parentNode).hasClass('cke_dialog_ui_input_select')
&& !jQuery(e.target.parentNode).hasClass('cke_dialog_ui_input_textarea')
&& !jQuery(e.target.parentNode).hasClass('cke_dialog_ui_input_text')) {
modal_this.$element.focus()
}
})};
});