1'use strict'; 2;(function(){/************************ ATTENTION *************************** 3* Please do not use Bitrix CoreJS in this class. * 4* This class can be called on page without Bitrix Framework * 5**************************************************************/ 6if(!window.BX){window.BX = {};} 7else if(window.BX.RestClient){return;} 8var BX = window.BX; 9BX.RestClient = function (options){options = options || {};this.endpoint = options.endpoint || '/rest'; 10this.queryParams = options.queryParams || ''; 11this.cors = options.cors === true;};BX.RestClient.prototype.callMethod = function(method, params, callback, sendCallback, logTag){return ajax({ 12method: method,data: params, 13callback: callback,sendCallback: sendCallback, 14logTag: logTag,endpoint: this.endpoint, 15queryParams: this.queryParams,cors: this.cors 16});};/* 17calls = [[method,params],[method,params]]; 18calls = [{method:method,params:params},[method,params]]; 19calls = {call_id:[method,params],...};*/ 20BX.RestClient.prototype.callBatch = function(calls, callback, bHaltOnError, sendCallback, logTag){var cmd = Utils.isArray(calls) ? [] : {};var cnt = 0; 21var cb = function(cmd){ajax.batch(cmd, callback, bHaltOnError, sendCallback, this.endpoint, this.queryParams, this.cors, logTag);}.bind(this); 22for(var i in calls){var method = null, params = null; 23if(!!calls[i] && calls.hasOwnProperty(i)){if(Utils.isArray(calls[i])) 24{ 25method = calls[i][0]; 26params = calls[i][1];} 27else if(!!calls[i].method){method = calls[i].method; 28params = calls[i].params;} 29if(!!method){cnt++; 30cmd[i] = [method, params];} 31} 32} 33if(cnt > 0){var e = function(i) 34{ 35return function(str){cmd[i] = cmd[i][0] + '?' + str; 36if(--cnt <= 0) 37cb(cmd);} 38};for(var c in cmd){if(cmd.hasOwnProperty(c)) 39{ 40ajax.prepareData(cmd[c][1], '', e(c));} 41} 42} 43};BX.RestClient.prototype.setEndpoint = function(url){this.endpoint = url;};BX.RestClient.prototype.enableCorsRequest = function(value){this.cors = value === true;};BX.RestClient.prototype.setQueryParams = function(params){this.queryParams = params;};/* self init for bitrix env */ 44if(typeof BX.namespace !== 'undefined'){var BXRest = new BX.RestClient(); 45if(typeof BX.rest == 'undefined'){BX.rest = {};} 46BX.rest.callMethod = function (method, params, callback, sendCallback, logTag){return BXRest.callMethod(method, params, callback, sendCallback, logTag);};/* 47calls = [[method,params],[method,params]]; 48calls = [{method:method,params:params},[method,params]]; 49calls = {call_id:[method,params],...};*/ 50BX.rest.callBatch = function (calls, callback, bHaltOnError, sendCallback, logTag){return BXRest.callBatch(calls, callback, bHaltOnError, sendCallback, logTag);};} 51var Utils = { 52isArray: function(item){return item && Object.prototype.toString.call(item) == "[object Array]";},isFunction: function(item){return item === null ? false : (typeof (item) == "function" || item instanceof Function);}, 53isString: function(item){return item === '' ? true : (item ? (typeof (item) == "string" || item instanceof String) : false);},isDomNode: function(item){return item && typeof (item) == "object" && "nodeType" in item;}, 54isDate: function(item){return item && Object.prototype.toString.call(item) == "[object Date]";},buildQueryString: function(params){var result = ''; 55for (var key in params){if(!params.hasOwnProperty(key)) 56{ 57continue;} 58var value = params[key]; 59if(this.isArray(value)){value.forEach(function(valueElement, index) 60{ 61result += encodeURIComponent(key + "[" + index + "]") + "=" + encodeURIComponent(valueElement) + "&";});}else{result += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&";} 62} 63if(result.length > 0){result = result.substr(0, result.length - 1);} 64return result;},clone: function(obj, bCopyObj){var _obj, i, l; 65if(bCopyObj !== false) 66bCopyObj = true; 67if(obj === null) 68return null; 69if(this.isDomNode(obj)){_obj = obj.cloneNode(bCopyObj);} 70else if(typeof obj == 'object'){if(this.isArray(obj)) 71{ 72_obj = []; 73for (i=0,l=obj.length;i<l;i++){if(typeof obj[i] == "object" && bCopyObj) 74_obj[i] = this.clone(obj[i], bCopyObj); 75else 76_obj[i] = obj[i];}}else{_obj = {};if(obj.constructor){if(this.isDate(obj)) 77_obj = new Date(obj); 78else 79_obj = new obj.constructor();} 80for (i in obj){if(typeof obj[i] == "object" && bCopyObj) 81_obj[i] = this.clone(obj[i], bCopyObj); 82else 83_obj[i] = obj[i];} 84}}else{_obj = obj;} 85return _obj;} 86};var ajax = function(config){var hasCallback = !!config.callback && Utils.isFunction(config.callback); 87var promise = typeof BX.Promise === 'undefined' || hasCallback? null: new BX.Promise(); 88var sendCallback = config.sendCallback || function(){};
88var withoutRestoringCsrf = config.withoutRestoringCsrf || false; 89var loginAttempt = config.loginAttempt || 0; 90var xhr = ajax.xhr(); 91var url = config.endpoint + '/' + ajax.escape(config.method) + '.json'+(config.logTag? '?logTag='+config.logTag: ''); 92xhr.open('POST', url); 93xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 94if(config.cors){xhr.withCredentials = true;} 95var bRequestCompleted = false; 96xhr.onprogress = function(){};xhr.ontimeout = function(){};xhr.timeout = 0; 97xhr.onload = function(){if(bRequestCompleted) 98return; 99xhr.onload = function(){};var bSuccess = ajax.isSuccess(xhr); 100var status = xhr.status; 101if(bSuccess){var data = xhr.responseText; 102if(data.length > 0){try 103{ 104data = JSON.parse(data);} 105catch(e){bSuccess = false;} 106} 107if(status == 401){if(data.extended_error === 'user_not_authorized' && loginAttempt === 0) 108{ 109if(data.sessid){console.warn('BX.rest: csrf-token has expired, replace to a new token'); 110BX.message({'bitrix_sessid': data.sessid});} 111if('BXDesktopSystem' in window){console.warn('BX.rest: you are not authorized, trying to log in'); 112config.loginAttempt = 1; 113BXDesktopSystem.Login({ 114success: function(){console.warn('BX.rest: successfully logged in, repeating request'); 115if(!hasCallback){config.callback = function(result) 116{ 117if(result.error()){promise.reject(result);}else{promise.fulfill(result);} 118} 119} 120ajax(config);} 121}); 122return true;} 123}
124else if(data.sessid && !withoutRestoringCsrf){BX.message({'bitrix_sessid': data.sessid}); 125console.warn('BX.rest: your csrf-token has expired, send query with a new token'); 126config.withoutRestoringCsrf = true; 127if(!hasCallback){config.callback = function(result) 128{ 129if(result.error()){promise.reject(result);}else{promise.fulfill(result);} 130} 131} 132ajax(config); 133return true;} 134} 135else if(status == 0){data = {result: {}, error: "ERROR_NETWORK", error_description: "A network error occurred while the request was being executed."};}else{if(status == 200){if(data.length <= 0) 136{ 137data = {result: {}, error: "BLANK_ANSWER", error_description: "Empty answer with correct http code, network error possible."};} 138} 139else if(data.length <= 0){data = {result: {}, error: "BLANK_ANSWER_WITH_ERROR_CODE", error_description: 'Empty answer with error http code: '+status};} 140} 141} 142xhr = null; 143if(bSuccess){var res = new ajaxResult(data, config, status); 144if(hasCallback){config.callback.apply(window, [res]);}else{if(res.error()){promise.reject(res);} 145else 146{ 147promise.fulfill(res);} 148}}else{var res = new ajaxResult({ 149error: "ERROR_UNEXPECTED_ANSWER",error_description: "Server returned an unexpected response.", 150ex: {}}, config, 0); 151if(hasCallback){config.callback.apply(window, [res]);}else{promise.reject(res);} 152} 153};xhr.onerror = function(e){var res = new ajaxResult({ 154error: "ERROR_NETWORK",error_description: "A network error occurred while the request was being executed.", 155ex: e 156}, config, 0); 157if(hasCallback){config.callback.apply(window, [res]);}else{promise.reject(res);} 158};var query_data = ''; 159if(config.queryParams){query_data = Utils.buildQueryString(config.queryParams);} 160else if(typeof BX.bitrix_sessid !== 'undefined'){query_data = 'sessid=' + BX.bitrix_sessid();} 161if(typeof config.start !== 'undefined'){query_data += '&start=' + parseInt(config.start);} 162if(!!config.data){ajax.prepareData(config.data, '', function(res) 163{ 164query_data += '&' + res; 165xhr.send(query_data); 166sendCallback(xhr);});}else{xhr.send(query_data); 167sendCallback(xhr);} 168return hasCallback || !promise? xhr: promise;};ajax.batch = function(calls, callback, bHaltOnError, sendCallback, endpoint, queryParams, cors, logTag){return ajax({ 169method: 'batch',data: {halt: !!bHaltOnError ? 1 : 0, cmd: calls}, 170callback: function(res, config, status){if(!callback) 171{ 172return false;} 173var error = res.error(); 174var data = res.data(); 175var result = Utils.isArray(calls) ? [] : {};for(var i in calls){if(!!calls[i] && calls.hasOwnProperty(i)) 176{ 177if(Utils.isString(calls[i])){var q = calls[i].split('?');}else{q = [ 178Utils.isArray(calls[i]) ? calls[i][0] : calls[i].method,Utils.isArray(calls[i]) ? calls[i][1] : calls[i].data 179];} 180if( 181data 182&& typeof data.result !== 'undefined' 183&& ( 184typeof data.result[i] !== 'undefined' 185|| typeof data.result_error[i] !== 'undefined' 186) 187){result[i] = new ajaxResult({ 188result: typeof data.result[i] !== 'undefined' ? data.result[i] : {},error: data.result_error[i] || undefined, 189total: data.result_total[i],time: data.result_time[i], 190next: data.result_next[i] 191}, { 192method: q[0],data: q[1], 193callback: callback,endpoint: endpoint, 194queryParams: queryParams,cors: cors 195}, res.status);} 196else if(error){result[i] = new ajaxResult({ 197result: {},error: error.ex, 198total: 0 199}, { 200method: q[0],data: q[1], 201callback: callback,endpoint: endpoint, 202queryParams: queryParams,cors: cors 203}, res.status);} 204} 205} 206callback.apply(window, [result]);},sendCallback: sendCallback, 207endpoint: endpoint,queryParams: queryParams, 208cors: cors,logTag: logTag 209});};ajax.xhr = function(){return new XMLHttpRequest();};ajax.escape = function(str){return encodeURIComponent(str);};ajax.prepareData = function(arData, prefix, callback){var data = '', objects = []; 210if(Utils.isString(arData) || arData === null){callback.call(document, arData || '');}else{for(var i in arData){if(!arData.hasOwnProperty(i)) 211{ 212continue;} 213var name = ajax.escape(i); 214if(prefix) 215name = prefix + '[' + name + ']'; 216if(typeof arData[i] === 'object'){if(Utils.isArray(arData[i]) && arData[i].length <= 0) 217{ 218continue;} 219objects.push([name, arData[i]]);}else{if(data.length > 0){data += '&';} 220if(typeof arData[i] === 'boolean'){data += name + '=' + (arData[i]? 1: 0);}else{data += name + '=' + ajax.escape(arData[i]) 221} 222} 223} 224var cnt = objects.length; 225if(cnt > 0){var cb = function(str) 226{ 227data += (!!str ? '&' : '') + str;
228if(--cnt <= 0){callback.call(document, data) 229} 230};var cnt1 = cnt; 231for(var i = 0; i < cnt1; i++){if(Utils.isDomNode(objects[i][1])) 232{ 233if(objects[i][1].tagName.toUpperCase() === 'INPUT' && objects[i][1].type === 'file'){if(fileReader.canUse()) 234{ 235fileReader(objects[i][1], (function(name){return function(result) 236{ 237if(Utils.isArray(result) && result.length > 0){cb(name + '[0]=' + ajax.escape(result[0]) + '&' + name + '[1]=' + ajax.escape(result[1]));}else{cb(name + '=');} 238} 239})(objects[i][0]));} 240} 241else if(typeof objects[i][1].value !== 'undefined'){cb(objects[i][0] + '=' + ajax.escape(objects[i][1].value));}else{cb('');} 242} 243else if(Utils.isDate(objects[i][1])){cb(objects[i][0] + '=' + ajax.escape(objects[i][1].toJSON()));} 244else if(Utils.isArray(objects[i][1]) && objects[i][1].length <= 0){cb(objects[i][0] + '=');}else{ajax.prepareData(objects[i][1], objects[i][0], cb);} 245}}else{callback.call(document, data) 246} 247} 248};ajax.isSuccess = function(xhr){return typeof xhr.status === 'undefined' || (xhr.status >= 200 && xhr.status < 300) || xhr.status === 304 || xhr.status >= 400 && xhr.status < 500 || xhr.status === 1223 || xhr.status === 0;};var ajaxResult = function(answer, query, status){this.answer = answer; 249this.query = Utils.clone(query); 250this.status = status; 251if(typeof this.answer.next !== 'undefined'){this.answer.next = parseInt(this.answer.next);} 252if(typeof this.answer.error !== 'undefined'){this.answer.ex = new ajaxError(this.status, typeof this.answer.error === 'string' ? this.answer : this.answer.error) 253} 254};ajaxResult.prototype.data = function(){return this.answer.result;};ajaxResult.prototype.time = function(){return this.answer.time;};ajaxResult.prototype.error = function(){return this.answer.ex;};ajaxResult.prototype.error_description = function(){return this.answer.error_description;};ajaxResult.prototype.more = function(){return !isNaN(this.answer.next);};ajaxResult.prototype.total = function(){return parseInt(this.answer.total);};ajaxResult.prototype.next = function(cb){if(this.more()) 255{ 256this.query.start = this.answer.next; 257if(!!cb && Utils.isFunction(cb)){this.query.callback = cb;} 258return ajax(this.query);} 259return false;};var ajaxError = function(status, ex){this.status = status; 260this.ex = ex;};ajaxError.prototype.getError = function(){return this.ex;};ajaxError.prototype.getStatus = function(){return this.status;};ajaxError.prototype.toString = function(){return this.ex.error + ( 261!!this.ex.error_description 262? ': ' + this.ex.error_description 263: '' 264) + ' (' + this.status + ')';};var arrayBufferToData = function(arrayBuffer){var uint8 = new Uint8Array(arrayBuffer); 265var data = ''; 266if(typeof uint8.forEach === 'function'){uint8.forEach(function (value) 267{ 268data += String.fromCharCode(value);});}else{var length = uint8.length; 269for (var i = 0; i < length; i += 1){data += String.fromCharCode(uint8[i]);} 270} 271return btoa(data);};var fileReader = function(fileInput, cb){if(fileReader.canUse()) 272{ 273var files = fileInput.files,len = 0, 274result = fileInput.multiple ? [] : null; 275for(var i = 0, f; f = files[i]; i++){var reader = new window.FileReader(); 276reader.BXFILENAME = files[i].name; 277reader.onload = function(e){e = e || window.event; 278var res = [this.BXFILENAME, arrayBufferToData(e.target.result)]; 279if(result === null) 280result = res; 281else 282result.push(res); 283if(--len <= 0){cb(result);} 284};reader.readAsArrayBuffer(f);} 285len = i; 286if(len <= 0){cb(result);} 287} 288};fileReader.canUse = function(){return !!window.FileReader;};})();
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.