fast.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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);
  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;
  64. if (typeof options.loading === 'undefined' || options.loading) {
  65. index = Layer.load(options.loading || 0);
  66. }
  67. options = $.extend({
  68. type: "POST",
  69. dataType: "json",
  70. success: function (ret) {
  71. index && Layer.close(index);
  72. ret = Fast.events.onAjaxResponse(ret);
  73. if (ret.code === 1) {
  74. Fast.events.onAjaxSuccess(ret, success);
  75. } else {
  76. Fast.events.onAjaxError(ret, error);
  77. }
  78. },
  79. error: function (xhr) {
  80. index && Layer.close(index);
  81. var ret = {code: xhr.status, msg: xhr.statusText, data: null};
  82. Fast.events.onAjaxError(ret, error);
  83. }
  84. }, options);
  85. return $.ajax(options);
  86. },
  87. //修复URL
  88. fixurl: function (url) {
  89. if (url.substr(0, 1) !== "/") {
  90. var r = new RegExp('^(?:[a-z]+:)?//', 'i');
  91. if (!r.test(url)) {
  92. url = Config.moduleurl + "/" + url;
  93. }
  94. } else if (url.substr(0, 8) === "/addons/") {
  95. url = Config.__PUBLIC__.replace(/(\/*$)/g, "") + url;
  96. }
  97. return url;
  98. },
  99. //获取修复后可访问的cdn链接
  100. cdnurl: function (url, domain) {
  101. var rule = new RegExp("^((?:[a-z]+:)?\\/\\/|data:image\\/)", "i");
  102. var url = rule.test(url) ? url : Config.upload.cdnurl + url;
  103. if (domain && !rule.test(url)) {
  104. domain = typeof domain === 'string' ? domain : location.origin;
  105. url = domain + url;
  106. }
  107. return url;
  108. },
  109. //查询Url参数
  110. query: function (name, url) {
  111. if (!url) {
  112. url = window.location.href;
  113. }
  114. name = name.replace(/[\[\]]/g, "\\$&");
  115. var regex = new RegExp("[?&/]" + name + "([=/]([^&#/?]*)|&|#|$)"),
  116. results = regex.exec(url);
  117. if (!results)
  118. return null;
  119. if (!results[2])
  120. return '';
  121. return decodeURIComponent(results[2].replace(/\+/g, " "));
  122. },
  123. //打开一个弹出窗口
  124. open: function (url, title, options) {
  125. title = options && options.title ? options.title : (title ? title : "");
  126. url = Fast.api.fixurl(url);
  127. url = url + (url.indexOf("?") > -1 ? "&" : "?") + "dialog=1";
  128. var area = Fast.config.openArea != undefined ? Fast.config.openArea : [$(window).width() > 800 ? '800px' : '95%', $(window).height() > 600 ? '600px' : '95%'];
  129. options = $.extend({
  130. type: 2,
  131. title: title,
  132. shadeClose: true,
  133. shade: false,
  134. maxmin: true,
  135. moveOut: true,
  136. area: area,
  137. content: url,
  138. zIndex: Layer.zIndex,
  139. success: function (layero, index) {
  140. var that = this;
  141. //存储callback事件
  142. $(layero).data("callback", that.callback);
  143. //$(layero).removeClass("layui-layer-border");
  144. Layer.setTop(layero);
  145. try {
  146. var frame = Layer.getChildFrame('html', index);
  147. var layerfooter = frame.find(".layer-footer");
  148. Fast.api.layerfooter(layero, index, that);
  149. //绑定事件
  150. if (layerfooter.size() > 0) {
  151. // 监听窗口内的元素及属性变化
  152. // Firefox和Chrome早期版本中带有前缀
  153. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  154. if (MutationObserver) {
  155. // 选择目标节点
  156. var target = layerfooter[0];
  157. // 创建观察者对象
  158. var observer = new MutationObserver(function (mutations) {
  159. Fast.api.layerfooter(layero, index, that);
  160. mutations.forEach(function (mutation) {
  161. });
  162. });
  163. // 配置观察选项:
  164. var config = {attributes: true, childList: true, characterData: true, subtree: true}
  165. // 传入目标节点和观察选项
  166. observer.observe(target, config);
  167. // 随后,你还可以停止观察
  168. // observer.disconnect();
  169. }
  170. }
  171. } catch (e) {
  172. }
  173. if ($(layero).height() > $(window).height()) {
  174. //当弹出窗口大于浏览器可视高度时,重定位
  175. Layer.style(index, {
  176. top: 0,
  177. height: $(window).height()
  178. });
  179. }
  180. }
  181. }, options ? options : {});
  182. if ($(window).width() < 480 || (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream && top.$(".tab-pane.active").size() > 0)) {
  183. options.area = [top.$(".tab-pane.active").width() + "px", top.$(".tab-pane.active").height() + "px"];
  184. options.offset = [top.$(".tab-pane.active").scrollTop() + "px", "0px"];
  185. }
  186. return Layer.open(options);
  187. },
  188. //关闭窗口并回传数据
  189. close: function (data) {
  190. var index = parent.Layer.getFrameIndex(window.name);
  191. var callback = parent.$("#layui-layer" + index).data("callback");
  192. //再执行关闭
  193. parent.Layer.close(index);
  194. //再调用回传函数
  195. if (typeof callback === 'function') {
  196. callback.call(undefined, data);
  197. }
  198. },
  199. layerfooter: function (layero, index, that) {
  200. var frame = Layer.getChildFrame('html', index);
  201. var layerfooter = frame.find(".layer-footer");
  202. if (layerfooter.size() > 0) {
  203. $(".layui-layer-footer", layero).remove();
  204. var footer = $("<div />").addClass('layui-layer-btn layui-layer-footer');
  205. footer.html(layerfooter.html());
  206. if ($(".row", footer).size() === 0) {
  207. $(">", footer).wrapAll("<div class='row'></div>");
  208. }
  209. footer.insertAfter(layero.find('.layui-layer-content'));
  210. //绑定事件
  211. footer.on("click", ".btn", function () {
  212. if ($(this).hasClass("disabled") || $(this).parent().hasClass("disabled")) {
  213. return;
  214. }
  215. var index = footer.find('.btn').index(this);
  216. $(".btn:eq(" + index + ")", layerfooter).trigger("click");
  217. });
  218. var titHeight = layero.find('.layui-layer-title').outerHeight() || 0;
  219. var btnHeight = layero.find('.layui-layer-btn').outerHeight() || 0;
  220. //重设iframe高度
  221. $("iframe", layero).height(layero.height() - titHeight - btnHeight);
  222. }
  223. //修复iOS下弹出窗口的高度和iOS下iframe无法滚动的BUG
  224. if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream) {
  225. var titHeight = layero.find('.layui-layer-title').outerHeight() || 0;
  226. var btnHeight = layero.find('.layui-layer-btn').outerHeight() || 0;
  227. $("iframe", layero).parent().css("height", layero.height() - titHeight - btnHeight);
  228. $("iframe", layero).css("height", "100%");
  229. }
  230. },
  231. success: function (options, callback) {
  232. var type = typeof options === 'function';
  233. if (type) {
  234. callback = options;
  235. }
  236. return Layer.msg(__('Operation completed'), $.extend({
  237. offset: 0, icon: 1
  238. }, type ? {} : options), callback);
  239. },
  240. error: function (options, callback) {
  241. var type = typeof options === 'function';
  242. if (type) {
  243. callback = options;
  244. }
  245. return Layer.msg(__('Operation failed'), $.extend({
  246. offset: 0, icon: 2
  247. }, type ? {} : options), callback);
  248. },
  249. msg: function (message, url) {
  250. var callback = typeof url === 'function' ? url : function () {
  251. if (typeof url !== 'undefined' && url) {
  252. location.href = url;
  253. }
  254. };
  255. Layer.msg(message, {
  256. time: 2000
  257. }, callback);
  258. },
  259. toastr: Toastr,
  260. layer: Layer
  261. },
  262. lang: function () {
  263. var args = arguments,
  264. string = args[0],
  265. i = 1;
  266. string = string.toLowerCase();
  267. //string = typeof Lang[string] != 'undefined' ? Lang[string] : string;
  268. if (typeof Lang !== 'undefined' && typeof Lang[string] !== 'undefined') {
  269. if (typeof Lang[string] == 'object')
  270. return Lang[string];
  271. string = Lang[string];
  272. } else if (string.indexOf('.') !== -1 && false) {
  273. var arr = string.split('.');
  274. var current = Lang[arr[0]];
  275. for (var i = 1; i < arr.length; i++) {
  276. current = typeof current[arr[i]] != 'undefined' ? current[arr[i]] : '';
  277. if (typeof current != 'object')
  278. break;
  279. }
  280. if (typeof current == 'object')
  281. return current;
  282. string = current;
  283. } else {
  284. string = args[0];
  285. }
  286. return string.replace(/%((%)|s|d)/g, function (m) {
  287. // m is the matched format, e.g. %s, %d
  288. var val = null;
  289. if (m[2]) {
  290. val = m[2];
  291. } else {
  292. val = args[i];
  293. // A switch statement so that the formatter can be extended. Default is %s
  294. switch (m) {
  295. case '%d':
  296. val = parseFloat(val);
  297. if (isNaN(val)) {
  298. val = 0;
  299. }
  300. break;
  301. }
  302. i++;
  303. }
  304. return val;
  305. });
  306. },
  307. init: function () {
  308. // 对相对地址进行处理
  309. $.ajaxSetup({
  310. beforeSend: function (xhr, setting) {
  311. setting.url = Fast.api.fixurl(setting.url);
  312. }
  313. });
  314. Layer.config({
  315. skin: 'layui-layer-fast'
  316. });
  317. // 绑定ESC关闭窗口事件
  318. $(window).keyup(function (e) {
  319. if (e.keyCode == 27) {
  320. if ($(".layui-layer").size() > 0) {
  321. var index = 0;
  322. $(".layui-layer").each(function () {
  323. index = Math.max(index, parseInt($(this).attr("times")));
  324. });
  325. if (index) {
  326. Layer.close(index);
  327. }
  328. }
  329. }
  330. });
  331. //公共代码
  332. //配置Toastr的参数
  333. Toastr.options = Fast.config.toastr;
  334. }
  335. };
  336. //将Layer暴露到全局中去
  337. window.Layer = Layer;
  338. //将Toastr暴露到全局中去
  339. window.Toastr = Toastr;
  340. //将语言方法暴露到全局中去
  341. window.__ = Fast.lang;
  342. //将Fast渲染至全局
  343. window.Fast = Fast;
  344. //默认初始化执行的代码
  345. Fast.init();
  346. return Fast;
  347. });