script.js 18 KB

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