script.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. //============================== Generic Javascript Functions
  2. var user_config = {
  3. log: 1,
  4. theme: 'default',
  5. menu_default: false, // true - Collapsed | false - Open
  6. allways_ajax: true
  7. };
  8. function log(message, importance = 0) {
  9. if (user_config.log === 0) {
  10. return;
  11. }
  12. if (importance === 0) {
  13. console.log("LOG:: |" + message + "|");
  14. }
  15. if (importance === 1) {
  16. console.log(message);
  17. }
  18. if (importance === 2) {
  19. alert("LOG:: |" + message + "|");
  20. }
  21. }
  22. function isDefined(data) {
  23. if (typeof data === 'undefined') {
  24. return false;
  25. } else {
  26. return true;
  27. }
  28. }
  29. function definedOr(data, value = '') {
  30. if (typeof data === 'undefined') {
  31. return value;
  32. } else {
  33. return data;
  34. }
  35. }
  36. //============================== Generic Template Functions
  37. function revalidate_screen() {
  38. $('[data-format]').each(function () {
  39. $(this).mask($(this).data('format'), {});
  40. });
  41. //$('[data-toggle="popover"]').popover();
  42. $('select').each(function () {
  43. if (typeof $(this).data('value') !== "undefined" && $(this).data('value') !== '') {
  44. $(this).val($(this).data('value'));
  45. }
  46. });
  47. $('.datepicker').each(function () {
  48. $(this).datepicker({
  49. format: "dd/mm/yyyy",
  50. todayBtn: "linked",
  51. clearBtn: true,
  52. autoclose: true,
  53. todayHighlight: true
  54. });
  55. });
  56. $('.selectpicker').each(function () {
  57. $(this).select2({
  58. liveSearch: true
  59. });
  60. });
  61. $(".wysiwyg").each(function () {
  62. $(this).trumbowyg();
  63. });
  64. $(".form-control-range").each(function(i, item){
  65. $(item).on('change', function(item, i){
  66. $(item.currentTarget)
  67. .parent()
  68. .find('.range-monitor')
  69. .html(item.currentTarget.value + "%");
  70. })
  71. });
  72. $('.popup-toggle').each(() => {
  73. var template = '<div class="modal-dialog"> <div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"> X </button><h4 class="modal-title">Modal Header</h4></div><div class="modal-body">{{aaa}}</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div>';
  74. if ($(this).hasClass('processed')) {
  75. return true;
  76. }
  77. $(this).addClass('processed');
  78. $(this).on('click', function (e) {
  79. e.preventDefault();
  80. $.ajax({
  81. method: 'GET',
  82. url: $(this).data('source')
  83. }).done(function (msg) {
  84. template = template.replace('{{aaa}}', msg);
  85. $('#modal').html(template).modal('show');
  86. revalidate_screen();
  87. });
  88. });
  89. $('#modal').on('shown.bs.modal', function () {
  90. revalidate_screen();
  91. $(this).find('.datatable-anchor').focus();
  92. });
  93. });
  94. $('.remote-popup').each((i, element) => {
  95. if ($(element).hasClass('processed')) {
  96. return true;
  97. }
  98. var instance = $(element);
  99. var number = $(element).find('.remote-popup-number');
  100. var button = $(element).find('button');
  101. var label = $(element).find('.remote-popup-label');
  102. searchRemote = (value = null) => {
  103. if (value == null) {
  104. value = $(number).val();
  105. }
  106. $.ajax({
  107. method: 'GET',
  108. url: $(instance).data('relay') + '/' + value
  109. }).done(function (msg) {
  110. $(label).val(msg.name);
  111. }).fail(function () {
  112. $(number).val('');
  113. $(label).val('');
  114. });
  115. }
  116. remotePopup = (source) => {
  117. $($('#modal')).off();
  118. var template = '<div class="modal-dialog"> <div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"> X </button><h4 class="modal-title">Modal Header</h4></div><div class="modal-body">{{aaa}}</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div>';
  119. $.ajax({
  120. method: 'GET',
  121. url: source
  122. }).done(function (msg) {
  123. template = template.replace('{{aaa}}', msg);
  124. $('#modal').html(template).modal('show');
  125. $('#modal').on('hide.bs.modal', () => {
  126. $(number).val($("#modal .selected").data('info').id);
  127. searchRemote();
  128. });
  129. revalidate_screen();
  130. });
  131. };
  132. $(button).on('click', () => {
  133. remotePopup($(instance).data('relay') + '/' + $(instance).data('mode'));
  134. });
  135. $(number).on('keyup', (e) => {
  136. if (e.keyCode == 112) {
  137. remotePopup($(instance).data('relay') + '/' + $(instance).data('mode'));
  138. }
  139. });
  140. $(number).on('focusout', () => {
  141. console.log($(number).val() == '');
  142. if ($(number).val() == '') {
  143. $(label).val('');
  144. return;
  145. }
  146. searchRemote($(number).val());
  147. });
  148. if($(number).val() != ''){
  149. searchRemote( $(number).val() )
  150. }
  151. /*console.log($(element))
  152. console.log(number);
  153. console.log(button);
  154. console.log(label);*/
  155. $(element).addClass('processed');
  156. });
  157. $('.live-selectpicker').each(function () {
  158. $(this).select2({
  159. liveSearch: true
  160. });
  161. var object = $(this);
  162. var source = definedOr($(this).data('source'), false);
  163. var placeholder = definedOr($(this).data('placeholder'));
  164. var filter_name = definedOr($(this).data('filter'));
  165. var value = definedOr($(this).data('value'));
  166. if (!source) {
  167. return;
  168. }
  169. object.ajax = {
  170. "url": source,
  171. "data": function (params) {
  172. var query = {
  173. "name": params.value
  174. }
  175. }
  176. };
  177. /*$(this).ajaxSelectPicker({
  178. ajax: {
  179. url: source + "/combo_data",
  180. method: 'GET',
  181. data: {
  182. nome: '{{{q}}}',
  183. filter: function() {
  184. if (filter_name === '') {
  185. return ''
  186. }
  187. return $("[name='" + filter_name + "'] option:selected").val();
  188. }
  189. }
  190. },
  191. locale: {
  192. emptyTitle: placeholder
  193. },
  194. preprocessData: function(data) {
  195. var registers = [];
  196. registers.push({
  197. 'value': '0',
  198. 'text': 'Selecione'
  199. });
  200. for (var i in data) {
  201. registers.push({
  202. 'value': data[i].id,
  203. 'text': data[i].nome
  204. });
  205. }
  206. return registers;
  207. },
  208. preserveSelected: true
  209. });*/
  210. });
  211. /*===========================================*/
  212. $('.icon-colapser').on('click', function () {
  213. $(this).parent().parent().find('.panel-collapse').collapse('toggle');
  214. });
  215. /*$('form').each(function () {
  216. $(this).validator();
  217. });*/
  218. $('.data-table').each(function () {
  219. if ($(this).hasClass('processed')) {
  220. return;
  221. }
  222. var wrapper = $(this);
  223. var table_anchor = $(this).find('a');
  224. var table = $(this).find('table');
  225. var str = '<thead> <tr class="header">';
  226. for (var i in table.data('columns')) {
  227. if (typeof table.data('columns')[i].name === 'undefined') {
  228. str = str + '<th>' + table.data('columns')[i].data + "</th>";
  229. } else {
  230. str = str + '<th>' + table.data('columns')[i].name + "</th>";
  231. }
  232. }
  233. str += '<th>*</th>';
  234. str += "</tr> </thead>";
  235. $(table).html(str);
  236. var datatable = $(table).DataTable({
  237. 'createdRow': function (row, data) {
  238. $(row).attr('data-info', JSON.stringify(data));
  239. var content = '';
  240. if (typeof $(table).data('update') != 'undefined') {
  241. content += '<a href="' + $(table).data('update') + '"> <i class="fa fa-pencil-square"></i> </a>'
  242. }
  243. if (typeof $(table).data('delete') != 'undefined') {
  244. content += '<a href="' + $(table).data('delete') + '"> <i class="fa fa-trash"></i> </a>'
  245. }
  246. if (typeof $(table).data('purge') != 'undefined') {
  247. content += '<a href="' + $(table).data('purge') + '"> <i class="fa fa-minus-square"></i> </a>'
  248. }
  249. $(row).append('<td>' + content + '</td>');
  250. },
  251. "drawCallback": function (settings) {
  252. if ($(wrapper).hasClass('selectable')) {
  253. $($(table).find('tr')[1]).addClass('selected');
  254. if (!$(wrapper).hasClass('processed')) {
  255. $(table_anchor).focus();
  256. }
  257. }
  258. },
  259. "initComplete": function (settings, json) {
  260. $(wrapper).addClass('processed');
  261. },
  262. processing: true,
  263. serverSide: true,
  264. searchDelay: 800,
  265. ajax: {'url': table.data('source'), 'type': 'POST'},
  266. columns: table.data('columns')
  267. });
  268. var search = $(this).find('input');
  269. $(search).on('keyup', function (e) {
  270. if (e.keyCode == 13) {
  271. $(table_anchor).focus();
  272. }
  273. });
  274. if ($(wrapper).hasClass('selectable')) {
  275. $(this).on('contextmenu', 'tr', function () {
  276. });
  277. $(this).on('click', 'tr', function () {
  278. $(table_anchor).focus();
  279. if ($(this).hasClass('selected')) {
  280. $(this).removeClass('selected');
  281. return;
  282. }
  283. $(this).parent().find('tr').each(function () {
  284. $(this).removeClass('selected');
  285. });
  286. $(this).addClass('selected');
  287. });
  288. $(table_anchor).on('focus', function () {
  289. $('body').addClass('no-scroll');
  290. $($(table).find('tr')[1]).addClass('selected');
  291. });
  292. $(table_anchor).on('focusout', function () {
  293. $('body').removeClass('no-scroll');
  294. $(table).find('tr').each((i, value) => {
  295. $(value).removeClass('selected');
  296. });
  297. });
  298. $(table_anchor).on('keypress', function (e) {
  299. });
  300. $(table_anchor).on('keyup', function (e) {
  301. e.preventDefault();
  302. if (e.keyCode == 70) {
  303. $(search).focus();
  304. }
  305. if (e.keyCode == 112) {
  306. ajax_call($(this).data('insert'));
  307. }
  308. if (e.keyCode == 113) {
  309. id = $(table).find('.selected').data('info').id;
  310. ajax_call($(this).data('insert') + '/' + id);
  311. }
  312. if (e.keyCode == 39 && e.ctrlKey) {
  313. datatable.page('last').draw('page');
  314. }
  315. if (e.keyCode == 37 && e.ctrlKey) {
  316. datatable.page('first').draw('page');
  317. $($(table).find('tr')[1]).addClass('selected');
  318. }
  319. if (e.keyCode == 37) { //left
  320. datatable.page('previous').draw('page');
  321. $($(table).find('tr')[1]).addClass('selected');
  322. }
  323. if (e.keyCode == 39) { //right
  324. datatable.page('next').draw('page');
  325. $($(table).find('tr')[1]).addClass('selected');
  326. }
  327. if (e.keyCode == 38) { //up
  328. var last = null;
  329. var line = null;
  330. $(table).find('tr').each((i, value) => {
  331. if ($(value).hasClass('selected')) {
  332. ;
  333. line = $(value)
  334. return false;
  335. }
  336. last = $(value)
  337. });
  338. if ($(last).hasClass('header')) {
  339. datatable.page('previous').draw('page');
  340. }
  341. if (line != null) {
  342. $(line).removeClass('selected');
  343. $(last).addClass('selected');
  344. }
  345. }
  346. if (e.keyCode == 40) { //up
  347. var next = false;
  348. $(table).find('tr').each((i, value) => {
  349. if (next) {
  350. $(value).addClass('selected');
  351. next = false;
  352. return false;
  353. }
  354. if ($(value).hasClass('selected')) {
  355. next = true;
  356. $(value).removeClass('selected');
  357. }
  358. });
  359. if (next) {
  360. datatable.page('next').draw('page');
  361. }
  362. }
  363. if (e.keyCode == 13) {
  364. if ($(this).parent().parent().hasClass('modal-body')) {
  365. $(this).parent().parent()
  366. .parent().parent()
  367. .parent().modal('hide');
  368. //console.log($(this).parent().find('.selected').data('info'));
  369. }
  370. }
  371. })
  372. }
  373. });
  374. popover_help();
  375. }
  376. /*===========================================*/
  377. $('.remote-element').each(function () {
  378. var element = $(this);
  379. var source = $(this).data('source');
  380. var value = $(this).data('value');
  381. $.ajax({
  382. method: "GET",
  383. url: source,
  384. data: {value: value}
  385. }).done(function (msg) {
  386. $(element).html(msg);
  387. });
  388. });
  389. //============================== Template Boot
  390. /* Função de aparecer ou desaparecer o menu lateral */
  391. $(".menu-toggle").on('click', function (e) {
  392. e.preventDefault();
  393. //e.preventPropagation();
  394. $("#wrapper").toggleClass("toggled");
  395. window.dispatchEvent(new Event('resize'));
  396. });
  397. if (user_config.menu_default) {
  398. $('#wrapper').addClass('toggled');
  399. window.dispatchEvent(new Event('resize'));
  400. }
  401. /* Função de aparecer ou desaparecer o menu lateral Secundário*/
  402. $(".menu-toggle").on('contextmenu', function (e) {
  403. e.preventDefault();
  404. //e.preventPropagation();
  405. alert("Configs maluquinhas");
  406. window.dispatchEvent(new Event('resize'));
  407. });
  408. $('#side-menu').metisMenu();
  409. revalidate_screen();
  410. //============================== - MouseTrap
  411. /* Atalho, Ao apertar alt, foca no primeiro ítem do menu lateral */
  412. Mousetrap.bind('alt', function (e) {
  413. e.preventDefault();
  414. $('#side-menu li:first-child a').focus();
  415. $("#wrapper").removeClass("toggled");
  416. return false;
  417. });
  418. Mousetrap.bind("alt+1", function (e) {
  419. $("#wrapper").addClass("toggled");
  420. e.preventDefault();
  421. });
  422. Mousetrap.bind("alt+2", function (e) {
  423. $('#menu-usuario > li').addClass('open');
  424. $('#menu-usuario > li >a').focus();
  425. e.preventDefault();
  426. });
  427. //Foca na barra de pesquisa
  428. Mousetrap.bind("ctrl+shift+f", function (e) {
  429. $(".search-nav input").focus();
  430. });
  431. function popover_help() {
  432. $('input').each(function () {
  433. Mousetrap($(this)[0]).bind('ctrl+f1', function () {
  434. //alert(10);
  435. });
  436. })
  437. }
  438. Mousetrap.bind("ctrl+f1", function (e) {
  439. //alert(10);
  440. });
  441. //============================== MenuSearch
  442. var menu = Array();
  443. $("#side-menu a").each(function () {
  444. if ($(this).attr('href') !== "") {
  445. menu.push("<li>" + $(this).parent().html() + "</li>")
  446. }
  447. });
  448. $("#side-second-menu").html(menu);
  449. $(".search-nav >input").on("keyup", function () {
  450. var search = $(this).val().toLowerCase();
  451. if ($(this).val() === "") {
  452. $("#side-menu").removeClass("hidden");
  453. $("#side-second-menu").addClass("hidden");
  454. } else {
  455. $("#side-menu").addClass("hidden");
  456. $("#side-second-menu").removeClass("hidden");
  457. $("#side-second-menu li").each(function () {
  458. if ($(this).html().toLowerCase().indexOf(search) > -1) {
  459. $(this).removeClass('hidden');
  460. } else {
  461. $(this).addClass('hidden');
  462. }
  463. });
  464. }
  465. });
  466. //============================== Ajax Page Handler
  467. $('a').on('click', function (e) {
  468. if (!user_config.allways_ajax) {
  469. return true;
  470. }
  471. var target = $(this).attr('href');
  472. var method = $(this).data('method');
  473. if (typeof target === 'undefined' ||
  474. target === '' ||
  475. target === '#' ||
  476. target == 'javascript:void(0)') {
  477. return;
  478. }
  479. if (method === 'reload') {
  480. return true;
  481. }
  482. e.preventDefault();
  483. if (typeof method === 'undefined') {
  484. method = 'GET';
  485. }
  486. $('#progress').removeClass('hidden');
  487. $.ajax({
  488. method: method,
  489. url: target
  490. }).done(function (msg) {
  491. $('#content').html(msg);
  492. history.replaceState({}, '', target);
  493. }).fail(function () {
  494. alert('fail');
  495. }).always(function () {
  496. $('#progress').addClass('hidden')
  497. revalidate_screen();
  498. });
  499. });
  500. function ajax_call(target) {
  501. $('#progress').removeClass('hidden');
  502. $.ajax({
  503. method: 'GET',
  504. url: target
  505. }).done(function (msg) {
  506. $('#content').html(msg);
  507. history.replaceState({}, '', target);
  508. }).fail(function () {
  509. alert('fail');
  510. }).always(function () {
  511. $('#progress').addClass('hidden')
  512. revalidate_screen();
  513. });
  514. }
  515. function open_popup(source) {
  516. var template = '<div class="modal-dialog"> <div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal"> X </button><h4 class="modal-title">Modal Header</h4></div><div class="modal-body">{{aaa}}</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div>';
  517. $.ajax({
  518. method: 'GET',
  519. url: source
  520. }).done(function (msg) {
  521. //alert(msg);
  522. template = template.replace('{{aaa}}', msg);
  523. $('#modal').html(template).modal('show');
  524. revalidate_screen();
  525. });
  526. }