zentaobiz.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. $(document).ready(function()
  2. {
  3. var pivot = DataStorage.pivot;
  4. if(!pivot.settings) pivot.settings = {columns: [{field: '', stat: '', slice: 'noSlice', showMode: 'default', showTotal: 'noShow'}], summary: true};
  5. if(!pivot.filters || !(pivot.filters instanceof Array)) pivot.filters = [];
  6. DataStorage.pivot = pivot;
  7. switchStep(DataStorage.step);
  8. $('body').resize(() => {calcDesignGrowFilter();});
  9. });
  10. /**
  11. * Tab click callback.
  12. *
  13. * @param object evt
  14. * @param int nextStep
  15. * @access public
  16. * @return void
  17. */
  18. function tabClick(evt, nextStep)
  19. {
  20. if($(evt).hasClass('disabled') || step == nextStep) return;
  21. var step = DataStorage.clone('step');
  22. if(nextStep < step)
  23. {
  24. DataStorage.step = nextStep;
  25. switchStep(nextStep);
  26. return;
  27. }
  28. finishStep(step, nextStep, function(nextStep)
  29. {
  30. DataStorage.step = nextStep;
  31. switchStep(nextStep);
  32. });
  33. }
  34. /**
  35. * Next step click callback.
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. function nextStep()
  41. {
  42. var step = DataStorage.clone('step');
  43. finishStep(step, step + 1, function(nextStep)
  44. {
  45. DataStorage.step = nextStep;
  46. switchStep(nextStep);
  47. });
  48. }
  49. /**
  50. * Submit publish.
  51. *
  52. * @access public
  53. * @return void
  54. */
  55. function publish(stage = 'published')
  56. {
  57. var pivot = DataStorage.pivot;
  58. var savedata = JSON.parse(JSON.stringify(pivot));
  59. var isPublished = savedata.stage == 'published';
  60. savedata.stage = stage;
  61. if(stage == 'published' && !validate()) return;
  62. if(stage == 'draft')
  63. {
  64. var newSql = $('#sql').val().trim();
  65. var hasSettings = checkSettings();
  66. var sqlChange = savedata.sql.length > 0 && newSql !== savedata.sql;
  67. if(sqlChange && hasSettings)
  68. {
  69. bootbox.confirm(clearSettingsLang, function(res)
  70. {
  71. if(res)
  72. {
  73. /* Init pivot design settings */
  74. savedata.settings = {columns: [{field: '', stat: '', slice: 'noSlice', showMode: 'default', showTotal: 'noShow'}]};
  75. /* Init filter settings */
  76. savedata.filters = savedata.filters.filter(function(filter){return filter.from == 'query';});
  77. if(pivot.used)
  78. {
  79. bootbox.confirm({message:confirmDraft,callback:save.bind(null, savedata)});
  80. }
  81. else
  82. {
  83. if(isPublished) bootbox.confirm({message:pivotLang.draftSave,callback:save.bind(null, savedata)});
  84. if(!isPublished) save(savedata);
  85. }
  86. }
  87. });
  88. }
  89. else
  90. {
  91. if(pivot.used)
  92. {
  93. bootbox.confirm({message:confirmDraft,callback:save.bind(null, savedata)});
  94. }
  95. else
  96. {
  97. if(isPublished) bootbox.confirm({message:pivotLang.draftSave,callback:save.bind(null, savedata)});
  98. if(!isPublished) save(savedata);
  99. }
  100. }
  101. return;
  102. }
  103. if(pivot.used) bootbox.confirm({message:confirmPublish,callback:save.bind(null, savedata)});
  104. if(!pivot.used) save(savedata);
  105. }
  106. function save(savedata, stus = true)
  107. {
  108. if(!stus || DataStorage.saving) return;
  109. DataStorage.saving = true;
  110. savedata.sql = $('#step1Content #sql').val().trim();
  111. savedata.settings.lastStep = DataStorage.step;
  112. savedata.fields = Object.keys(DataStorage.fields);
  113. savedata.fieldSettings = DataStorage.fieldSettings;
  114. savedata.langs = DataStorage.langs;
  115. $.post(createLink('pivot', 'design', 'pivotID=' + savedata.id + '&from=' + from + '&params=' + params), savedata, function(response)
  116. {
  117. response = JSON.parse(response);
  118. if(response.result == 'success')
  119. {
  120. zuiMessage(response.result, response.message);
  121. DataStorage.pivot = savedata;
  122. setTimeout(function()
  123. {
  124. window.location.href = response.locate;
  125. DataStorage.saving = false;
  126. }, 2000);
  127. }
  128. else
  129. {
  130. DataStorage.saving = false;
  131. /* The result.message is just a string. */
  132. if($.type(response.message) == 'string')
  133. {
  134. if($('#responser').length == 0) return alert(response.message);
  135. return $('#responser').html(response.message).addClass('text-error').show().delay(3000).fadeOut(100);
  136. }
  137. /* The result.message is just a object. */
  138. if($.type(response.message) == 'object')
  139. {
  140. var triggered = new Array();
  141. $.each(response.message, function(key, value)
  142. {
  143. /* Define the id of the error objecjt and it's label. */
  144. var errorOBJ = '#legendBasicInfo #' + key;
  145. var errorLabel = key + 'Label';
  146. var i = triggered.push(false);
  147. /* Create the error message. */
  148. var errorMSG = '<div id="' + errorLabel + '" class="text-danger red">';
  149. errorMSG += $.type(value) == 'string' ? value : value.join(';');
  150. errorMSG += '</div>';
  151. /* Append error message, set style and set the focus events. */
  152. $('#' + errorLabel).remove();
  153. var isInputGroup = $(errorOBJ).closest('.input-group').size();
  154. var $showOBJ = isInputGroup ? $(errorOBJ).closest('.input-group').parent() : $(errorOBJ).parent();
  155. $showOBJ.append(errorMSG);
  156. $(errorOBJ).css('margin-bottom', 0);
  157. $(errorOBJ).css('border-color','#D2322D');
  158. if($(errorOBJ + '_chosen').size() > 0)
  159. {
  160. $(errorOBJ + '_chosen .chosen-single').css('border-color','#D2322D');
  161. $(errorOBJ + '_chosen').bind('keydown mousedown', function()
  162. {
  163. if(!triggered[i])
  164. {
  165. $(this).removeAttr('style');
  166. $('#' + errorLabel).remove();
  167. triggered[i] = true;
  168. }
  169. });
  170. }
  171. $(errorOBJ).bind('keydown mousedown', function()
  172. {
  173. if(!triggered[i])
  174. {
  175. $(this).removeAttr('style');
  176. $('#' + errorLabel).remove();
  177. triggered[i] = true;
  178. }
  179. });
  180. if(i == 1)$(errorOBJ).focus();
  181. })
  182. }
  183. }
  184. });
  185. }
  186. /**
  187. * Finish current step and check.
  188. *
  189. * @param int step
  190. * @access public
  191. * @return void
  192. */
  193. function finishStep(step, nextStep, callback)
  194. {
  195. if(step == 1)
  196. {
  197. var check = checkStep1();
  198. if(!check) return false;
  199. if(methods.step1.query.needEditQuery()) return false;
  200. var pivot = DataStorage.clone('pivot');
  201. /* Update sql query values */
  202. getFieldSettings();
  203. var newSql = $('#sql').val().trim();
  204. /* Fix bug #32348. */
  205. newSql = newSql.replace(';', '');
  206. pivot.sql = pivot.sql.replace(';', '');
  207. var hasSettings = checkSettings();
  208. newSqlNoBr = newSql.replace(/\r\n/g, ' ');
  209. newSqlNoBr = newSqlNoBr.replace(/\n/g, ' ');
  210. oldSqlNoBr = pivot.sql.replace(/\r\n/g, ' ');
  211. oldSqlNoBr = oldSqlNoBr.replace(/\n/g, ' ');
  212. pivot.sqlChange = oldSqlNoBr.length > 0 && newSqlNoBr !== oldSqlNoBr;
  213. if(pivot.sqlChange && hasSettings)
  214. {
  215. bootbox.confirm(resetSettingsLang, function(res)
  216. {
  217. if(res)
  218. {
  219. query(function()
  220. {
  221. /* Remove step active. */
  222. $('#step1').nextAll('.step-tab').removeClass('active');
  223. /* Set pivot refreshTab for function getFinish. */
  224. pivot.refreshTab = true;
  225. pivot.stage = 'draft';
  226. /* If filterType is not query Init filter settings */
  227. if(!DataStorage.isQueryFilter()) pivot.filters = [];
  228. /* Init pivot design settings */
  229. pivot.settings = {columns: [{field: '', stat: '', slice: 'noSlice', showMode: 'default', showTotal: 'noShow'}], filterType: pivot.settings.filterType, columnTotal: 'noShow'};
  230. /* Remove reportData table and show empty tip. */
  231. $('#datagridSpanExample2').find('.reportData').remove();
  232. $('#datagrid-tip').removeClass('hidden');
  233. pivot.sql = newSql;
  234. pivot.fields = Object.keys(DataStorage.fields);
  235. pivot.fieldSettings = DataStorage.fieldSettings;
  236. DataStorage.pivot = pivot;
  237. if(callback instanceof Function) callback(2);
  238. });
  239. }
  240. });
  241. return false;
  242. }
  243. pivot.sql = newSql;
  244. pivot.fields = Object.keys(DataStorage.fields);
  245. pivot.fieldSettings = DataStorage.fieldSettings;
  246. DataStorage.pivot = pivot;
  247. }
  248. if(step == 2)
  249. {
  250. var check = checkStep2();
  251. if(!check) return false;
  252. }
  253. if(callback instanceof Function) callback(nextStep);
  254. }
  255. /**
  256. * Get current finish step stage.
  257. *
  258. a @access public
  259. * @return int
  260. */
  261. function getFinish()
  262. {
  263. var pivot = DataStorage.clone('pivot');
  264. if('refreshTab' in pivot)
  265. {
  266. delete pivot.refreshTab;
  267. DataStorage.pivot = pivot;
  268. return 1;
  269. }
  270. if(pivot.stage == 'published' || pivot.stage == 4) return 4;
  271. if(checkStep2(false)) return 2;
  272. if(checkStep1(false)) return 1;
  273. return 0;
  274. }
  275. function checkSettings()
  276. {
  277. var pivot = DataStorage.pivot;
  278. for(var key in pivot.settings)
  279. {
  280. if(key.indexOf('group') != '-1' && pivot.settings[key].length > 0) return true;
  281. if(pivot.settings[key].length > 0) return true;
  282. if(key == 'columns')
  283. {
  284. var columns = pivot.settings.columns;
  285. if(columns.length > 1) return true;
  286. if(columns.length == 1 && columns[0].field.length > 0) return true;
  287. }
  288. }
  289. return false;
  290. }
  291. /**
  292. * Check step 1 is finished.
  293. *
  294. * @param bool page
  295. * @access public
  296. * @return bool
  297. */
  298. function checkStep1(page = true)
  299. {
  300. var pivot = DataStorage.pivot;
  301. if(!page)
  302. {
  303. var sql = pivot.sql;
  304. var fields = DataStorage.fields;
  305. var fieldSettings = DataStorage.fieldSettings;
  306. return sql && fieldSettings && fields && sql.length > 0 && Object.keys(fieldSettings).length > 0 && Object.keys(fields).length > 0;
  307. }
  308. $.ajaxSettings.async = false;
  309. query();
  310. $.ajaxSettings.async = true;
  311. return $('#dataform .error').length == $('#dataform .error.hidden').length;
  312. }
  313. /**
  314. * Check step 2 is finished.
  315. *
  316. * @param bool page
  317. * @access public
  318. * @return bool
  319. */
  320. function checkStep2(page = true)
  321. {
  322. var pivot = DataStorage.pivot;
  323. if(!page)
  324. {
  325. var settings = pivot.settings;
  326. return settings && validate();
  327. }
  328. return validate(true);
  329. }
  330. function switchStep(step)
  331. {
  332. setStepActive(step);
  333. setStepEnable(step);
  334. var pivot = DataStorage.pivot;
  335. if(step == 1)
  336. {
  337. filters = pivot.filters;
  338. if(filters.length > 0 && filters[0].from != 'query')
  339. {
  340. $('#step1Content .add-query-filter').addClass('disabled').attr('disabled', true).attr('title', cannotAddQuery);
  341. }
  342. else
  343. {
  344. $('#step1Content .add-query-filter').removeClass('disabled').attr('disabled', false).removeAttr('title');
  345. }
  346. methods.step1.query.renderFilters();
  347. return;
  348. }
  349. if(step == 2 && Object.keys(DataStorage.fields).length > 0)
  350. {
  351. $.post(createLink('pivot', 'ajaxGetFieldSelect'),
  352. {
  353. fields: DataStorage.fieldSettings,
  354. langs: DataStorage.langs
  355. }, function(data)
  356. {
  357. $('#step2Content #fieldSelectTpl').html(data);
  358. initStep2();
  359. });
  360. }
  361. if(step != 1) apply(false);
  362. if(step == 2) initSummary();
  363. if(step == 2 || step == 3 || step == 4) renderFilters(step);
  364. }
  365. function initStep2()
  366. {
  367. initGroupForm();
  368. renderColumns();
  369. }
  370. /**
  371. * Set step tab label active.
  372. *
  373. * @param int step
  374. * @access public
  375. * @return void
  376. */
  377. function setStepActive(step)
  378. {
  379. var pivot = DataStorage.pivot;
  380. var lastStep = pivot.settings.lastStep;
  381. /* Remove old class and Add new active class. */
  382. //$('.step-tab').removeClass('active current');
  383. $('.step-tab').removeClass('current');
  384. $('#step' + step).addClass('active current');
  385. $('#step' + step).prevAll('.step-tab').addClass('active');
  386. $('.step-content').hide();
  387. $('#step' + step + 'Content').show();
  388. }
  389. /**
  390. * Set step tab label enable.
  391. *
  392. * @access public
  393. * @return void
  394. */
  395. function setStepEnable(step)
  396. {
  397. var stus = [];
  398. var finish = getFinish();
  399. if(finish == 0) stus = [1, 1, 0, 0];
  400. if(finish == 1 && step == 1) stus = [1, 1, 0, 0];
  401. if(finish == 1 && step == 2) stus = [1, 1, 1, 0];
  402. if(finish == 2 || finish == 4) stus = [1, 1, 1, 1];
  403. $('.step-tab').removeClass('disabled');
  404. $('.step-tab').removeClass('clickable');
  405. stus.forEach(function(value, step)
  406. {
  407. var disable = value == 1 ? '' : 'disabled';
  408. $('#step' + (step + 1)).addClass(disable);
  409. /* If step haven't active and disabled, means it hasn't be clicked to set active, but it's clickable now. */
  410. if(!$('#step' + (step + 1)).hasClass('active') && !$('#step' + (step + 1)).hasClass('disabled')) $('#step' + (step + 1)).addClass('clickable')
  411. });
  412. }
  413. function exportData()
  414. {
  415. var step = DataStorage.clone('step');
  416. var $domObj = $('#datagridSpanExample' + step).find(".table-condensed")[0];
  417. exportFile($domObj);
  418. }
  419. function initSummary()
  420. {
  421. var pivot = DataStorage.clone('pivot');
  422. var isChecked = true;
  423. if("summary" in pivot.settings && pivot.settings['summary'] == 'notuse') isChecked = false;
  424. if(isChecked) $("#summary").prop("checked", true);
  425. setSummaryForm(isChecked);
  426. }
  427. function changeSummary()
  428. {
  429. var isChecked = $("#summary").prop("checked");
  430. var pivot = DataStorage.clone('pivot');
  431. pivot.settings['summary'] = isChecked ? 'use' : 'notuse';
  432. DataStorage.pivot = pivot;
  433. setSummaryForm(isChecked);
  434. }
  435. function setSummaryForm(isChecked)
  436. {
  437. if(!isChecked)
  438. {
  439. $('#summaryForm').addClass('hidden');
  440. $('.btn-save-setting').addClass('hidden');
  441. apply();
  442. }
  443. else
  444. {
  445. $('#summaryForm').removeClass('hidden');
  446. $('.btn-save-setting').removeClass('hidden');
  447. }
  448. }