site.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Created by yidashi on 16/7/28.
  3. */
  4. $(document).ajaxError(function(event,xhr,options,exc){
  5. var message = xhr.responseJSON ? xhr.responseJSON.message : '操作失败';
  6. if (xhr.status == 403) {
  7. message = xhr.responseText;
  8. }
  9. $.modal.error(message);
  10. });
  11. $.extend(yii, {
  12. clickableSelector: 'a[data-ajax!=1], button, input[type="submit"], input[type="button"], input[type="reset"], ' +
  13. 'input[type="image"]',
  14. confirm: function (message, ok, cancel) {
  15. $.modal.confirm(message, ok, cancel);
  16. }
  17. });
  18. var ajaxLink = function (options) {
  19. options = $.extend({
  20. method:$(this).data('method') || 'get',
  21. action:$(this).data('action') || $(this).attr('href'),
  22. refreshPjaxContainer: $(this).data('refresh-pjax-container') || null,
  23. refresh: $(this).data('refresh') || false,
  24. callback: $(this).data('callback') || null,
  25. confirm: $(this).data('confirm') || null,
  26. data: $(this).data('params') || {}
  27. }, options);
  28. var ele = $(this);
  29. var fn = function () {
  30. $.modal.loading();
  31. $.ajax({
  32. url: options.action,
  33. method: options.method,
  34. data: options.data,
  35. dataType: 'json',
  36. success: function (res) {
  37. if (res.status != undefined && res.status == 0) {
  38. $.modal.error(res.message || '操作失败');
  39. return;
  40. }
  41. if (!res.message) {
  42. res.message = '操作成功';
  43. }
  44. $.modal.notify(res.message, 'success', function () {
  45. if (options.refreshPjaxContainer) {
  46. $.pjax.reload({container:'#' + options.refreshPjaxContainer, timeout: 0});
  47. }
  48. if (res.redirect) {
  49. location.href = res.redirect;
  50. } else {
  51. if (options.refresh) {
  52. location.reload();
  53. }
  54. if (options.callback) {
  55. options.callback(res);
  56. }
  57. }
  58. });
  59. },
  60. complete: function () {
  61. $.modal.unloading();
  62. }
  63. });
  64. }
  65. if (options.confirm != null) {
  66. $.modal.confirm(options.confirm, function () {
  67. fn();
  68. });
  69. } else {
  70. fn();
  71. }
  72. return false;
  73. }
  74. var ajaxSubmit = function (options) {
  75. var $form = $(this);
  76. options = $.extend({
  77. method: $form.attr('method'),
  78. action: $form.attr('action'),
  79. refreshPjaxContainer: $form.data('refresh-pjax-container') || null,
  80. refresh: $form.data('refresh') || false,
  81. callback: $form.data('callback') || null,
  82. confirm: $form.data('confirm') || null
  83. }, options);
  84. var method = options.method,
  85. action = options.action,
  86. refreshPjaxContainer = options.refreshPjaxContainer,
  87. refresh = options.refresh,
  88. callback = options.callback;
  89. var fn = function () {
  90. $.modal.loading();
  91. $.ajax({
  92. url: action,
  93. method: method,
  94. data: $form.serialize(),
  95. dataType: 'json',
  96. success: function (res) {
  97. if (res.status != undefined && res.status == 0) {
  98. $.modal.error(res.message || '操作失败');
  99. return;
  100. }
  101. if (!res.message) {
  102. res.message = '操作成功';
  103. }
  104. $.modal.notify(res.message, 'success', function () {
  105. $form.trigger('reset');
  106. if (refreshPjaxContainer) {
  107. $.pjax.reload({container:'#' + refreshPjaxContainer, timeout: 0});
  108. }
  109. if (refresh) {
  110. location.reload();
  111. }
  112. if (callback) {
  113. callback(res);
  114. }
  115. });
  116. },
  117. complete: function () {
  118. $.modal.unloading();
  119. }
  120. });
  121. }
  122. if (options.confirm != null) {
  123. $.modal.confirm(options.confirm, function () {
  124. fn();
  125. });
  126. } else {
  127. fn();
  128. }
  129. return false;
  130. }
  131. $(function () {
  132. $(document).on('click', "a[target='_blank']", function () {
  133. if ($(this).attr('no-iframe')) {
  134. return true;
  135. }
  136. if (parent != window) {
  137. parent.admin_tab(this);
  138. return false;
  139. }
  140. });
  141. $(".fancybox").fancybox({
  142. prevEffect : 'none',
  143. nextEffect : 'none',
  144. helpers : {
  145. title : {
  146. type: 'float'
  147. },
  148. buttons : {},
  149. thumbs : {
  150. width : 50,
  151. height : 50
  152. }
  153. }
  154. });
  155. $('[data-toggle=popover]').popover();
  156. $('[data-toggle=tooltip]').tooltip();
  157. $('.modal-dialog').drags({handle:".modal-header"});
  158. $(document).on('click', 'a[data-ajax=1]', function () {
  159. return ajaxLink.call(this);
  160. });
  161. $(document).on('beforeSubmit', 'form[data-ajax=1]', function () {
  162. return ajaxSubmit.call(this);
  163. });
  164. var elems = Array.prototype.slice.call(document.querySelectorAll('[data-toggle=switcher]'));
  165. elems.forEach(function(html) {
  166. var disabled = !!$(html).data('switcher-disabled');
  167. var size = $(html).data('switcher-size') || 'small';
  168. var switchery = new Switchery(html,{ size: size, disabled:disabled, disabledOpacity:0.5 });
  169. $(html).data('switchery', switchery);
  170. });
  171. })
  172. String.prototype.addQueryParams = function(params) {
  173. var split = '?';
  174. if (this.indexOf('?') > -1) {
  175. split = '&';
  176. }
  177. var queryParams = '';
  178. for(var i in params) {
  179. queryParams += i + '=' + params[i] + '&';
  180. }
  181. queryParams = queryParams.substr(0, queryParams.length -1)
  182. return this + split + queryParams;
  183. }