mgComponents.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. {
  216. if(!jQuery(this).parents('.lu-form-group').first().hasClass('lu-is-error')) {
  217. jQuery(this).parents('.lu-form-group').first().addClass('lu-is-error');
  218. }
  219. var messagePlaceholder = jQuery(this).parents('.lu-form-group').first().children('.lu-form-feedback');
  220. if(jQuery(messagePlaceholder).length > 0)
  221. {
  222. jQuery(messagePlaceholder).html(errorsList[name].slice(-1)[0]);
  223. if(jQuery(messagePlaceholder).attr('hidden')){
  224. jQuery(messagePlaceholder).removeAttr('hidden');
  225. }
  226. }
  227. }else if(name !== undefined) {
  228. if(jQuery(this).parents('.lu-form-group').first().hasClass('lu-is-error')) {
  229. jQuery(this).parents('.lu-form-group').first().removeClass('lu-is-error');
  230. }
  231. var messagePlaceholder = jQuery(this).parents('.lu-form-group').first().children('.lu-form-feedback');
  232. if(jQuery(messagePlaceholder).length > 0){
  233. jQuery(messagePlaceholder).html('');
  234. if(!jQuery(messagePlaceholder).attr('hidden')){
  235. jQuery(messagePlaceholder).attr('hidden', 'hidden');
  236. }
  237. }
  238. }
  239. }
  240. });
  241. };
  242. };
  243. //Sortable
  244. function tldCategoriesSortableController()
  245. {
  246. var helperHeight = 0;
  247. //Add sortable for parent categories
  248. if (! $('#groupList.vSortable').hasClass('ui-sortable'))
  249. {
  250. $("#groupList.vSortable").sortable(
  251. {
  252. items: "li:not(.lu-nav--sub li, .sortable-disabled)",
  253. start: function(event, ui)
  254. {
  255. $(ui.item).find("ul").hide();
  256. $("#groupList").attr("isBeingSorted", "true");
  257. },
  258. stop: function(event, ui)
  259. {
  260. var order = [];
  261. $("#groupList .nav__item").each(function(index, element)
  262. {
  263. if($(element).hasClass("ui-sortable-placeholder"))
  264. {
  265. return;
  266. }
  267. var catId = $(element).attr("actionid");
  268. order.push(catId);
  269. });
  270. mgPageControler.vueLoader.updateSorting(order, 'addCategoryForm', 'ThurData_KerioEmail_App_UI_Widget_DoeTldConfigComponents_CategoryForms_AddCategoryForm');
  271. $(ui.item).css("height", helperHeight);
  272. $(ui.item).find("a").css("height", 32);
  273. $(ui.item).find("ul").show();
  274. },
  275. sort: function(event, ui)
  276. {
  277. $( "#groupList" ).sortable( "refreshPositions" );
  278. },
  279. helper: function(event, li)
  280. {
  281. helperHeight = $(li).css("height");
  282. $(li).css("height", 32);
  283. return li;
  284. },
  285. });
  286. }
  287. //Add sortable for children - this has to be refreshed per catego content load
  288. $("#groupList.vSortable .nav--sub").sortable(
  289. {
  290. stop: function(event, ui)
  291. {
  292. var order = [];
  293. $(this).find(".nav__item").each(function(index, element)
  294. {
  295. if($(element).hasClass("ui-sortable-placeholder"))
  296. {
  297. return;
  298. }
  299. var catId = $(element).attr("actionid");
  300. order.push(catId);
  301. });
  302. mgPageControler.vueLoader.updateSorting(order, 'addCategoryForm', 'ThurData_KerioEmail_App_UI_Widget_DoeTldConfigComponents_CategoryForms_AddCategoryForm');
  303. },
  304. });
  305. //Add Sortable on table
  306. $('#itemContentContainer tbody.vSortable').sortable(
  307. {
  308. stop: function(event, ui)
  309. {
  310. var order = [];
  311. $("#itemContentContainer tbody").find("tr").each(function(index, element)
  312. {
  313. if($(element).hasClass("ui-sortable-placeholder"))
  314. {
  315. return;
  316. }
  317. var catId = $(element).attr("actionid");
  318. order.push(catId);
  319. });
  320. mgPageControler.vueLoader.updateSorting(order, 'assignTldForm', 'ThurData_KerioEmail_App_UI_Widget_DoeTldConfigComponents_CategoryForms_AssignTldForm');
  321. },
  322. helper: function(e, tr)
  323. {
  324. var $originals = tr.children();
  325. var $helper = tr.clone();
  326. $helper.children().each(function(index)
  327. {
  328. $(this).width($originals.eq(index).width()+100);
  329. });
  330. return $helper;
  331. },
  332. });
  333. }
  334. // CUSTOM FUNCTIONS
  335. //this is example custom action, use it for non-ajax actions
  336. function custAction1(vueControler, params, event){
  337. console.log('custAction1', vueControler, params, event);
  338. }
  339. //this is example custom action, use it for ajax actions
  340. function custAction2(vueControler, params, event){
  341. console.log('custAction2', vueControler, params, event);
  342. }
  343. function mgEmptyToPause(name, row) {
  344. if (!row[name] || row[name] === '') {
  345. return '-';
  346. }
  347. else {
  348. return row[name];
  349. }
  350. }
  351. function newCall(data) {
  352. console.log(data);
  353. }
  354. function buildOptionTag(text, value, selected, disabled) {
  355. var option = document.createElement("option");
  356. option.text = text;
  357. option.value = value;
  358. if (selected) {
  359. option.setAttribute('selected', 'selected');
  360. }
  361. if (disabled) {
  362. option.setAttribute('disabled', 'disabled');
  363. }
  364. return option;
  365. }
  366. function initTaggersForContainer(containerId){
  367. jQuery('#' + containerId + ' .lu-tagger').each(function()
  368. {
  369. if(!jQuery(this).hasClass('selectize-input'))
  370. {
  371. $(this).selectize({
  372. plugins: ['restore_on_backspace'],
  373. delimiter: ',',
  374. persist: false,
  375. create: function(input) {
  376. return {
  377. value: input,
  378. text: input
  379. }
  380. }
  381. });
  382. }
  383. });
  384. }
  385. function initModalTaggers() {
  386. initTaggersForContainer('mgModalContainer');
  387. }
  388. function initAppTaggers() {
  389. initTaggersForContainer('layers');
  390. }
  391. mgEventHandler.on('AppCreated', null, function(id, params){
  392. setTimeout(function() {
  393. initAppTaggers();
  394. }, 100);
  395. }, 900);