mgLibs.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. function replaceEmptyRowDataTable()
  2. {
  3. var emptyRow = $('.dataTables_empty');
  4. if (emptyRow.length == 1) {
  5. $('.dataTables_info').hide();
  6. emptyRow.text($('.dataTables_info').text());
  7. } else {
  8. $('.dataTables_info').show();
  9. }
  10. }
  11. (function(jQuery) { //TODO: na klase
  12. var $last_obj;
  13. filesToUpload = null;
  14. $(document).ready(function() {
  15. turnOnTooltips();
  16. select2Create();
  17. turnOnIcheck();
  18. });
  19. $(document).delegate('.mg-wrapper [data-seturlto]', 'mousedown', function(e) {
  20. if( e.which == 2 ) {
  21. openInNewTab($(this).data('seturlto'));
  22. }
  23. });
  24. $(document).delegate('.mg-wrapper :checkbox', 'ifChanged', function() {
  25. $(this).trigger('change');
  26. });
  27. $(document).delegate('.mg-wrapper select.select2, .mg-wrapper select.select2x', 'select2-selected', function(e) {
  28. if($(e.choice.element).data('act'))
  29. doAction($(e.choice.element));
  30. });
  31. $(document).delegate('#MGConfirmationModal [data-confirm]', 'click', function(e) {
  32. doAction($last_obj);
  33. });
  34. $(document).delegate('.mg-wrapper [data-act]', 'click', function() {
  35. $last_obj = $(this);
  36. if($last_obj.data('confirm-body')) {
  37. if($last_obj.data('confirm-title'))
  38. $('#MGConfirmationModal .modal-title h1').html($last_obj.data('confirm-title'));
  39. else
  40. $('#MGConfirmationModal .modal-title').html($('#MGConfirmationModal .default-title').html());
  41. $('#MGConfirmationModal .modal-body').html($last_obj.data('confirm-body'));
  42. var btn_class = $last_obj.attr("class").match(/btn-([\w-]*)\b/);
  43. if(btn_class)
  44. $('#MGConfirmationModal .modal-footer button').last().removeClass().addClass('btn').addClass('btn-' + btn_class[1] + ' btn-inverse');
  45. else
  46. $('#MGConfirmationModal .modal-footer button').last().removeClass().addClass('btn').addClass('btn-danger btn-inverse');
  47. $('#MGConfirmationModal').modal();
  48. $('body > .modal-backdrop').last().css('z-index', '1050');//.css('height', '100vh');
  49. } else {
  50. doAction($(this));
  51. }
  52. });
  53. function doAction($this) {
  54. var input_data = [];
  55. var modalobj = false;
  56. var validate = $this.data('no-validate') ? false : true;
  57. if($this.data('seturlto')) {
  58. changeActiveNavbar($this.data('seturlto'));
  59. if(typeof (window.history['pushState']) == "function") {
  60. try {
  61. window.history.pushState({}, 'WHMCS - DNS Manager', $this.data('seturlto'));
  62. var arr = queryToAssociativeArray($this.data('seturlto').split("?", 2).pop());
  63. JSONParser.currentPage = arr['mg-page']?arr['mg-page']:'dashboard';
  64. } catch (e) {
  65. window.location.href = $this.data('seturlto');
  66. return false;
  67. }
  68. } else {
  69. window.location.href = $this.data('seturlto');
  70. }
  71. }
  72. if($this.parents('.modal').length > 0) {
  73. modalobj = $this.parents('.modal').first();
  74. if(!$this.data('formid')) {
  75. var actions = ['addNewRecord', 'editZoneSave', 'addNewRecordSave'];
  76. var filterRecords = jQuery.inArray($this.data('act'), actions) > -1 ? true : false;
  77. input_data = $.merge(input_data, $(':input', modalobj).serializeArrayWithTurnedOffCheckboxes(filterRecords));
  78. if(validate && !isFormValidate(modalobj)) {
  79. return 0;
  80. }
  81. }
  82. }
  83. if($this.data('query')) {
  84. input_data = $.merge(input_data, queryToArray($this.data('query')));
  85. }
  86. if($this.data('formid')) {
  87. var selector = '#' + $this.data('formid');
  88. if($this.data('dt')) {
  89. selector = $('#' + $this.data('formid')).DataTable().rows().nodes();
  90. }
  91. input_data = $.merge(input_data, $(':input', selector).serializeArrayWithTurnedOffCheckboxes());
  92. if(validate && !isFormValidate($('#' + $this.data('formid')))) {
  93. return 0;
  94. }
  95. }
  96. hideTooltips();
  97. var actions = $this.data('act').split('|');
  98. actions.forEach(function(action){
  99. JSONParser.request(action, input_data, function(data) {
  100. runGlobalFunctionFromString($this.data("callback"), data.response, $this);
  101. runGlobalFunctionFromString(action + 'Callback', data.response, $this);
  102. if(data.modal) {
  103. if(modalobj) {
  104. removeVisibleModal();
  105. }
  106. modalobjtemp = $('#MGModal').html(data.modal).find('.modal');
  107. modalobjtemp.modal('show');
  108. if(modalobj)
  109. modalobj = modalobjtemp;
  110. }
  111. if(modalobj) {
  112. if(data.errors.length || typeof $this.data('do-not-close-the-modal') != 'undefined') {
  113. if($('.mg-alerts', modalobj).length == 0) $('.modal-body', modalobj).prepend('<div class="mg-alerts"></div>');
  114. showAlerts(data.errors, data.infos, data.warnings, $('.mg-alerts', modalobj).first());
  115. } else {
  116. modalobj.modal('hide');
  117. showAlerts(data.errors, data.infos, data.warnings);
  118. }
  119. } else {
  120. showAlerts(data.errors, data.infos, data.warnings);
  121. }
  122. if(data.refresh_html) {
  123. removeVisibleModal();
  124. $('.page-content > .container-fluid').html(data.refresh_html);
  125. }
  126. // if($this.data('scroll')) {
  127. // scrollTo($this);
  128. // }
  129. if(!$this.data('bigdata')) {
  130. turnOnTooltips();
  131. turnOnIcheck();
  132. select2Create();
  133. }
  134. });
  135. });
  136. }
  137. function changeActiveNavbar(page) {
  138. $('ul[class="nav navbar-nav"]').children().each(function (i, data) {
  139. $(data).removeClass('active');
  140. })
  141. $('a[data-seturlto="' + page + '"]').closest('ul[class="nav navbar-nav"] > li').addClass('active');
  142. }
  143. function scrollTo($obj) {
  144. if($obj.parents('.modal').length)
  145. $obj.parents('.modal').first().find('.modal-body').animate({scrollTop: 0});
  146. else
  147. $('html,body').animate({scrollTop: $obj.offset().top - 20});
  148. }
  149. function select2Create() {
  150. try {
  151. $(".mg-wrapper select.select2").select2({
  152. containerCssClass: ' tpx-select2-container',
  153. dropdownCssClass: ' tpx-select2-drop'
  154. });
  155. } catch(e) {}
  156. }
  157. function hideTooltips() {
  158. $(".mg-wrapper [title]").bstooltip('hide');
  159. }
  160. function turnOnTooltips() {
  161. $(".mg-wrapper [title]").bstooltip();
  162. }
  163. function turnOnIcheck() {
  164. var collection = $(".mg-wrapper .checkbox :checkbox");
  165. if(collection.length < 300) {
  166. collection.iCheck({
  167. checkboxClass: 'checkbox-styled',
  168. radioClass: 'radio-styled',
  169. increaseArea: '20%'
  170. });
  171. }
  172. }
  173. function removeVisibleModal() {
  174. if($('.modal:visible').length) {
  175. $('body').removeClass('modal-open').css('padding-right','');
  176. $('.modal:visible').hide();
  177. $('.modal-backdrop').remove();
  178. }
  179. }
  180. function isFormValidate($obj) {
  181. $obj.validator('validate');
  182. if ($('.has-error', $obj).length) {
  183. return false;
  184. }
  185. return true;
  186. }
  187. function runGlobalFunctionFromString(global_function, data, $obj) {
  188. if(typeof global_function != 'undefined' && typeof (window[global_function]) == "function") {
  189. window[global_function](data, $obj);
  190. }
  191. }
  192. function showAlerts(errors, infos, warnings, $obj) {
  193. if(typeof $obj == 'undefined') $obj = $('#MGAlerts');
  194. $.each(errors, function(index, value) {
  195. $obj.alerts('error',value);
  196. });
  197. $.each(infos, function(index, value) {
  198. $obj.alerts('success',value);
  199. });
  200. $.each(warnings, function(index, value) {
  201. $obj.alerts('warning',value);
  202. });
  203. scrollTo($obj);
  204. // if($obj.parents('.modal').length)
  205. // $obj.parents('.modal').first().find('.modal-body').animate({scrollTop: 0});
  206. // else
  207. // $('html,body').animate({scrollTop: $obj.offset().top - 20});
  208. }
  209. function queryToArray(query) {
  210. var queryParameters = [], queryString = query, re = /([^&=]+)=([^&]*)/g, m;
  211. while (m = re.exec(queryString)) {
  212. if (typeof m[2] == 'string') {
  213. m[2] = m[2].replace(/\+/g, " ").replace(/%2b/g, '+');
  214. }
  215. queryParameters = $.merge(queryParameters, [{name: decodeURIComponent(m[1]), value: decodeURIComponent(m[2])}]);
  216. }
  217. return queryParameters;
  218. }
  219. function queryToAssociativeArray(query) {
  220. var queryParameters = {}, queryString = query, re = /([^&=]+)=([^&]*)/g, m;
  221. while (m = re.exec(queryString)) {
  222. if (typeof m[2] == 'string') {
  223. m[2] = m[2].replace(/\+/g, " ").replace(/%2b/g, '+');
  224. }
  225. queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  226. }
  227. return queryParameters;
  228. }
  229. function arrayToQuery(array) {
  230. return $.param(array);
  231. }
  232. function openInNewTab(url) {
  233. var win = window.open(url, '_blank');
  234. win.focus();
  235. }
  236. jQuery.fn.serializeArrayWithTurnedOffCheckboxes = function(shouldFilter = false) {
  237. var values = jQuery(this).serializeArray();
  238. if(shouldFilter)
  239. {
  240. $.each(values, function (index, value) {
  241. try
  242. {
  243. if(value.hasOwnProperty('name') && value.name.substr(0, 7) == 'record[')
  244. {
  245. values[index] = undefined;
  246. }
  247. }catch (e) {
  248. }
  249. });
  250. values = jQuery.grep(values, function(value) {
  251. return value != undefined;
  252. });
  253. }
  254. values = values.concat(
  255. jQuery(this).filter('input[type=checkbox]:not(:checked, [data-let-it-go])').map(
  256. function() {
  257. return {"name": this.name, "value": ''};
  258. }).get()
  259. );
  260. return values;
  261. };
  262. })(jQuery);
  263. //==============================================================================
  264. var JSONParser = {
  265. url: false,
  266. type: 'post',
  267. startString: 'JSONRESPONSE#',
  268. stopString: '#ENDJSONRESPONSE',
  269. currentPage: false,
  270. requestCounter: 0,
  271. create: function(url,type){
  272. this.url = url;
  273. if(type !== undefined)
  274. {
  275. this.type = type;
  276. }
  277. },
  278. getJSON: function(json,disableError){
  279. this.requestCounter--;
  280. if(this.requestCounter == 0)
  281. {
  282. jQuery('#MGLoader').loader('hide');
  283. }
  284. var old = json;
  285. var start = json.indexOf(this.startString);
  286. json = json.substr(start+this.startString.length,json.indexOf(this.stopString)-start-this.startString.length);
  287. try{
  288. return jQuery.parseJSON(json);
  289. }catch(e)
  290. {
  291. //alert(old); //TODO: usunąć
  292. jQuery('#MGAlerts').alerts('error',"Somethings Goes Wrong, check logs, contact admin");
  293. jQuery('.modal.in').modal('hide');
  294. return false;
  295. }
  296. },
  297. refreshUrl : function () {
  298. var that = this;
  299. if (window.location.href.indexOf('&mg-action') > 0)
  300. {
  301. that.url = window.location.href.substr(0, window.location.href.indexOf('&mg-action')) + '&json=1';
  302. }
  303. },
  304. request: function (action, data, callback, loader, disableErrors) {
  305. var details = {};
  306. var that = this;
  307. if(data === undefined)
  308. {
  309. data = [];
  310. }
  311. data = jQuery.merge(data, [{name: 'mg-action', value: action}]);
  312. if(this.currentPage)
  313. {
  314. data = jQuery.merge(data, [{name: 'mg-page', value: this.currentPage}]);
  315. }
  316. if(loader === undefined) {
  317. jQuery('#MGLoader').loader();
  318. }else if(loader!="off"){
  319. jQuery(loader).loader();
  320. }
  321. this.requestCounter++;
  322. var tmpType = this.type;
  323. if(filesToUpload !== null){
  324. tmpType = 'postFile';
  325. }
  326. switch(tmpType)
  327. {
  328. default:
  329. jQuery.post(this.url,data,function(response){
  330. parsed = that.getJSON(response,disableErrors);
  331. if(parsed)
  332. {
  333. if(parsed.hasOwnProperty('data')){
  334. if(parsed.data !== null){
  335. if(parsed.data.seturlto){
  336. window.open(parsed.data.seturlto);
  337. return;
  338. }
  339. }
  340. }
  341. if(parsed.success)
  342. {
  343. jQuery('#MGAlerts').alerts('success',parsed.success);
  344. }
  345. if(parsed.error)
  346. {
  347. jQuery('#MGAlerts').alerts('error',parsed.error);
  348. jQuery('.modal.in').modal('hide');
  349. }
  350. if(parsed.data) {
  351. callback(parsed.data);
  352. } else {
  353. callback({});
  354. }
  355. }
  356. else
  357. {
  358. jQuery('#MGAlerts').alerts('error',"Somethings Goes Wrong, check logs, contact admin");
  359. jQuery('.modal.in').modal('hide');
  360. }
  361. }).fail(function(response) {
  362. if(response.responseText)
  363. {
  364. jQuery('#MGAlerts').alerts('error',response.responseText);
  365. jQuery('#MGLoader').loader('hide');
  366. }
  367. });
  368. break;
  369. case 'postFile':
  370. var tmpData = new FormData();
  371. var fileName;
  372. $.each(filesToUpload, function(key, value)
  373. {
  374. tmpData.append(key, value);
  375. fileName = value.name;
  376. });
  377. that.refreshUrl();
  378. $.ajax({
  379. type: "POST",
  380. url: this.url+'&mg-action='+action+'&fileName='+fileName,
  381. data: tmpData,
  382. cache: false,
  383. processData: false,
  384. contentType: false
  385. }).done(function(response){
  386. parsed = that.getJSON(response,disableErrors);
  387. if(parsed)
  388. {
  389. if(parsed.hasOwnProperty('data')){
  390. if(parsed.data !== null){
  391. if(parsed.data.seturlto){
  392. window.open(parsed.data.seturlto);
  393. return;
  394. }
  395. }
  396. }
  397. if(parsed.success)
  398. {
  399. jQuery('#MGAlerts').alerts('success',parsed.success);
  400. filesToUpload = null;
  401. }
  402. if(parsed.error)
  403. {
  404. jQuery('#MGAlerts').alerts('error',parsed.error);
  405. jQuery('.modal.in').modal('hide');
  406. }
  407. if(parsed.data) {
  408. callback(parsed.data);
  409. } else {
  410. callback({});
  411. }
  412. }
  413. else
  414. {
  415. jQuery('#MGAlerts').alerts('error',"Somethings Goes Wrong, check logs, contact admin");
  416. jQuery('.modal.in').modal('hide');
  417. }
  418. }).fail(function(response) {
  419. if(response.responseText)
  420. {
  421. jQuery('#MGAlerts').alerts('error',response.responseText);
  422. jQuery('#MGLoader').loader('hide');
  423. }
  424. });
  425. break;
  426. }
  427. }
  428. };
  429. function ucfirst(string)
  430. {
  431. return string.charAt(0).toUpperCase() + string.slice(1);
  432. }
  433. jQuery.fn.alerts = function(type,message,configs){
  434. configs = jQuery.extend({
  435. items: null
  436. ,confirmCallback: null
  437. ,link: null
  438. }, configs);
  439. items = configs.items;
  440. confirmCallback = configs.confirmCallback;
  441. var container = this;
  442. var now = new Date().getTime();
  443. var current = new Array();
  444. var count = 0;
  445. var max = 2;
  446. jQuery(container).children('div[data-time]').each(function(){
  447. var time = new String(jQuery(this).attr('data-time'));
  448. current[time] = 1;
  449. count++;
  450. });
  451. current.sort();
  452. if(count > max)
  453. {
  454. for(x in current)
  455. {
  456. var set = parseInt(x);
  457. if(set > 0)
  458. {
  459. if( now-set > 10 && count-max > 0)
  460. {
  461. jQuery(container).find('div[data-time="'+set+'"]').remove();
  462. count--;
  463. }
  464. }
  465. }
  466. }
  467. if(type === 'clear')
  468. {
  469. jQuery(container).find('div[data-time]').remove();
  470. return;
  471. }
  472. var prototype = jQuery('#MGAlerts').find('div[data-prototype="'+type+'"]').clone();
  473. prototype.find('strong').append(message);
  474. var length = message.length;
  475. if(items != undefined)
  476. {
  477. var html = '<ul>';
  478. for(x in items)
  479. {
  480. html += '<li>'+items[x]+'</li>';
  481. }
  482. html += '</ul>';
  483. prototype.append(html);
  484. length += html.length;
  485. }
  486. prototype.find('.close').click(function(){
  487. jQuery(this).parent().remove();
  488. });
  489. prototype.attr('data-time',now);
  490. if(configs.link)
  491. {
  492. prototype.append('<a href="'+configs.link.url+'">'+configs.link.name+'</a>');
  493. }
  494. prototype.removeAttr('data-prototype');
  495. prototype.show();
  496. jQuery(container).append(prototype);
  497. setTimeout(function() {
  498. prototype.fadeOut('fast', function() {
  499. $(this).remove();
  500. });
  501. }, 5000 + length * 50);
  502. };
  503. jQuery.fn.loader = function(action)
  504. {
  505. if(action === undefined || action == 'show')
  506. {
  507. jQuery('body').css('position','relative');
  508. jQuery(this).show();
  509. }
  510. else
  511. {
  512. jQuery(this).hide();
  513. }
  514. }
  515. jQuery.fn.MGGetForms = function(action)
  516. {
  517. var that = this;
  518. var data = {};
  519. jQuery(this).find('input,select').each(function(){
  520. if(!jQuery(this).is(':disabled'))
  521. {
  522. var name = jQuery(this).attr('name');
  523. var value = null;
  524. if(name !== undefined)
  525. {
  526. var type = jQuery(this).attr('type');
  527. var regExp = /([a-zA-Z_0-9]+)\[([a-zA-Z_0-9]+)\]/g;
  528. if(type == 'checkbox')
  529. {
  530. var value = [];
  531. jQuery(that).find('input[name="'+name+'"]').each(function(){
  532. if(jQuery(this).is(':checked'))
  533. {
  534. value.push(jQuery(this).val());
  535. }
  536. });
  537. }
  538. else if(type == 'radio')
  539. {
  540. if(jQuery(this).is(':checked'))
  541. {
  542. var value = jQuery(this).val();
  543. }
  544. }
  545. else
  546. {
  547. var value = jQuery(this).val();
  548. }
  549. if(value !== null)
  550. {
  551. if(result = regExp.exec(name))
  552. {
  553. if(data[result[1]] === undefined)
  554. {
  555. data[result[1]] = {}
  556. }
  557. data[result[1]][result[2]] = value;
  558. }
  559. else
  560. {
  561. data[name] = value;
  562. }
  563. }
  564. }
  565. }
  566. });
  567. return data;
  568. }
  569. jQuery.fn.MGModalActions = function(){
  570. var that = this;
  571. var rowUpdateFunction;
  572. this.putField = function(modal,name,value){
  573. var element = modal.find('*[name="'+name+'"]');
  574. if(element.length > 0){
  575. switch(element.prop('tagName').toLowerCase())
  576. {
  577. case 'input':
  578. if(element.attr('type') == 'checkbox')
  579. {
  580. if(value == 1)
  581. {
  582. element.attr('checked','checked');
  583. }
  584. else
  585. {
  586. element.removeAttr('checked');
  587. }
  588. }
  589. else if(element.attr('type') == 'radio')
  590. {
  591. element.filter('*[value="'+value+'"]').attr('checked','checked');
  592. }
  593. else
  594. {
  595. element.val(value);
  596. }
  597. break;
  598. case 'select':
  599. element.val(value);
  600. break;
  601. }
  602. element.change();
  603. }
  604. var element = modal.find('*[name="'+name+'[]"]');
  605. if(element.length > 0){
  606. switch(element.prop('tagName').toLowerCase())
  607. {
  608. case 'select':
  609. if(element.attr('multiple'))
  610. {
  611. element.find('option').removeAttr('selected');
  612. for(x in value)
  613. {
  614. element.find('option[value="'+value[x]+'"]').attr('selected','selected');
  615. }
  616. }
  617. break;
  618. case 'input':
  619. if(element.attr('type') == 'checkbox')
  620. {
  621. modal.find('input[type=checkbox][name="'+name+'[]"]').removeAttr('checked');
  622. for(x in value)
  623. {
  624. modal.find('input[type=checkbox][name="'+name+'[]"][value="'+value[x]+'"]').attr('checked','checked');
  625. }
  626. }
  627. break;
  628. }
  629. element.change();
  630. }
  631. }
  632. this.addErrorField = function(modal,name,error){
  633. var element = modal.find('*[name="'+name+'"]');
  634. if(element.length == 0){
  635. var element = modal.find('*[name="'+name+'[]"]');
  636. }
  637. var contener = element.closest('div.form-group');
  638. contener.addClass('has-error');
  639. contener.find('.error-block').text(error).show();
  640. }
  641. this.clearModalError = function(modal){
  642. modal.find('.form-group.has-error').removeClass('has-error');
  643. modal.find('.error-block').text('').hide();
  644. modal.find('.modal-alerts').alerts('clear');
  645. }
  646. this.setRowUpdateFunction = function(updatefunction ){
  647. this.rowUpdateFunction = updatefunction;
  648. }
  649. this.on('click','*[data-modal-id]', function(event){
  650. event.preventDefault();
  651. var target = jQuery(event.currentTarget).attr('data-modal-target');
  652. if(!target)
  653. {
  654. throw "Define target ID (data-modal-target)";
  655. }
  656. var modal = jQuery(event.currentTarget).attr('data-modal-id');
  657. if(!modal)
  658. {
  659. throw "Define modal id (data-modal-id)";
  660. }
  661. var action = jQuery('#'+modal).attr('data-modal-load');
  662. var functionName = jQuery('#'+modal).attr('data-modal-on-load');
  663. var onload = window[functionName];
  664. jQuery('#'+modal).find('[data-target]').val(target);
  665. that.clearModalError(jQuery('#'+modal));
  666. if(action)
  667. {
  668. jQuery('#'+modal).find('.modal-body').hide();
  669. jQuery('#'+modal).find('.modal-loader').show();
  670. JSONParser.request(
  671. action
  672. , {
  673. id: target
  674. }
  675. , function(data) {
  676. if(typeof onload === 'function')
  677. {
  678. data = onload(data);
  679. }
  680. if(data.form)
  681. {
  682. for(x in data.form)
  683. {
  684. that.putField(jQuery('#'+modal),x,data.form[x]);
  685. }
  686. }
  687. if(data.error)
  688. {
  689. jQuery('#MGAlerts').alerts('success',data.error);
  690. jQuery('#'+modal).find('*[data-modal-action]').attr('disabled','disabled');
  691. }
  692. else
  693. {
  694. jQuery('#'+modal).find('*[data-modal-action]').removeAttr('disabled');
  695. }
  696. if(data.vars)
  697. {
  698. jQuery('#'+modal).find('*[data-modal-var]').each(function(){
  699. var name = jQuery(this).attr('data-modal-var');
  700. if(data.vars[name])
  701. {
  702. jQuery(this).text(data.vars[name]);
  703. }
  704. else
  705. {
  706. jQuery(this).text();
  707. }
  708. });
  709. }
  710. jQuery('#'+modal).find('.modal-body').show();
  711. jQuery('#'+modal).find('.modal-loader').hide();
  712. }
  713. );
  714. }
  715. else
  716. {
  717. jQuery('#'+modal).find('.modal-body').show();
  718. jQuery('#'+modal).find('.modal-loader').hide();
  719. }
  720. jQuery('#'+modal).modal();
  721. });
  722. this.updateRow = function(rowData){
  723. for(var x in rowData)
  724. {
  725. if(x == 'DT_RowData')
  726. {
  727. var selector = 'tr';
  728. for(var z in rowData['DT_RowData'])
  729. {
  730. selector += '[data-'+z+'="'+rowData['DT_RowData'][z]+'"]';
  731. }
  732. var row = that.find(selector);
  733. }
  734. }
  735. if(row)
  736. {
  737. for(var x in rowData)
  738. {
  739. if(x == 'DT_RowClass')
  740. {
  741. jQuery(row).attr('class','');
  742. jQuery(row).addClass(rowData[x]);
  743. }
  744. else if(x != 'DT_RowData')
  745. {
  746. jQuery(row).find('td:eq('+x+')').html(rowData[x]);
  747. }
  748. }
  749. }
  750. row = null;
  751. }
  752. this.modalAction = function(action,target,data){
  753. if(target)
  754. {
  755. data['id'] = target;
  756. }
  757. JSONParser.request(
  758. action
  759. ,data
  760. , function(data) {
  761. if(data.saved)
  762. {
  763. if(typeof that.rowUpdateFunction === 'function')
  764. {
  765. data = that.rowUpdateFunction(data);
  766. }
  767. that.updateRow(data.saved);
  768. }
  769. if(data.deleted)
  770. {
  771. if(typeof that.rowUpdateFunction === 'function')
  772. {
  773. data.deleted = that.rowUpdateFunction(data.deleted);
  774. }
  775. for(var x in data.deleted)
  776. {
  777. if(x == 'DT_RowData')
  778. {
  779. var selector = 'tr';
  780. for(var z in data.deleted['DT_RowData'])
  781. {
  782. selector += '[data-'+z+'="'+data.deleted['DT_RowData'][z]+'"]';
  783. }
  784. var row = that.find(selector);
  785. }
  786. }
  787. if(row)
  788. {
  789. if(data.deletedRowMessage)
  790. {
  791. row.html('<td colspan="'+row.find('td').length+'">'+data.deletedRowMessage+'</td>');
  792. }
  793. else
  794. {
  795. row.remove();
  796. }
  797. }
  798. row = null;
  799. }
  800. if(data.modalError || data.modalSuccess)
  801. {
  802. if(data.modalError)
  803. {
  804. jQuery('.modal.in .modal-alerts').alerts('error',data.modalError);
  805. }
  806. if(parsed.modalSuccess)
  807. {
  808. jQuery('.modal.in .modal-alerts').alerts('success',data.modalSuccess);
  809. }
  810. }
  811. else
  812. {
  813. jQuery('.modal.in').modal('hide');
  814. }
  815. if(data.modalFieldsErrors)
  816. {
  817. for(x in data.modalFieldsErrors)
  818. {
  819. if(data.modalFieldsErrors[x])
  820. {
  821. that.addErrorField(jQuery('.modal.in'),x,data.modalFieldsErrors[x]);
  822. }
  823. }
  824. }
  825. }
  826. );
  827. }
  828. this.on('click','*[data-modal-action]', function(event){
  829. event.preventDefault();
  830. var action = jQuery(this).attr('data-modal-action');
  831. var target = jQuery(this).attr('data-modal-target');
  832. var data = jQuery(this).closest('.modal').MGGetForms();
  833. that.modalAction(action,target,data);
  834. });
  835. return this;
  836. }
  837. // edited by Vitalii A (cut from templates/pages/settings/packages.tpl
  838. jQuery(document).ready(function(){
  839. $(document).delegate('[name="allowed_record_types[]"]', 'change', function(e) {
  840. var $container = $(this).parents('.tab-pane').first().find('.reords-limit').first();
  841. if(e.added) {
  842. var record_name = e.added.id;
  843. var name = 'settings[allowed_record_types]['+ record_name +']';
  844. var number = $container.children(':input').length;
  845. var template = $container.find('.template').html();
  846. var html = template.replace(/:number:/g, number).replace(/:record_name:/g, record_name).replace(/:name:/g, name);
  847. $container.append(html);
  848. } else {
  849. var record_name = e.removed.id;
  850. $container.find('.record-' + record_name).remove();
  851. }
  852. var $containerDefaultTTL = $(this).parents('.tab-pane').first().find('.reords-limit-default-ttl').first();
  853. if(e.added) {
  854. var record_name = e.added.id;
  855. var name = 'settings[default_ttl]['+ record_name +']';
  856. var number = $containerDefaultTTL.children(':input').length;
  857. var template = $containerDefaultTTL.find('.template').html();
  858. var html = template.replace(/:number:/g, number).replace(/:record_name:/g, record_name).replace(/:name:/g, name);
  859. $containerDefaultTTL.append(html);
  860. } else {
  861. var record_name = e.removed.id;
  862. $containerDefaultTTL.find('.record-' + record_name).remove();
  863. }
  864. });
  865. });