PluginProbe
Visual Slider / trunk
Visual Slider vtrunk
visual-slider / admin / assets / js / code.js

code.js in Visual Slider trunk, at admin/assets/js/code.js

248 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function($, undefined) {
2 'use strict';
3 function isset(variable) {
4 if (variable === "undefined") {
5 return '';
6 } else if (variable === undefined) {
7 return '';
8 } else if (variable === null) {
9 return '';
10 } else if (variable === 0) {
11 return 0;
12 } else if (variable === '0') {
13 return '0';
14 } else {
15 return variable;
16 }
17
18 }
19 /*************************************************************************************************************************************************************************
20 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
21 vs_settingEmptyAndZero
22 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
23 **************************************************************************************************************************************************************************/
24 function vs_settingEmptyAndZero(obj) {
25 for (var key in obj) {
26 if (obj[key] === null || obj[key] === undefined || obj[key] === 'undefined' || obj[key] === '') {
27 delete obj[key];
28 } else if ($.type(obj[key]) === 'object') {
29 vs_settingEmptyAndZero(obj[key]);
30 var sum = Object.values(obj[key]).reduce(function(acc, val) {
31 return acc + val;
32 }, 0);
33 if (sum === 0) {
34 delete obj[key];
35 }
36 }
37 }
38 return obj;
39 }
40 $.fn.vs_settingEmptyAndZero = function(obj) {
41 for (var key in obj) {
42 if (obj[key] === null || obj[key] === undefined || obj[key] === 'undefined' || obj[key] === '') {
43 delete obj[key];
44 } else if ($.type(obj[key]) === 'object') {
45 vs_settingEmptyAndZero(obj[key]);
46 var sum = Object.values(obj[key]).reduce(function(acc, val) {
47 return acc + val;
48 }, 0);
49 if (sum === 0) {
50 delete obj[key];
51 }
52 }
53 }
54 return obj;
55
56 };
57
58 /*************************************************************************************************************************************************************************
59 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
60 UrlCode
61 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
62 **************************************************************************************************************************************************************************/
63 function vs_urlencode(inputString='') {
64 if (inputString && typeof inputString !== 'object') {
65 try {
66 return btoa(encodeURIComponent(inputString));
67 } catch (e) {
68 return '';
69 }
70 } else {
71 return '';
72 }
73 }
74 /*************************************************************************************************************************************************************************
75 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
76 vs_urldecode
77 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
78 **************************************************************************************************************************************************************************/
79 function vs_urldecode(inputString = '') {
80 if (typeof inputString !== 'object' && inputString.length > 0) {
81 try {
82 let decodedString = atob(inputString);
83 return decodeURIComponent(decodedString.replace(/\+/g, '%20'));
84 } catch (e) {
85 return '';
86 }
87 } else {
88 return '';
89 }
90 }
91 /*************************************************************************************************************************************************************************
92 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
93 Data Decode
94 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
95 **************************************************************************************************************************************************************************/
96 function vs_data_encode(option, code) {
97 function vs_encodeRecursive(obj, depth, parentIndex) {
98 var output = '';
99 var r = 0;
100 for (var key in obj) {
101 if (obj.hasOwnProperty(key)) {
102 var value = obj[key];
103 var index = '';
104 if (parentIndex !== 0) {
105 index = parentIndex + '_';
106 }
107 if (value !== null && typeof value === 'object') {
108 r++;
109 output += '[' + code + '_' + index + r + ' key="' + key + '"]';
110 output += vs_encodeRecursive(value, depth + 1, index + +r);
111 output += '[/' + code + '_' + index + r + ']';
112
113 } else {
114 r++;
115 var index = '';
116 if (parentIndex !== 0) {
117 index = parentIndex + '_';
118 }
119 output += '[' + code + '_' + index + r + ' key="' + key + '"]';
120 output += vs_urlencode(value);
121 output += '[/' + code + '_' + index + r + ']';
122 }
123 }
124 }
125
126 return output;
127 }
128
129 return vs_encodeRecursive(option, 0, 0);
130 }
131
132 /*************************************************************************************************************************************************************************
133 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
134 vs_serializecode
135 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
136 **************************************************************************************************************************************************************************/
137 function vs_serializecode(input='', code='dt') {
138 var regex = new RegExp('\\[(' + code + '_\\d+)(_\\d+)?(?: (key)="([^"]*)")?\\](.+?(?=\\[\/\\1\\2?\\]))?', 'g');
139 var matches = input.matchAll(regex);
140 var shortcodes = {};
141
142 for (const match of matches) {
143 if (match[1] && match[1] !== '') {
144 // store child data in parent's content array
145 shortcodes[match[1]] = {
146 'name': match[1] + (match[2] || ''),
147 'key': (match[4] && match[4] !== '') ? match[4] : '',
148 'value': (match[5] && match[5] !== '') ? match[5] : ''
149 };
150 }
151 }
152
153 if (Object.keys(shortcodes).length === 0) {
154 return input;
155 }
156
157 return shortcodes;
158 }
159 /*************************************************************************************************************************************************************************
160 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
161 vs_data_decode
162 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
163 **************************************************************************************************************************************************************************/
164 function vs_data_decode(data, code) {
165 var obj = vs_serializecode(data, code);
166 var a = {};
167
168 if (typeof obj === 'object' && Object.keys(obj).length > 0) {
169 $.each(obj, function (index, value) {
170 a[value['key']] = vs_data_decode_array(value);
171
172 });
173 } else {
174
175 a = vs_urldecode(obj);
176
177 }
178 return a;
179
180 }
181 /*************************************************************************************************************************************************************************
182 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
183 vs_data_decode Array
184 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
185 **************************************************************************************************************************************************************************/
186
187 function vs_data_decode_array(v) {
188 var a = {};
189 var o_2 = vs_serializecode(v['value'], v['name']);
190 if (typeof o_2 === 'object' && Object.keys(o_2).length > 0) {
191
192 $.each(o_2, function (index, v_2) {
193 a[v_2['key']] = vs_data_decode_array(v_2);
194 });
195
196 } else {
197 a = vs_urldecode(v['value']);
198 }
199 return a;
200 }
201
202 /*************************************************************************************************************************************************************************
203 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
204 encode
205 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
206 **************************************************************************************************************************************************************************/
207 $.fn.vs_encode = function(data,code) {
208 return vs_data_encode(vs_settingEmptyAndZero(data),code );
209 };
210
211 /*************************************************************************************************************************************************************************
212 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
213 decode
214 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
215 **************************************************************************************************************************************************************************/
216 $.fn.vs_decode = function( data,code) {
217 return vs_data_decode( data,code);
218 };
219 /*************************************************************************************************************************************************************************
220 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
221 Get Value From Objects
222 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
223 **************************************************************************************************************************************************************************/
224 $.fn.vs_getValueFromObject=function(obj, str) {
225 const regex = /([^\[\]]+)|\[([^\[\]]+)\]/g;
226 let match;
227 let keys = [];
228 while ((match = regex.exec(str)) !== null) {
229 if (match[1]) {
230 keys.push(match[1]);
231 } else if (match[2]) {
232 keys.push(match[2]);
233 }
234 }
235 let current = obj;
236 for (let i = 0; i < keys.length; i++) {
237 if ( current[keys[i]] !== undefined) {
238 current = current[keys[i]];
239 } else {
240 return '';
241 }
242 }
243
244 return current;
245 }
246
247
248 })(jQuery);