mgLibs.js 33 KB

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