PageRenderTime 247ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/components/bitrix/sale.location.import/templates/admin/script.js

https://gitlab.com/alexprowars/bitrix
JavaScript | 432 lines | 314 code | 101 blank | 17 comment | 86 complexity | db71af7337193311a50a5472bf4f6457 MD5 | raw file
  1. BX.namespace('BX.Sale.component.location');
  2. if(typeof BX.Sale.component.location.import == 'undefined' && typeof BX.ui != 'undefined' && typeof BX.ui.widget != 'undefined'){
  3. BX.Sale.component.location.import = function(opts, nf){
  4. this.parentConstruct(BX.Sale.component.location.import, opts);
  5. BX.merge(this, {
  6. opts: { // default options
  7. url: '/somewhere.php',
  8. ajaxFlag: 'AJAX_MODE',
  9. progressWidth : 500,
  10. firstImport: false,
  11. statistics: {}
  12. },
  13. vars: { // significant variables
  14. stage: false,
  15. state: 'remote',
  16. formSubmitted: false,
  17. fileUploaded: false,
  18. awaitInterruption: false,
  19. firstImport: false,
  20. statistics: {}
  21. },
  22. ctrls: { // links to controls
  23. buttons: {}
  24. },
  25. sys: {
  26. code: 'loc-i'
  27. }
  28. });
  29. this.handleInitStack(nf, BX.Sale.component.location.import, opts);
  30. };
  31. BX.extend(BX.Sale.component.location.import, BX.ui.widget);
  32. BX.merge(BX.Sale.component.location.import.prototype, {
  33. init: function(){
  34. var so = this.opts,
  35. sv = this.vars,
  36. sc = this.ctrls,
  37. ctx = this;
  38. sv.firstImport = so.firstImport;
  39. sv.statistics = BX.clone(so.statistics);
  40. // iterator
  41. sv.iterator = new BX.iterator({
  42. source: so.url,
  43. interval: 100,
  44. waitAjaxOnStop: true,
  45. whenHit: function(result){
  46. ctx.setPercent(result.data.PERCENT);
  47. var next = result.data.NEXT_STAGE;
  48. // set message
  49. if(BX.type.isNotEmptyString(next) && sv.stage != result.data.NEXT_STAGE)
  50. ctx.setStage(result.data.NEXT_STAGE);
  51. var proceed = result.data.PERCENT < 100;
  52. if(!proceed)
  53. {
  54. if(typeof result.data.STAT != 'undefined')
  55. ctx.updateStatistics(result.data.STAT);
  56. var lastRequest = this.vars.fields;
  57. if((typeof lastRequest.OPTIONS != 'undefined' && typeof lastRequest.OPTIONS.DROP_ALL != 'undefined') || (typeof lastRequest.ONLY_DELETE_ALL != 'undefined'))
  58. sv.firstImport = false; // now it is definitely NOT a first import
  59. }
  60. return proceed;
  61. }
  62. });
  63. sv.iterator.bindEvent('set-status', function(stat){
  64. if(this.vars.fields.ONLY_DELETE_ALL == 1)
  65. {
  66. if(stat == 'R'){
  67. sc.buttons.startStop.setAttribute('disabled', 'disabled');
  68. sc.buttons.deleteAll.setAttribute('disabled', 'disabled');
  69. ctx.setCSSState('running');
  70. }else if(stat == 'S'){
  71. sc.buttons.startStop.removeAttribute('disabled');
  72. sc.buttons.deleteAll.removeAttribute('disabled');
  73. ctx.dropCSSState('running');
  74. ctx.setStage('COMPLETE_REMOVE_ALL');
  75. }
  76. }
  77. else
  78. {
  79. if(stat == 'R'){
  80. sc.buttons.startStop.value = so.messages.stop;
  81. ctx.setCSSState('running');
  82. }else if(stat == 'I'){
  83. sc.buttons.startStop.value = so.messages.stopping;
  84. sc.buttons.startStop.setAttribute('disabled', 'disabled');
  85. ctx.setStage('INTERRUPTING');
  86. sv.awaitInterruption = true;
  87. }else if(stat == 'S'){
  88. function enableButtons(){
  89. sc.buttons.startStop.value = so.messages.start;
  90. ctx.dropCSSState('running');
  91. sc.buttons.startStop.removeAttribute('disabled');
  92. ctx.setStage(sv.awaitInterruption ? 'INTERRUPTED' : 'COMPLETE');
  93. sv.awaitInterruption = false;
  94. }
  95. if(false && sv.awaitInterruption){
  96. // here we must ensure indexes restored
  97. BX.ajax({
  98. url: so.url,
  99. method: 'post',
  100. dataType: 'html',
  101. async: true,
  102. processData: true,
  103. emulateOnload: true,
  104. start: true,
  105. data: {'RESTORE_INDEXES': 1},
  106. //cache: true,
  107. onsuccess: enableButtons,
  108. onfailure: enableButtons
  109. });
  110. }else
  111. enableButtons();
  112. }
  113. }
  114. });
  115. // tree
  116. sv.tree = new BX.ui.itemTree({
  117. scope: this.getControl('location-set'),
  118. bindEvents: {
  119. 'toggle-bundle-before': function(way, controls){
  120. BX[way ? 'addClass' : 'removeClass'](controls.expander, 'expanded');
  121. }
  122. }
  123. });
  124. // file loader
  125. sc.fLoader = new BX.ui.fileAsyncLoader({
  126. scope: ctx.getControl('userfile'),
  127. url: so.pageUrl
  128. });
  129. // moving item back to its designed origin
  130. var moveInputBack = function(){
  131. var admInput = BX.findChildren(this.ctrls.inputContainer, {className: 'adm-input-file'});
  132. admInput[0].appendChild(this.ctrls.input);
  133. }
  134. sc.fLoader.bindEvent('upload-success', function(){
  135. sv.fileUploaded = true;
  136. moveInputBack.call(this);
  137. });
  138. sc.fLoader.bindEvent('upload-fail', function(){
  139. sv.fileUploaded = false;
  140. moveInputBack.call(this);
  141. });
  142. sc.buttons.startStop = this.getControl('button-start');
  143. sc.buttons.deleteAll = this.getControl('delete-all');
  144. sc.nameOfSet = this.getControl('location-set');
  145. sc.percentIndicator = this.getControl('percents', false, false, true);
  146. sc.percentGrade = this.getControl('adm-progress-bar-inner');
  147. sc.statusText = this.getControl('status-text');
  148. sc.statictics = {
  149. list: this.getControl('stat-list'),
  150. all: this.getControl('stat-all'),
  151. groups: this.getControl('stat-groups')
  152. };
  153. //this.pushFuncStack('buildUpDOM', BX.Sale.component.location.import);
  154. this.pushFuncStack('bindEvents', BX.Sale.component.location.import);
  155. },
  156. /*buildUpDOM: function(){},*/
  157. bindEvents: function(){
  158. var sc = this.ctrls,
  159. sv = this.vars,
  160. so = this.opts,
  161. ctx = this;
  162. // iterator
  163. BX.bind(sc.buttons.startStop, 'click', function(){
  164. if(sv.iterator.getIsRunning()){
  165. sv.iterator.stop();
  166. }else{
  167. // location sets
  168. var children = BX.findChildren(sc.nameOfSet, {tag: 'input'}, true);
  169. var sets = [];
  170. for(var k in children){
  171. if(children[k].checked && children[k].name.length > 0 && children[k].value.length > 0)
  172. sets.push(children[k].value);
  173. }
  174. if(sv.state == 'remote' && !sets.length)
  175. {
  176. alert(so.messages.selectItems);
  177. return;
  178. }
  179. if(sv.state == 'file' && !sv.fileUploaded)
  180. {
  181. alert(so.messages.uploadFile);
  182. return;
  183. }
  184. var request = {
  185. LOCATION_SETS: sets,
  186. ADDITIONAL: ctx.getFormControlValues('additional'),
  187. OPTIONS: ctx.getFormControlValues('option')
  188. };
  189. request[so.ajaxFlag] = 1;
  190. if(request.OPTIONS.DROP_ALL && !ctx.askRemove())
  191. return;
  192. ctx.setPercent(0);
  193. ctx.setStage('DOWNLOAD_FILES');
  194. BX.show(ctx.getControl('progressbar'));
  195. sv.iterator.start(request);
  196. }
  197. });
  198. BX.bind(sc.buttons.deleteAll, 'click', function(e){
  199. if(ctx.askRemove()){
  200. var request = {
  201. ONLY_DELETE_ALL: 1
  202. };
  203. request[so.ajaxFlag] = 1;
  204. ctx.setPercent(0);
  205. ctx.setStage('DELETE_ALL');
  206. BX.show(ctx.getControl('progressbar'));
  207. sv.iterator.start(request);
  208. }
  209. BX.PreventDefault(e);
  210. });
  211. var onError = function(errors){
  212. sc.buttons.startStop.value = so.messages.start;
  213. var errMsg = [];
  214. if(typeof errors != 'undefined'){
  215. for(var k in errors){
  216. if(errors[k].message)
  217. errMsg.push(errors[k].message);
  218. }
  219. }
  220. ctx.setStatusText(so.messages.error_occured+': '+errMsg.join(', '), true);
  221. }
  222. sv.iterator.bindEvent('server-error', onError);
  223. sv.iterator.bindEvent('ajax-error', onError);
  224. var setState = function(state, initial){
  225. sc.scope.className = '';
  226. ctx.setCSSState('mode-'+state);
  227. sv.state = state;
  228. /*
  229. if(!initial && state == 'remote'){
  230. sv.fileUploaded = false;
  231. sc.fLoader.hideMessages();
  232. }
  233. */
  234. }
  235. BX.bindDelegate(this.getControl('mode-switch'), 'change', {tag: 'input'}, function(){
  236. setState(this.value);
  237. });
  238. setState('remote', true);
  239. },
  240. askRemove: function(){
  241. var so = this.opts;
  242. if(typeof this.vars.statistics.TOTAL != 'undefined' && this.vars.statistics.TOTAL.CNT == 0)
  243. return true; // empty base
  244. var hasRelicLocations = this.vars.firstImport;
  245. var answer = confirm(so.messages[hasRelicLocations ? 'confirm_delete_relic' : 'confirm_delete']);
  246. return (hasRelicLocations && !answer) || (!hasRelicLocations && answer);
  247. },
  248. setPercent: function(value){
  249. var sc = this.ctrls,
  250. so = this.opts;
  251. value = parseInt(value);
  252. if(value < 0)
  253. value = 0;
  254. if(value > 100)
  255. value = 100;
  256. if(sc.percentIndicator != null){
  257. for(var k in sc.percentIndicator){
  258. sc.percentIndicator[k].innerHTML = value;
  259. }
  260. }
  261. value = value * (so.progressWidth / 100) - 4;
  262. if(value < 0)
  263. value = 0;
  264. BX.style(sc.percentGrade, 'width', value+'px');
  265. },
  266. setStatusText: function(text, highlight){
  267. this.ctrls.statusText.innerHTML = BX.util.htmlspecialchars(text);
  268. BX.style(this.ctrls.statusText, 'color', highlight ? 'red' : 'inherit');
  269. },
  270. setStage: function(stageCode){
  271. var so = this.opts,
  272. sv = this.vars;
  273. if(typeof so.messages['stage_'+stageCode] == 'undefined'){
  274. this.setStatusText(BX.util.htmlspecialchars(stageCode), true);
  275. sv.stage = false;
  276. return;
  277. }
  278. this.setStatusText(this.opts.messages['stage_'+stageCode], false);
  279. sv.stage = stageCode;
  280. },
  281. getFormControlValues: function(fcCode){
  282. var result = {};
  283. var controls = this.getControl(fcCode, true, false, true);
  284. for(var k = 0; k < controls.length; k++)
  285. {
  286. var opt = controls[k];
  287. if('name' in opt)
  288. {
  289. var name = opt.name;
  290. if(!('type' in opt))
  291. continue;
  292. if(opt.nodeName == 'INPUT' && (opt.type == 'checkbox' || opt.type == 'radio') && !opt.checked)
  293. continue;
  294. result[name] = opt.value;
  295. }
  296. }
  297. return result;
  298. },
  299. setTab: function(tab)
  300. {
  301. BX.style(this.ctrls.buttons.startStop, 'visibility', tab == 'tab_cleanup' ? 'hidden' : 'visible');
  302. },
  303. updateStatistics: function(stat){
  304. var sc = this.ctrls;
  305. BX.cleanNode(sc.statictics.list);
  306. var html = '';
  307. var all = 0;
  308. var groups = 0;
  309. for(var k in stat)
  310. {
  311. if(typeof this.vars.statistics[stat[k].CODE] == 'undefined')
  312. this.vars.statistics[stat[k].CODE] = {};
  313. this.vars.statistics[stat[k].CODE].CNT = stat[k].CNT;
  314. if(typeof stat[k].NAME == 'string' && stat[k].NAME.length > 0)
  315. {
  316. html += this.getHTMLByTemplate('stat-item', {
  317. type: stat[k].NAME,
  318. count: parseInt(stat[k].CNT)
  319. });
  320. }
  321. if(stat[k].CODE == 'TOTAL')
  322. all = parseInt(stat[k].CNT);
  323. if(stat[k].CODE == 'GROUPS')
  324. groups = parseInt(stat[k].CNT);
  325. }
  326. BX.html(sc.statictics.list, html);
  327. BX.html(sc.statictics.all, all);
  328. BX.html(sc.statictics.groups, groups);
  329. }
  330. });
  331. }