laravel-admin.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. $.fn.editable.defaults.params = function (params) {
  2. params._token = LA.token;
  3. params._editable = 1;
  4. params._method = 'PUT';
  5. return params;
  6. };
  7. $.fn.editable.defaults.error = function (data) {
  8. var msg = '';
  9. if (data.responseJSON.errors) {
  10. $.each(data.responseJSON.errors, function (k, v) {
  11. msg += v + "\n";
  12. });
  13. }
  14. return msg
  15. };
  16. toastr.options = {
  17. closeButton: true,
  18. progressBar: true,
  19. showMethod: 'slideDown',
  20. timeOut: 4000
  21. };
  22. $.pjax.defaults.timeout = 5000;
  23. $.pjax.defaults.maxCacheLength = 0;
  24. $(document).pjax('a:not(a[target="_blank"])', {
  25. container: '#pjax-container'
  26. });
  27. NProgress.configure({parent: '#app'});
  28. $(document).on('pjax:timeout', function (event) {
  29. event.preventDefault();
  30. })
  31. $(document).on('submit', 'form[pjax-container]', function (event) {
  32. $.pjax.submit(event, '#pjax-container')
  33. });
  34. $(document).on("pjax:popstate", function () {
  35. $(document).one("pjax:end", function (event) {
  36. $(event.target).find("script[data-exec-on-popstate]").each(function () {
  37. $.globalEval(this.text || this.textContent || this.innerHTML || '');
  38. });
  39. });
  40. });
  41. $(document).on('pjax:send', function (xhr) {
  42. if (xhr.relatedTarget && xhr.relatedTarget.tagName && xhr.relatedTarget.tagName.toLowerCase() === 'form') {
  43. $submit_btn = $('form[pjax-container] :submit');
  44. if ($submit_btn) {
  45. $submit_btn.button('loading')
  46. }
  47. }
  48. NProgress.start();
  49. });
  50. $(document).on('pjax:complete', function (xhr) {
  51. if (xhr.relatedTarget && xhr.relatedTarget.tagName && xhr.relatedTarget.tagName.toLowerCase() === 'form') {
  52. $submit_btn = $('form[pjax-container] :submit');
  53. if ($submit_btn) {
  54. $submit_btn.button('reset')
  55. }
  56. }
  57. NProgress.done();
  58. $.admin.grid.selects = {};
  59. });
  60. $(document).click(function () {
  61. $('.sidebar-form .dropdown-menu').hide();
  62. });
  63. $(function () {
  64. $('.sidebar-menu li:not(.treeview) > a').on('click', function () {
  65. var $parent = $(this).parent().addClass('active');
  66. $parent.siblings('.treeview.active').find('> a').trigger('click');
  67. $parent.siblings().removeClass('active').find('li').removeClass('active');
  68. });
  69. var menu = $('.sidebar-menu li > a[href$="' + (location.pathname + location.search + location.hash) + '"]').parent().addClass('active');
  70. menu.parents('ul.treeview-menu').addClass('menu-open');
  71. menu.parents('li.treeview').addClass('active');
  72. $('[data-toggle="popover"]').popover();
  73. // Sidebar form autocomplete
  74. $('.sidebar-form .autocomplete').on('keyup focus', function () {
  75. var $menu = $('.sidebar-form .dropdown-menu');
  76. var text = $(this).val();
  77. if (text === '') {
  78. $menu.hide();
  79. return;
  80. }
  81. var regex = new RegExp(text, 'i');
  82. var matched = false;
  83. $menu.find('li').each(function () {
  84. if (!regex.test($(this).find('a').text())) {
  85. $(this).hide();
  86. } else {
  87. $(this).show();
  88. matched = true;
  89. }
  90. });
  91. if (matched) {
  92. $menu.show();
  93. }
  94. }).click(function(event){
  95. event.stopPropagation();
  96. });
  97. $('.sidebar-form .dropdown-menu li a').click(function (){
  98. $('.sidebar-form .autocomplete').val($(this).text());
  99. });
  100. });
  101. $(window).scroll(function() {
  102. if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
  103. $('#totop').fadeIn(500);
  104. } else {
  105. $('#totop').fadeOut(500);
  106. }
  107. });
  108. $('#totop').on('click', function (e) {
  109. e.preventDefault();
  110. $('html,body').animate({scrollTop: 0}, 500);
  111. });
  112. (function ($) {
  113. var Grid = function () {
  114. this.selects = {};
  115. };
  116. Grid.prototype.select = function (id) {
  117. this.selects[id] = id;
  118. };
  119. Grid.prototype.unselect = function (id) {
  120. delete this.selects[id];
  121. };
  122. Grid.prototype.selected = function () {
  123. var rows = [];
  124. $.each(this.selects, function (key, val) {
  125. rows.push(key);
  126. });
  127. return rows;
  128. };
  129. $.fn.admin = LA;
  130. $.admin = LA;
  131. $.admin.swal = swal;
  132. $.admin.toastr = toastr;
  133. $.admin.grid = new Grid();
  134. $.admin.reload = function () {
  135. $.pjax.reload('#pjax-container');
  136. $.admin.grid = new Grid();
  137. };
  138. $.admin.redirect = function (url) {
  139. $.pjax({container:'#pjax-container', url: url });
  140. $.admin.grid = new Grid();
  141. };
  142. $.admin.getToken = function () {
  143. return $('meta[name="csrf-token"]').attr('content');
  144. };
  145. $.admin.loadedScripts = [];
  146. $.admin.loadScripts = function(arr) {
  147. var _arr = $.map(arr, function(src) {
  148. if ($.inArray(src, $.admin.loadedScripts)) {
  149. return;
  150. }
  151. $.admin.loadedScripts.push(src);
  152. return $.getScript(src);
  153. });
  154. _arr.push($.Deferred(function(deferred){
  155. $(deferred.resolve);
  156. }));
  157. return $.when.apply($, _arr);
  158. }
  159. })(jQuery);