fast.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. define(['jquery', 'bootstrap', 'toastr', 'layer', 'lang'], function ($, undefined, Toastr, Layer, Lang) {
  2. var Fast = {
  3. config: {
  4. //toastr默认配置
  5. toastr: {
  6. "closeButton": true,
  7. "debug": false,
  8. "newestOnTop": false,
  9. "progressBar": false,
  10. "positionClass": "toast-top-center",
  11. "preventDuplicates": false,
  12. "onclick": null,
  13. "showDuration": "300",
  14. "hideDuration": "1000",
  15. "timeOut": "5000",
  16. "extendedTimeOut": "1000",
  17. "showEasing": "swing",
  18. "hideEasing": "linear",
  19. "showMethod": "fadeIn",
  20. "hideMethod": "fadeOut"
  21. }
  22. },
  23. events: {
  24. //请求成功的回调
  25. onAjaxSuccess: function (ret, onAjaxSuccess) {
  26. var data = typeof ret.data !== 'undefined' ? ret.data : null;
  27. var msg = typeof ret.msg !== 'undefined' && ret.msg ? ret.msg : __('Operation completed');
  28. if (typeof onAjaxSuccess === 'function') {
  29. var result = onAjaxSuccess.call(this, data, ret);
  30. if (result === false)
  31. return;
  32. }
  33. Toastr.success(msg);
  34. },
  35. //请求错误的回调
  36. onAjaxError: function (ret, onAjaxError) {
  37. var data = typeof ret.data !== 'undefined' ? ret.data : null;
  38. if (typeof onAjaxError === 'function') {
  39. var result = onAjaxError.call(this, data, ret);
  40. if (result === false) {
  41. return;
  42. }
  43. }
  44. Toastr.error(ret.msg + "(code:" + ret.code + ")");
  45. },
  46. //服务器响应数据后
  47. onAjaxResponse: function (response) {
  48. try {
  49. var ret = typeof response === 'object' ? response : JSON.parse(response);
  50. if (!ret.hasOwnProperty('code')) {
  51. $.extend(ret, {code: -2, msg: response, data: null});
  52. }
  53. } catch (e) {
  54. var ret = {code: -1, msg: e.message, data: null};
  55. }
  56. return ret;
  57. }
  58. },
  59. api: {
  60. //发送Ajax请求
  61. ajax: function (options, success, error) {
  62. options = typeof options === 'string' ? {url: options} : options;
  63. var index = Layer.load();
  64. options = $.extend({
  65. type: "POST",
  66. dataType: "json",
  67. success: function (ret) {
  68. Layer.close(index);
  69. ret = Fast.events.onAjaxResponse(ret);
  70. if (ret.code === 1) {
  71. Fast.events.onAjaxSuccess(ret, success);
  72. } else {
  73. Fast.events.onAjaxError(ret, error);
  74. }
  75. },
  76. error: function (xhr) {
  77. Layer.close(index);
  78. var ret = {code: xhr.status, msg: xhr.statusText, data: null};
  79. Fast.events.onAjaxError(ret, error);
  80. }
  81. }, options);
  82. $.ajax(options);
  83. },
  84. //修复URL
  85. fixurl: function (url) {
  86. if (url.substr(0, 1) !== "/") {
  87. var r = new RegExp('^(?:[a-z]+:)?//', 'i');
  88. if (!r.test(url)) {
  89. url = Config.moduleurl + "/" + url;
  90. }
  91. } else if (url.substr(0, 8) === "/addons/") {
  92. url = Config.__PUBLIC__.replace(/(\/*$)/g, "") + url;
  93. }
  94. return url;
  95. },
  96. //获取修复后可访问的cdn链接
  97. cdnurl: function (url) {
  98. return /^(?:[a-z]+:)?\/\//i.test(url) ? url : Config.upload.cdnurl + url;
  99. },
  100. //查询Url参数
  101. query: function (name, url) {
  102. if (!url) {
  103. url = window.location.href;
  104. }
  105. name = name.replace(/[\[\]]/g, "\\$&");
  106. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  107. results = regex.exec(url);
  108. if (!results)
  109. return null;
  110. if (!results[2])
  111. return '';
  112. return decodeURIComponent(results[2].replace(/\+/g, " "));
  113. },
  114. //打开一个弹出窗口
  115. open: function (url, title, options) {
  116. title = title ? title : "";
  117. url = Fast.api.fixurl(url);
  118. url = url + (url.indexOf("?") > -1 ? "&" : "?") + "dialog=1";
  119. var area = [$(window).width() > 800 ? '800px' : '95%', $(window).height() > 600 ? '600px' : '95%'];
  120. options = $.extend({
  121. type: 2,
  122. title: title,
  123. shadeClose: true,
  124. shade: false,
  125. maxmin: true,
  126. moveOut: true,
  127. area: area,
  128. content: url,
  129. zIndex: Layer.zIndex,
  130. success: function (layero, index) {
  131. var that = this;
  132. //存储callback事件
  133. $(layero).data("callback", that.callback);
  134. //$(layero).removeClass("layui-layer-border");
  135. Layer.setTop(layero);
  136. var frame = Layer.getChildFrame('html', index);
  137. var layerfooter = frame.find(".layer-footer");
  138. Fast.api.layerfooter(layero, index, that);
  139. //绑定事件
  140. if (layerfooter.size() > 0) {
  141. // 监听窗口内的元素及属性变化
  142. // Firefox和Chrome早期版本中带有前缀
  143. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
  144. // 选择目标节点
  145. var target = layerfooter[0];
  146. // 创建观察者对象
  147. var observer = new MutationObserver(function (mutations) {
  148. Fast.api.layerfooter(layero, index, that);
  149. mutations.forEach(function (mutation) {
  150. });
  151. });
  152. // 配置观察选项:
  153. var config = {attributes: true, childList: true, characterData: true, subtree: true}
  154. // 传入目标节点和观察选项
  155. observer.observe(target, config);
  156. // 随后,你还可以停止观察
  157. // observer.disconnect();
  158. }
  159. }
  160. }, options ? options : {});
  161. if ($(window).width() < 480 || (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream && top.$(".tab-pane.active").size() > 0)) {
  162. options.area = [top.$(".tab-pane.active").width() + "px", top.$(".tab-pane.active").height() + "px"];
  163. options.offset = [top.$(".tab-pane.active").scrollTop() + "px", "0px"];
  164. }
  165. Layer.open(options);
  166. return false;
  167. },
  168. //关闭窗口并回传数据
  169. close: function (data) {
  170. var index = parent.Layer.getFrameIndex(window.name);
  171. var callback = parent.$("#layui-layer" + index).data("callback");
  172. //再执行关闭
  173. parent.Layer.close(index);
  174. //再调用回传函数
  175. if (typeof callback === 'function') {
  176. callback.call(undefined, data);
  177. }
  178. },
  179. layerfooter: function (layero, index, that) {
  180. var frame = Layer.getChildFrame('html', index);
  181. var layerfooter = frame.find(".layer-footer");
  182. if (layerfooter.size() > 0) {
  183. $(".layui-layer-footer", layero).remove();
  184. var footer = $("<div />").addClass('layui-layer-btn layui-layer-footer');
  185. footer.html(layerfooter.html());
  186. if ($(".row", footer).size() === 0) {
  187. $(">", footer).wrapAll("<div class='row'></div>");
  188. }
  189. footer.insertAfter(layero.find('.layui-layer-content'));
  190. //绑定事件
  191. footer.on("click", ".btn", function () {
  192. if ($(this).hasClass("disabled") || $(this).parent().hasClass("disabled")) {
  193. return;
  194. }
  195. $(".btn:eq(" + $(this).index() + ")", layerfooter).trigger("click");
  196. });
  197. var titHeight = layero.find('.layui-layer-title').outerHeight() || 0;
  198. var btnHeight = layero.find('.layui-layer-btn').outerHeight() || 0;
  199. //重设iframe高度
  200. $("iframe", layero).height(layero.height() - titHeight - btnHeight);
  201. }
  202. //修复iOS下弹出窗口的高度和iOS下iframe无法滚动的BUG
  203. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  204. var titHeight = layero.find('.layui-layer-title').outerHeight() || 0;
  205. var btnHeight = layero.find('.layui-layer-btn').outerHeight() || 0;
  206. $("iframe", layero).parent().css("height", layero.height() - titHeight - btnHeight);
  207. $("iframe", layero).css("height", "100%");
  208. }
  209. },
  210. success: function (options, callback) {
  211. var type = typeof options === 'function';
  212. if (type) {
  213. callback = options;
  214. }
  215. return Layer.msg(__('Operation completed'), $.extend({
  216. offset: 0, icon: 1
  217. }, type ? {} : options), callback);
  218. },
  219. error: function (options, callback) {
  220. var type = typeof options === 'function';
  221. if (type) {
  222. callback = options;
  223. }
  224. return Layer.msg(__('Operation failed'), $.extend({
  225. offset: 0, icon: 2
  226. }, type ? {} : options), callback);
  227. },
  228. toastr: Toastr,
  229. layer: Layer
  230. },
  231. lang: function () {
  232. var args = arguments,
  233. string = args[0],
  234. i = 1;
  235. string = string.toLowerCase();
  236. //string = typeof Lang[string] != 'undefined' ? Lang[string] : string;
  237. if (typeof Lang[string] != 'undefined') {
  238. if (typeof Lang[string] == 'object')
  239. return Lang[string];
  240. string = Lang[string];
  241. } else if (string.indexOf('.') !== -1 && false) {
  242. var arr = string.split('.');
  243. var current = Lang[arr[0]];
  244. for (var i = 1; i < arr.length; i++) {
  245. current = typeof current[arr[i]] != 'undefined' ? current[arr[i]] : '';
  246. if (typeof current != 'object')
  247. break;
  248. }
  249. if (typeof current == 'object')
  250. return current;
  251. string = current;
  252. } else {
  253. string = args[0];
  254. }
  255. return string.replace(/%((%)|s|d)/g, function (m) {
  256. // m is the matched format, e.g. %s, %d
  257. var val = null;
  258. if (m[2]) {
  259. val = m[2];
  260. } else {
  261. val = args[i];
  262. // A switch statement so that the formatter can be extended. Default is %s
  263. switch (m) {
  264. case '%d':
  265. val = parseFloat(val);
  266. if (isNaN(val)) {
  267. val = 0;
  268. }
  269. break;
  270. }
  271. i++;
  272. }
  273. return val;
  274. });
  275. },
  276. init: function () {
  277. // 对相对地址进行处理
  278. $.ajaxSetup({
  279. beforeSend: function (xhr, setting) {
  280. setting.url = Fast.api.fixurl(setting.url);
  281. }
  282. });
  283. Layer.config({
  284. skin: 'layui-layer-fast'
  285. });
  286. // 绑定ESC关闭窗口事件
  287. $(window).keyup(function (e) {
  288. if (e.keyCode == 27) {
  289. if ($(".layui-layer").size() > 0) {
  290. var index = 0;
  291. $(".layui-layer").each(function () {
  292. index = Math.max(index, parseInt($(this).attr("times")));
  293. });
  294. if (index) {
  295. Layer.close(index);
  296. }
  297. }
  298. }
  299. });
  300. //公共代码
  301. //配置Toastr的参数
  302. Toastr.options = Fast.config.toastr;
  303. }
  304. };
  305. //将Layer暴露到全局中去
  306. window.Layer = Layer;
  307. //将Toastr暴露到全局中去
  308. window.Toastr = Toastr;
  309. //将语言方法暴露到全局中去
  310. window.__ = Fast.lang;
  311. //将Fast渲染至全局
  312. window.Fast = Fast;
  313. //默认初始化执行的代码
  314. Fast.init();
  315. return Fast;
  316. });