fast.js 17 KB

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