script.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 isDefined(data) {
  9. if (typeof data === 'undefined') {
  10. return false;
  11. } else {
  12. return true;
  13. }
  14. }
  15. function definedOr(data, value = '') {
  16. if (typeof data === 'undefined') {
  17. return value;
  18. } else {
  19. return data;
  20. }
  21. }
  22. function setIfNotEmpty(location, value) {
  23. if (value != '') {
  24. $(location).val(value);
  25. }
  26. }
  27. /*
  28. * ============================== Template Boot
  29. * _______ _ _
  30. * |__ __| | | | |
  31. * | | ___ _ __ ___ _ __ | | __ _| |_ ___
  32. * | |/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
  33. * | | __/ | | | | | |_) | | (_| | || __/
  34. * |_|\___|_| |_| |_| .__/|_|\__,_|\__\___|
  35. * | |
  36. * |_|
  37. */
  38. /* Função de aparecer ou desaparecer o menu lateral */
  39. $(".menu-toggle").on('click', function (e) {
  40. e.preventDefault();
  41. //e.preventPropagation();
  42. $("#wrapper").toggleClass("toggled");
  43. window.dispatchEvent(new Event('resize'));
  44. });
  45. if (user_config.menu_default) {
  46. $('#wrapper').addClass('toggled');
  47. window.dispatchEvent(new Event('resize'));
  48. }
  49. /* Função de aparecer ou desaparecer o menu lateral Secundário*/
  50. $(".menu-toggle").on('contextmenu', function (e) {
  51. e.preventDefault();
  52. //e.preventPropagation();
  53. alert("Configs maluquinhas");
  54. window.dispatchEvent(new Event('resize'));
  55. });
  56. $('#side-menu').metisMenu();
  57. /*
  58. * ============================== - MouseTrap
  59. * __ __ _______
  60. * | \/ | |__ __|
  61. * | \ / | ___ _ _ ___ ___| |_ __ __ _ _ __
  62. * | |\/| |/ _ \| | | / __|/ _ \ | '__/ _` | '_ \
  63. * | | | | (_) | |_| \__ \ __/ | | | (_| | |_) |
  64. * |_| |_|\___/ \__,_|___/\___|_|_| \__,_| .__/
  65. * | |
  66. * |_|
  67. *
  68. */
  69. /* Atalho, Ao apertar alt, foca no primeiro ítem do menu lateral */
  70. Mousetrap.bind('alt', function (e) {
  71. e.preventDefault();
  72. $('#side-menu li:first-child a').focus();
  73. $("#wrapper").removeClass("toggled");
  74. return false;
  75. });
  76. Mousetrap.bind("alt+1", function (e) {
  77. $("#wrapper").addClass("toggled");
  78. e.preventDefault();
  79. });
  80. Mousetrap.bind("alt+2", function (e) {
  81. $('#menu-usuario > li').addClass('open');
  82. $('#menu-usuario > li >a').focus();
  83. e.preventDefault();
  84. });
  85. //Foca na barra de pesquisa
  86. Mousetrap.bind("ctrl+shift+f", function (e) {
  87. $(".search-nav input").focus();
  88. });
  89. function popover_help() {
  90. $('input').each(function () {
  91. Mousetrap($(this)[0]).bind('ctrl+f1', function () {
  92. //alert(10);
  93. });
  94. })
  95. }
  96. Mousetrap.bind("ctrl+f1", function (e) {
  97. //alert(10);
  98. });
  99. //============================== MenuSearch
  100. var menu = Array();
  101. $("#side-menu a").each(function () {
  102. if ($(this).attr('href') !== "") {
  103. menu.push("<li>" + $(this).parent().html() + "</li>")
  104. }
  105. });
  106. $("#side-second-menu").html(menu);
  107. $(".search-nav >input").on("keyup", function () {
  108. var search = $(this).val().toLowerCase();
  109. if ($(this).val() === "") {
  110. $("#side-menu").removeClass("hidden");
  111. $("#side-second-menu").addClass("hidden");
  112. } else {
  113. $("#side-menu").addClass("hidden");
  114. $("#side-second-menu").removeClass("hidden");
  115. $("#side-second-menu li").each(function () {
  116. if ($(this).html().toLowerCase().indexOf(search) > -1) {
  117. $(this).removeClass('hidden');
  118. } else {
  119. $(this).addClass('hidden');
  120. }
  121. });
  122. }
  123. });
  124. /*
  125. * ============================== - Ajax Page Handler
  126. * _ _____
  127. * /\ (_) | __ \
  128. * / \ _ __ ___ __ | |__) |_ _ __ _ ___ ___
  129. * / /\ \ | |/ _` \ \/ / | ___/ _` |/ _` |/ _ \/ __|
  130. * / ____ \| | (_| |> < | | | (_| | (_| | __/\__ \
  131. * /_/ \_\ |\__,_/_/\_\ |_| \__,_|\__, |\___||___/
  132. * _/ | __/ |
  133. * |__/ |___/
  134. */
  135. //==============================
  136. //=== Link clicks
  137. //==============================
  138. $('body').on('click', 'a', function (e) {
  139. if (!user_config.allways_ajax) {
  140. return true;
  141. }
  142. var target = $(this).attr('href');
  143. var method = $(this).data('method');
  144. if (typeof target === 'undefined' ||
  145. target === '' ||
  146. target === '#' ||
  147. target == 'javascript:void(0)') {
  148. return;
  149. }
  150. if (method === 'reload' || method === 'follow') {
  151. return true;
  152. }
  153. e.preventDefault();
  154. if (typeof method === 'undefined') {
  155. method = 'GET';
  156. }
  157. asyncRedirect(target, method);
  158. });
  159. //==============================
  160. //=== Form submissions
  161. //==============================
  162. $('body').on('submit', 'form', async function (e) {
  163. var formAjax = true;
  164. if (formAjax == false) {
  165. return true;
  166. }
  167. e.preventDefault();
  168. let request = await fetch(e.target.action, {
  169. method: e.target.method,
  170. body: JSON.stringify($(this).serialize()).replace(/^"+|"+$/g, ''),
  171. headers: {
  172. 'Content-Type': 'application/x-www-form-urlencoded',
  173. 'X-Requested-With': 'fetch'
  174. }
  175. });
  176. let responseJson = await request.json();
  177. if (!request.ok) {
  178. $('#toast-container').bootstrapToastWrapper({
  179. 'type': 'danger',
  180. 'title': 'Erro',
  181. 'body': responseJson,
  182. 'timeout': 5000
  183. });
  184. } else {
  185. $('#toast-container').bootstrapToastWrapper({
  186. 'type': 'success',
  187. 'title': 'Sucesso',
  188. 'body': 'Ação bem sucedida',
  189. 'timeout': 5000
  190. });
  191. if (responseJson.location) {
  192. asyncRedirect(responseJson.location, 'GET');
  193. }
  194. }
  195. });
  196. /*
  197. * Ao sair do campo digitável de um valor buscavel. Executa a busca
  198. */
  199. $('#content').on('blur', '[data-search] input', function (e) {
  200. val = $(this).val();
  201. if (val == "") { return; }
  202. el = $(this);
  203. elwrap = $(this).parent().parent();
  204. url = $(this).parent().parent().data('search');
  205. fields = $(this).parent().parent().data('searchfields').split('+');
  206. fetch(url + val, {
  207. method: 'GET',
  208. headers: {
  209. 'Content-Type': 'application/x-www-form-urlencoded',
  210. 'X-Requested-With': 'fetch'
  211. }
  212. }).then(function (response) {
  213. let json = response.json();
  214. return json;
  215. }).then(function (json) {
  216. if (json == null) {
  217. $(this).val('');
  218. $($(elwrap).find('input')[1]).val("");
  219. return;
  220. }
  221. var fieldData = "";
  222. for (value in fields) {
  223. fieldData += json[fields[value]] + " ";
  224. }
  225. $($(elwrap).find('input')[1]).val(fieldData);
  226. $(this).find('input').trigger('filled', json);
  227. //$(e).trigger('filled', json);
  228. //console.log();
  229. });
  230. });
  231. // Revalidate screen
  232. function revalidate_screen(location) {
  233. $(location).find('[data-mask]').each(function () {
  234. if ($(this).data('maskreverse') == false) {
  235. } else {
  236. var params = {
  237. reverse: true
  238. }
  239. }
  240. if ($(this).data('maskcomplete')) {
  241. params.onComplete = window[$(this).data('maskcomplete')]
  242. }
  243. $(this).mask($(this).data('mask'), params);
  244. $(this).on('filled', { 'params': params }, function (e) {
  245. $(this).mask($(this).data('mask').mask);
  246. $(this).mask($(this).data('mask').mask, params);
  247. });
  248. });
  249. /*$(location).find('.remoteFill').each(function(i, el){
  250. $(el).on('click', function(){
  251. $(this).remoteFill();
  252. })
  253. });*/
  254. //$('[data-toggle="popover"]').popover();
  255. $(location).find('.remote-element').each(async function () {
  256. var element = $(this);
  257. var source = $(this).data('source');
  258. var value = $(this).data('value');
  259. source = source + '?value=' + value;
  260. await asyncFill(source, 'GET', element, revalidate_screen, element);
  261. });
  262. $(location).find('.remote-datatable').each(async function () {
  263. var element = $(this);
  264. var source = $(this).data('source');
  265. var value = $(this).data('value');
  266. await asyncFill(source, 'GET', element, datatableInplaceBoot, $(this));
  267. });
  268. setSelectValues(location);
  269. datatableBasicBoot();
  270. $('.icon-colapser').on('click', function () {
  271. $(this).parent().parent().find('.panel-collapse').collapse('toggle');
  272. });
  273. $(location).find('[data-tablesearch]').on('click', async function (e) {
  274. await asyncFill(
  275. $(e.currentTarget).data('tablesearch'),
  276. 'GET',
  277. $('#modal .modal-body'),
  278. datatableBasicBoot,
  279. function (el) {
  280. $(el).on('click', 'button', function (ee) {
  281. $($(modalTarget).find('input')[0]).val($(ee.currentTarget).data('id')).trigger('blur');
  282. modal.toggle();
  283. })
  284. });
  285. await openModal({
  286. 'target': $(e.currentTarget).parent().parent().parent(),
  287. 'source': $(e.currentTarget).data('search')
  288. });
  289. });
  290. $(location).find('[data-tablesearchadd]').on('click', async function (e) {
  291. await asyncFill(
  292. $(e.currentTarget).data('tablesearchadd') + '/table',
  293. 'GET',
  294. $('#modal .modal-body'),
  295. datatableBasicBoot,
  296. async function (el) {
  297. await $(el).on('click', 'button', async function (ee) {
  298. modalTarget.trigger('added', $(ee.target).data('id'));
  299. modal.toggle();
  300. })
  301. });
  302. await openModal({
  303. 'target': $(e.currentTarget).parent().parent().find('.list-group'),
  304. 'source': $(e.currentTarget).data('search')
  305. });
  306. });
  307. $(location).find('[data-ean] button').on('click', function (e) {
  308. $('#modal .modal-body').html('<div id="reader" width="600px"></div>');
  309. function onScanSuccess(decodedText, decodedResult) {
  310. // handle the scanned code as you like, for example:
  311. $(this).parent().find('input').val(decodedText);
  312. myModal.hide();
  313. }
  314. scan = new Html5QrcodeScanner(
  315. "reader",
  316. { fps: 10, qrbox: { width: 250, height: 250 } },
  317. /* verbose= */ false);
  318. scan.render(onScanSuccess);
  319. openModal({
  320. 'target': $(e.currentTarget).parent().parent().find('.list-group'),
  321. 'source': $(e.currentTarget).data('search'),
  322. 'close': function () {
  323. scan.stop();
  324. }
  325. });
  326. });
  327. /*
  328. * Campos que se autopreenchem quando o valor é buscado
  329. */
  330. $(location).find('[data-listener]').each(function (el, i) {
  331. var element = $(this);
  332. let selector = "[name='" + $(this).data('listener') + "']";
  333. let key = $(this).data('listenervalue');
  334. $(selector).on('filled blur', function (e, data, data1) {
  335. console.log($(this));
  336. });
  337. });
  338. /* TODO create validation rules */
  339. $('#content').find('form').each(function () {
  340. //$(this).validator();
  341. });
  342. popover_help();
  343. }
  344. /*
  345. * ================================================
  346. * _____ _ _ _ _
  347. * | __ \ | | | | | | | |
  348. * | | | | __ _| |_ __ _| |_ __ _| |__ | | ___ ___
  349. * | | | |/ _` | __/ _` | __/ _` | '_ \| |/ _ \/ __|
  350. * | |__| | (_| | || (_| | || (_| | |_) | | __/\__ \
  351. * |_____/ \__,_|\__\__,_|\__\__,_|_.__/|_|\___||___/
  352. *
  353. */
  354. function datatableBasicBoot(param) {
  355. $('.datatable').each(function (i, el) {
  356. var columns = [];
  357. $(this).find('thead').find('th').each(function (i, el) {
  358. columns.push({ 'data': $(el).data('field') });
  359. });
  360. var ajax = {
  361. "url": $(el).data('src'),
  362. 'type': 'POST',
  363. }
  364. $(el).DataTable({
  365. "processing": true,
  366. "serverSide": true,
  367. "ajax": ajax,
  368. "columns": columns
  369. });
  370. param(el);
  371. });
  372. }
  373. function datatableInplaceBoot(location = '#content', param = function () { }) {
  374. $(location).find('.datatable').each(function (i, el) {
  375. var columns = [];
  376. $(this).find('thead').find('th').each(function (i, el) {
  377. columns.push({ 'data': $(el).data('field') });
  378. });
  379. var ajax = {
  380. "url": $(el).data('src'),
  381. 'type': 'POST',
  382. }
  383. $(el).DataTable({
  384. "processing": true,
  385. "serverSide": true,
  386. "ajax": ajax,
  387. "columns": columns
  388. });
  389. param(el);
  390. });
  391. }
  392. function datatableRemoteLoad() {
  393. }
  394. /*
  395. * ================================================
  396. * _____ _ _
  397. * / ____| | | | |
  398. * | (___ ___| | ___ ___| |_ ___
  399. * \___ \ / _ \ |/ _ \/ __| __/ __|
  400. * ____) | __/ | __/ (__| |_\__ \
  401. * |_____/ \___|_|\___|\___|\__|___/
  402. *
  403. */
  404. function setSelectValues(select, value) {
  405. if ($(select).prop('nodeName') == 'SELECT') {
  406. setSelectValue(select, $(select).data('value'));
  407. } else {
  408. $(select).find('select').each(function (i, el) {
  409. if (typeof $(this).data('value') !== "undefined" && $(this).data('value') !== '') {
  410. setSelectValue($(this), $(this).data('value'))
  411. }
  412. });
  413. }
  414. }
  415. function setSelectValue(select, value) {
  416. $(select).find('option').each(function (i, el) {
  417. $(el).attr('selected', false);
  418. if ($(el).val() == value) {
  419. $(el).attr('selected', true);
  420. }
  421. });
  422. }
  423. function setSelectName(select, value) {
  424. $(select).find('select').each(function (i, el) {
  425. if (typeof $(this).data('value') !== "undefined" && $(this).data('value') !== '') {
  426. setSelectName($(this), $(this).data('value'))
  427. }
  428. });
  429. }
  430. function setSelectName(select, value) {
  431. $(select).find('option').each(function (i, el) {
  432. $(el).attr('selected', false);
  433. if ($(el).html() == value) {
  434. $(el).attr('selected', true);
  435. }
  436. });
  437. }
  438. $('#content').on('reloaded', function () {
  439. revalidate_screen('#content');
  440. });
  441. revalidate_screen('body');