mgComponents.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. function getItemNamespace (elId) {
  2. return jQuery('#' + elId).attr('namespace');
  3. }
  4. function getItemIndex (elId) {
  5. return jQuery('#' + elId).attr('index');
  6. }
  7. function initMassActionsOnDatatables(elId){
  8. $('#' + elId + ' [data-check-container]').luCheckAll({
  9. onCheck: function(container, counter) {
  10. var massActions = container.find('.lu-t-c__mass-actions');
  11. massActions.find('.lu-value').html(counter);
  12. if (counter > 0) {
  13. massActions.addClass('is-open');
  14. } else {
  15. massActions.removeClass('is-open');
  16. }
  17. }
  18. });
  19. }
  20. function collectTableMassActionsData(elId){
  21. var colectedData = {};
  22. $('#' + elId + ' [data-check-container] tbody input:checkbox.table-mass-action-check:enabled:checked')
  23. .each(function(index, value){
  24. colectedData[index] = $(this).val();
  25. });
  26. return colectedData;
  27. }
  28. function uncheckSelectAllCheck(elId){
  29. closeAllSelectMassActions();
  30. }
  31. function closeAllSelectMassActions(){
  32. $('.lu-t-c__mass-actions').removeClass('is-open');
  33. $('.table-mass-action-check').prop('checked', false);
  34. $('thead input:checkbox:enabled').prop('checked', false);
  35. }
  36. function initTooltipsForDatatables(elId) {
  37. $('#' + elId + ' [data-toggle="lu-tooltip"], [data-tooltip]').luTooltip({});
  38. }
  39. function initModalSelects(){
  40. initContainerSelects('mgModalContainer');
  41. }
  42. function initContainerSelects(id) {
  43. $('#' + id + ' select:not(.ajax)').each(function(){
  44. if ($(this).attr('bi-event-change')) {
  45. var biEventAction = $(this).attr('bi-event-change');
  46. $(this).selectize({
  47. plugins: ['remove_button'],
  48. onItemAdd : function(value){
  49. if (biEventAction === 'initReloadModal') {
  50. mgPageControler.modalInstance.initReloadModal();
  51. }
  52. else if (biEventAction && typeof mgPageControler[biEventAction] === "function") {
  53. setTimeout(function(){
  54. mgPageControler[biEventAction]();
  55. }, 500);
  56. }
  57. else if (biEventAction && typeof mgPageControler.vueLoader[biEventAction] === "function") {
  58. setTimeout(function(){
  59. mgPageControler.vueLoader[biEventAction]();
  60. }, 500);
  61. } else if (biEventAction && typeof window[biEventAction] === "function") {
  62. setTimeout(function(){
  63. window[biEventAction]();
  64. }, 500);
  65. }
  66. }
  67. });
  68. } else {
  69. $(this).luSelect({});
  70. }
  71. });
  72. }
  73. function initModalSwitchEvents(){
  74. $('#mgModalContainer :checkbox').each(function(){
  75. if ($(this).attr('bi-event-change')) {
  76. var biEventAction = $(this).attr('bi-event-change');
  77. $(this).change(function() {
  78. if (biEventAction && typeof mgPageControler[biEventAction] === "function") {
  79. setTimeout(function(){
  80. mgPageControler[biEventAction]();
  81. }, 500);
  82. }
  83. else if (biEventAction && typeof mgPageControler.vueLoader[biEventAction] === "function") {
  84. setTimeout(function(){
  85. mgPageControler.vueLoader[biEventAction]();
  86. }, 500);
  87. } else if (biEventAction && typeof window[biEventAction] === "function") {
  88. setTimeout(function(){
  89. window[biEventAction]();
  90. }, 500);
  91. }
  92. });
  93. }
  94. });
  95. }
  96. function initModalTooltips(){
  97. initContainerTooltips('mgModalContainer');
  98. }
  99. function initContainerTooltips(id) {
  100. $('#' + id + ' [data-toggle="lu-tooltip"], [data-tooltip]').luTooltip({});
  101. }
  102. function mgFormControler(targetFormId) {
  103. this.fields = null;
  104. this.data = {};
  105. this.formId = targetFormId;
  106. this.formElement = null;
  107. this.formDataObj = null;
  108. this.loadFormFields = function(){
  109. var that = this;
  110. jQuery('#'+this.formId).find('input,select,textarea').each(function () {
  111. if (!jQuery(this).is(':disabled')) {
  112. var name = jQuery(this).attr('name');
  113. var value = null;
  114. if (name !== undefined) {
  115. var type = jQuery(this).attr('type');
  116. var regExp = /([a-zA-Z_0-9]+)\[([a-zA-Z_0-9]+)\]/g;
  117. var regExpLg = /([a-zA-Z_0-9]+)\[([a-zA-Z_0-9]+)\]\[([a-zA-Z_0-9]+)\]/g;
  118. if (type === 'checkbox') {
  119. var value = 'off';
  120. jQuery('#'+that.formId).find('input[name="'+name+'"]').each(function () {
  121. if (jQuery(this).is(':checked')) {
  122. value = jQuery(this).val();
  123. }
  124. });
  125. } else if (type === 'radio') {
  126. if (jQuery(this).is(':checked')) {
  127. var value = jQuery(this).val();
  128. }
  129. } else if (type === 'file') {
  130. var value ={
  131. type: 'file',
  132. value: jQuery(this).val()
  133. };
  134. } else {
  135. var value = jQuery(this).val();
  136. }
  137. if (value !== null) {
  138. if (result = regExpLg.exec(name)) {
  139. if (that.data[result[1]] === undefined) {
  140. that.data[result[1]] = {};
  141. }
  142. if (that.data[result[1]][result[2]] === undefined) {
  143. that.data[result[1]][result[2]] = {};
  144. }
  145. that.data[result[1]][result[2]][result[3]] = value;
  146. }else if (result = regExp.exec(name)) {
  147. if (that.data[result[1]] === undefined) {
  148. that.data[result[1]] = {};
  149. }
  150. that.data[result[1]][result[2]] = value;
  151. } else {
  152. that.data[name] = value;
  153. }
  154. }
  155. }
  156. }
  157. });
  158. };
  159. this.getFieldsData = function() {
  160. this.formElement = null;
  161. this.formDataObj = null;
  162. this.loadFormFields();
  163. var formatedData = new FormData();
  164. for (var key in this.data) {
  165. if (!this.data.hasOwnProperty(key)) {
  166. continue;
  167. }
  168. var KeyValue = this.data[key];
  169. if (key.indexOf('[') > 0){
  170. var formatedKey = 'formData[' + key.substring(0, key.indexOf('[')) + ']' + key.substring(key.indexOf('['));
  171. } else {
  172. var formatedKey = 'formData[' + key + ']'
  173. }
  174. if(typeof KeyValue === 'object') {
  175. if (KeyValue.type === 'file'){
  176. this.addFieldData(formatedData, key, formatedKey, KeyValue);
  177. } else{
  178. this.addIteratedData(formatedData, formatedKey, KeyValue);
  179. }
  180. } else {
  181. formatedData.set(formatedKey, KeyValue);
  182. }
  183. }
  184. return formatedData;
  185. };
  186. this.addFieldData = function(formatedData, key, formatedKey, KeyValue){
  187. if (!this.formElement) {
  188. this.formElement = document.querySelector("#" + this.formId);
  189. this.formDataObj = new FormData(this.formElement);
  190. }
  191. KeyValue = this.formDataObj.get(key);
  192. if (typeof KeyValue !== 'object') {
  193. formatedData.set(formatedKey, KeyValue);
  194. } else {
  195. formatedData.set(formatedKey, KeyValue, KeyValue.name);
  196. }
  197. };
  198. this.addIteratedData = function(formatedData, formatedKey, KeyValue){
  199. for (var key in KeyValue) {
  200. if (!KeyValue.hasOwnProperty(key)) {
  201. continue;
  202. }
  203. if (typeof KeyValue[key] === 'object') {
  204. this.addIteratedData(formatedData, formatedKey + '[' + key + ']', KeyValue[key]);
  205. } else {
  206. formatedData.set(formatedKey + '[' + key + ']', KeyValue[key]);
  207. }
  208. }
  209. };
  210. this.updateFieldsValidationMessages = function(errorsList) {
  211. jQuery('#'+this.formId).find('input,select,textarea').each(function () {
  212. if (!jQuery(this).is(':disabled')) {
  213. var name = jQuery(this).attr('name');
  214. if(name !== undefined && errorsList[name] === undefined) {
  215. if(jQuery(this).parents('.lu-form-group').first().hasClass('lu-is-error')) {
  216. jQuery(this).parents('.lu-form-group').first().removeClass('lu-is-error');
  217. }
  218. var messagePlaceholder = jQuery(this).parents('.lu-form-group').first().children('.lu-form-feedback').first();
  219. if(jQuery(messagePlaceholder).length > 0){
  220. jQuery(messagePlaceholder).html('');
  221. if(!jQuery(messagePlaceholder).attr('hidden')){
  222. jQuery(messagePlaceholder).attr('hidden', 'hidden');
  223. }
  224. }
  225. }
  226. }
  227. });
  228. jQuery('#'+this.formId).find('input,select,textarea').each(function () {
  229. if (!jQuery(this).is(':disabled')) {
  230. var name = jQuery(this).attr('name');
  231. if(name !== undefined && errorsList[name] !== undefined)
  232. {
  233. if(!jQuery(this).parents('.lu-form-group').first().hasClass('lu-is-error')) {
  234. jQuery(this).parents('.lu-form-group').first().addClass('lu-is-error');
  235. }
  236. var messagePlaceholder = jQuery(this).parents('.lu-form-group').first().children('.lu-form-feedback');
  237. if(jQuery(messagePlaceholder).length > 0)
  238. {
  239. jQuery(messagePlaceholder).html(errorsList[name].slice(-1)[0]);
  240. if(jQuery(messagePlaceholder).attr('hidden')){
  241. jQuery(messagePlaceholder).removeAttr('hidden');
  242. }
  243. }
  244. }
  245. }
  246. });
  247. };
  248. };
  249. //Sortable
  250. function tldCategoriesSortableController()
  251. {
  252. var helperHeight = 0;
  253. //Add sortable for parent categories
  254. if (! $('#groupList.vSortable').hasClass('ui-sortable'))
  255. {
  256. $("#groupList.vSortable").sortable(
  257. {
  258. items: "li:not(.lu-nav--sub li, .sortable-disabled)",
  259. start: function(event, ui)
  260. {
  261. $(ui.item).find("ul").hide();
  262. $("#groupList").attr("isBeingSorted", "true");
  263. },
  264. stop: function(event, ui)
  265. {
  266. var order = [];
  267. $("#groupList .nav__item").each(function(index, element)
  268. {
  269. if($(element).hasClass("ui-sortable-placeholder"))
  270. {
  271. return;
  272. }
  273. var catId = $(element).attr("actionid");
  274. order.push(catId);
  275. });
  276. mgPageControler.vueLoader.updateSorting(order, 'addCategoryForm', 'ModulesGarden_ZimbraEmail_App_UI_Widget_DoeTldConfigComponents_CategoryForms_AddCategoryForm');
  277. $(ui.item).css("height", helperHeight);
  278. $(ui.item).find("a").css("height", 32);
  279. $(ui.item).find("ul").show();
  280. },
  281. sort: function(event, ui)
  282. {
  283. $( "#groupList" ).sortable( "refreshPositions" );
  284. },
  285. helper: function(event, li)
  286. {
  287. helperHeight = $(li).css("height");
  288. $(li).css("height", 32);
  289. return li;
  290. },
  291. });
  292. }
  293. //Add sortable for children - this has to be refreshed per catego content load
  294. $("#groupList.vSortable .nav--sub").sortable(
  295. {
  296. stop: function(event, ui)
  297. {
  298. var order = [];
  299. $(this).find(".nav__item").each(function(index, element)
  300. {
  301. if($(element).hasClass("ui-sortable-placeholder"))
  302. {
  303. return;
  304. }
  305. var catId = $(element).attr("actionid");
  306. order.push(catId);
  307. });
  308. mgPageControler.vueLoader.updateSorting(order, 'addCategoryForm', 'ModulesGarden_ZimbraEmail_App_UI_Widget_DoeTldConfigComponents_CategoryForms_AddCategoryForm');
  309. },
  310. });
  311. //Add Sortable on table
  312. $('#itemContentContainer tbody.vSortable').sortable(
  313. {
  314. stop: function(event, ui)
  315. {
  316. var order = [];
  317. $("#itemContentContainer tbody").find("tr").each(function(index, element)
  318. {
  319. if($(element).hasClass("ui-sortable-placeholder"))
  320. {
  321. return;
  322. }
  323. var catId = $(element).attr("actionid");
  324. order.push(catId);
  325. });
  326. mgPageControler.vueLoader.updateSorting(order, 'assignTldForm', 'ModulesGarden_ZimbraEmail_App_UI_Widget_DoeTldConfigComponents_CategoryForms_AssignTldForm');
  327. },
  328. helper: function(e, tr)
  329. {
  330. var $originals = tr.children();
  331. var $helper = tr.clone();
  332. $helper.children().each(function(index)
  333. {
  334. $(this).width($originals.eq(index).width()+100);
  335. });
  336. return $helper;
  337. },
  338. });
  339. }
  340. // CUSTOM FUNCTIONS
  341. //this is example custom action, use it for non-ajax actions
  342. function custAction1(vueControler, params, event){
  343. console.log('custAction1', vueControler, params, event);
  344. }
  345. //this is example custom action, use it for ajax actions
  346. function custAction2(vueControler, params, event){
  347. console.log('custAction2', vueControler, params, event);
  348. }
  349. function mgEmptyToPause(name, row) {
  350. if (!row[name] || row[name] === '') {
  351. return '-';
  352. }
  353. else {
  354. return row[name];
  355. }
  356. }
  357. function newCall(data) {
  358. console.log(data);
  359. }
  360. function buildOptionTag(text, value, selected, disabled) {
  361. var option = document.createElement("option");
  362. option.text = text;
  363. option.value = value;
  364. if (selected) {
  365. option.setAttribute('selected', 'selected');
  366. }
  367. if (disabled) {
  368. option.setAttribute('disabled', 'disabled');
  369. }
  370. return option;
  371. }
  372. function initTaggersForContainer(containerId){
  373. jQuery('#' + containerId + ' .lu-tagger').each(function()
  374. {
  375. if(!jQuery(this).hasClass('selectize-input'))
  376. {
  377. $(this).selectize({
  378. plugins: ['restore_on_backspace', 'remove_button'],
  379. delimiter: ',',
  380. persist: false,
  381. create: function(input) {
  382. return {
  383. value: input,
  384. text: input
  385. }
  386. }
  387. });
  388. }
  389. });
  390. }
  391. function initModalTaggers() {
  392. initTaggersForContainer('mgModalContainer');
  393. }
  394. function initAppTaggers() {
  395. initTaggersForContainer('layers');
  396. }
  397. mgEventHandler.on('AppCreated', null, function(id, params){
  398. setTimeout(function() {
  399. initAppTaggers();
  400. }, 100);
  401. }, 900);