1/** 2 * Copyright © Magento, Inc. All rights reserved. 3 * See COPYING.txt for license details. 4 */ 5 6define([ 7 'underscore', 8 'jquery', 9 'mage/utils/objects' 10], function (_, $, utils) { 11 'use strict'; 12 13 var defaultAttributes, 14 ajaxSettings, 15 map; 16 17 defaultAttributes = { 18 method: 'post', 19 enctype: 'multipart/form-data' 20 }; 21 22 ajaxSettings = { 23 default: { 24 method: 'POST', 25 cache: false, 26 processData: false, 27 contentType: false 28 }, 29 simple: { 30 method: 'POST', 31 dataType: 'json' 32 } 33 }; 34 35 map = { 36 'D': 'DDD', 37 'dd': 'DD', 38 'd': 'D', 39 'EEEE': 'dddd', 40 'EEE': 'ddd', 41 'e': 'd', 42 'yyyy': 'YYYY', 43 'yy': 'YY', 44 'y': 'YYYY', 45 'a': 'A' 46 }; 47 48 return { 49 50 /** 51 * Generates a unique identifier. 52 * 53 * @param {Number} [size=7] - Length of a resulting identifier. 54 * @returns {String} 55 */ 56 uniqueid: function (size) { 57 var code = Math.random() * 25 + 65 | 0, 58 idstr = String.fromCharCode(code); 59 60 size = size || 7; 61 62 while (idstr.length < size) { 63 code = Math.floor(Math.random() * 42 + 48); 64 65 if (code < 58 || code > 64) { 66 idstr += String.fromCharCode(code); 67 } 68 } 69 70 return idstr; 71 }, 72 73 /** 74 * Limits function call. 75 * 76 * @param {Object} owner 77 * @param {String} target 78 * @param {Number} limit 79 */ 80 limit: function (owner, target, limit) { 81 var fn = owner[target]; 82 83 owner[target] = _.debounce(fn.bind(owner), limit); 84 }, 85 86 /** 87 * Converts mage date format to a moment.js format. 88 * 89 * @param {String} mageFormat 90 * @returns {String} 91 */ 92 normalizeDate: function (mageFormat) { 93 var result = mageFormat; 94 95 _.each(map, function (moment, mage) { 96 result = result.replace( 97 new RegExp(mage + '(?=([^\u0027]*\u0027[^\u0027]*\u0027)*[^\u0027]*$)'), 98 moment 99 ); 100 }); 101 result = result.replace(/'(.*?)'/g, '[$1]'); 102 return result; 103 }, 104 105 /** 106 * Puts provided value in range of min and max parameters. 107 * 108 * @param {Number} value - Value to be located. 109 * @param {Number} min - Min value. 110 * @param {Number} max - Max value. 111 * @returns {Number} 112 */ 113 inRange: function (value, min, max) { 114 return Math.min(Math.max(min, value), max); 115 }, 116 117 /** 118 * Serializes and sends data via POST request. 119 * 120 * @param {Object} options - Options object that consists of 121 * a 'url' and 'data' properties. 122 * @param {Object} attrs - Attributes that will be added to virtual form. 123 */ 124 submit: function (options, attrs) { 125 var form = document.createElement('form'), 126 data = utils.serialize(options.data), 127 attributes = _.extend({}, defaultAttributes, attrs || {}); 128 129 if (!attributes.action) { 130 attributes.action = options.url; 131 } 132 133 data['form_key'] = window.FORM_KEY; 134 135 _.each(attributes, function (value, name) { 136 form.setAttribute(name, value); 137 }); 138 139 data = _.map( 140 data, 141 function (value, name) { 142 return '<input type="hidden" ' + 143 'name="' + _.escape(name) + '" ' + 144 'value="' + _.escape(value) + '"' + 145 ' />'; 146 } 147 ).join(''); 148 149 form.insertAdjacentHTML('afterbegin', data); 150 document.body.appendChild(form); 151 152 form.submit(); 153 }, 154 155 /** 156 * Serializes and sends data via AJAX POST request. 157 * 158 * @param {Object} options - Options object that consists of 159 * a 'url' and 'data' properties. 160 * @param {Object} config 161 */ 162 ajaxSubmit: function (options, config) { 163 var t = new Date().getTime(), 164 settings; 165 166 options.data['form_key'] = window.FORM_KEY; 167 options.data = this.prepareFormData(options.data, config.ajaxSaveType); 168 settings = _.extend({}, ajaxSettings[config.ajaxSaveType], options || {}); 169 170 if (!config.ignoreProcessEvents) { 171 $('body').trigger('processStart'); 172 } 173 174 return $.ajax(settings) 175 .done(function (data) { 176 if (config.response) { 177 data.t = t; 178 config.response.data(data); 179 config.response.status(undefined); 180 config.response.status(!data.error); 181 } 182 }) 183 .fail(function () { 184 if (config.response) { 185 config.response.status(undefined); 186 config.response.status(false); 187 config.response.data({ 188 error: true, 189 messages: 'Something went wrong.', 190 t: t 191 }); 192 } 193 }) 194 .always(function () { 195 if (!config.ignoreProcessEvents) { 196 $('body').trigger('processStop'); 197 } 198 }); 199 }, 200 201 /** 202 * Creates FormData object and append this data. 203 * 204 * @param {Object} data 205 * @param {String} type 206 * @returns {FormData} 207 */ 208 prepareFormData: function (data, type) { 209 var formData; 210 211 if (type === 'default') { 212 formData = new FormData(); 213 _.each(utils.serialize(data), function (val, name) { 214 formData.append(name, val); 215 }); 216 } else if (type === 'simple') { 217 formData = utils.serialize(data); 218 } 219 220 return formData; 221 }, 222 223 /** 224 * Filters data object. Finds properties with suffix 225 * and sets their values to properties with the same name without suffix. 226 * 227 * @param {Object} data - The data object that should be filtered 228 * @param {String} suffix - The string by which data object should be filtered 229 * @param {String} separator - The string that is separator between property and suffix 230 * 231 * @returns {Object} Filtered data object 232 */ 233 filterFormData: function (data, suffix, separator) { 234 data = data || {}; 235 suffix = suffix || 'prepared-for-send'; 236 separator = separator || '-'; 237 238 _.each(data, function (value, key) { 239 if (_.isObject(value) && !Array.isArray(value)) { 240 this.filterFormData(value, suffix, separator); 241 } else if (_.isString(key) && ~key.indexOf(suffix)) { 242 data[key.split(separator)[0]] = value; 243 delete data[key]; 244 } 245 }, this); 246 247 return data; 248 }, 249 250 /** 251 * Replaces special characters with their corresponding HTML entities. 252 * 253 * @param {String} string - Text to escape. 254 * @returns {String} Escaped text. 255 */ 256 escape: function (string) { 257 return string ? $('<p></p>').text(string).html().replace(/"/g, '"') : string; 258 }, 259 260 /** 261 * Replaces symbol codes with their unescaped counterparts. 262 * 263 * @param {String} data 264 * 265 * @returns {String} 266 */ 267 unescape: function (data) { 268 var unescaped = _.unescape(data), 269 mapCharacters = { 270 ''': '\'' 271 }; 272 273 _.each(mapCharacters, function (value, key) { 274 unescaped = unescaped.replace(key, value); 275 }); 276 277 return unescaped; 278 }, 279 280 /** 281 * Converts PHP IntlFormatter format to moment format. 282 * 283 * @param {String} format - PHP format 284 * @returns {String} - moment compatible formatting 285 */ 286 convertToMomentFormat: function (format) { 287 var newFormat; 288 289 newFormat = format.replace(/yyyy|yy|y/, 'YYYY'); // replace the year 290 newFormat = newFormat.replace(/dd|d/g, 'DD'); // replace the date 291 292 return newFormat; 293 }, 294 295 /** 296 * Get Url Parameters. 297 * 298 * @param {String} url - Url string 299 * @returns {Object} 300 */ 301 getUrlParameters: function (url) { 302 var params = {}, 303 queries = url.split('?'), 304 temp, 305 i, 306 l; 307 308 if (!queries[1]) { 309 return params; 310 } 311 312 queries = queries[1].split('&'); 313 314 for (i = 0, l = queries.length; i < l; i++) { 315 temp = queries[i].split('='); 316 317 if (temp[1]) { 318 params[temp[0]] = decodeURIComponent(temp[1].replace(/\+/g, '%20')); 319 } else { 320 params[temp[0]] = ''; 321 } 322 } 323 324 return params; 325 } 326 }; 327});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.