layer.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. mbui.define([], function (exports) {
  2. var doc = document, query = 'querySelectorAll', claname = 'getElementsByClassName', S = function (s) {
  3. return doc[query](s);
  4. };
  5. var template = {
  6. warning: function (text) {
  7. //警告提示
  8. return `<div class="mbui-toptips mbui-toptips-warning fixed" id="${classs}${index}">
  9. <i class="mbui-toptips-warning-icon"></i><span>${text}</span>
  10. </div>`;
  11. },
  12. error: function (text) {
  13. //失败提示
  14. return `<div class="mbui-toptips mbui-toptips-error fixed" id="${classs}${index}">
  15. <i class="mbui-toptips-error-icon"></i><span>${text}</span>
  16. </div>`;
  17. },
  18. toast: function (text) {
  19. //轻提示
  20. return `<div class="mbui-toast" id="${classs}${index}">
  21. <div class="mbui-toast-backdrop"></div>
  22. <div class="mbui-toast-dialog">
  23. <div class="mbui-toast-content">
  24. <span>${text}</span>
  25. </div>
  26. </div>
  27. </div>`;
  28. },
  29. loading: function (text) {
  30. //轻提示
  31. return `<div class="mbui-toast mbui-toast-loading" id="${classs}${index}">
  32. <div class="mbui-toast-backdrop"></div>
  33. <div class="mbui-toast-dialog">
  34. <div class="mbui-toast-content">
  35. <span>
  36. <div class="mbui-toast-loading_icon">
  37. <div class="loading-icon-leaf loading-icon-leaf_0"></div>
  38. <div class="loading-icon-leaf loading-icon-leaf_1"></div>
  39. <div class="loading-icon-leaf loading-icon-leaf_2"></div>
  40. <div class="loading-icon-leaf loading-icon-leaf_3"></div>
  41. <div class="loading-icon-leaf loading-icon-leaf_4"></div>
  42. <div class="loading-icon-leaf loading-icon-leaf_5"></div>
  43. <div class="loading-icon-leaf loading-icon-leaf_6"></div>
  44. <div class="loading-icon-leaf loading-icon-leaf_7"></div>
  45. <div class="loading-icon-leaf loading-icon-leaf_8"></div>
  46. <div class="loading-icon-leaf loading-icon-leaf_9"></div>
  47. <div class="loading-icon-leaf loading-icon-leaf_10"></div>
  48. <div class="loading-icon-leaf loading-icon-leaf_11"></div>
  49. </div>
  50. ${text}
  51. </span>
  52. </div>
  53. </div>
  54. </div>`;
  55. },
  56. success: function (text) {
  57. //轻提示
  58. return `<div class="mbui-toast mbui-toast-success" id="${classs}${index}">
  59. <div class="mbui-toast-backdrop"></div>
  60. <div class="mbui-toast-dialog">
  61. <div class="mbui-toast-content">
  62. <span><i class="mbui-toast-success-icon"></i>${text}</span>
  63. </div>
  64. </div>
  65. </div>`;
  66. }
  67. }
  68. //默认配置
  69. var config = {
  70. type: 1,
  71. shade: true,
  72. shadeClose: false,
  73. fixed: true
  74. };
  75. var ready = {
  76. extend: function (obj) {
  77. var newobj = JSON.parse(JSON.stringify(config));
  78. for (var i in obj) {
  79. newobj[i] = obj[i];
  80. }
  81. return newobj;
  82. },
  83. timer: {}, end: {}
  84. };
  85. //点触事件
  86. ready.touch = function (elem, fn) {
  87. elem.addEventListener('click', function (e) {
  88. fn.call(this, e);
  89. }, false);
  90. };
  91. var index = 0, classs = ['mbui-layer'], Layer = function (options) {
  92. var that = this;
  93. that.config = ready.extend(options);
  94. that.view();
  95. };
  96. Layer.prototype.view = function () {
  97. var that = this, config = that.config, layerbox = doc.createElement('div');
  98. that.id = layerbox.id = classs[0] + index;
  99. layerbox.setAttribute('class', classs[0] + ' ' + classs[0] + (config.type || 0));
  100. layerbox.setAttribute('index', index);
  101. //标题区域
  102. var title = (function () {
  103. var titype = typeof config.title === 'object';
  104. return config.title
  105. ? '<div class="modal-header" style="' + (titype ? config.title[1] : '') + '">' + (titype ? config.title[0] : config.title) + '</div>' : '';
  106. }());
  107. //按钮区域
  108. var button = (function () {
  109. typeof config.btn === 'string' && (config.btn = [config.btn]);
  110. var btns = (config.btn || []).length, btndom;
  111. if (btns === 0 || !config.btn) {
  112. return '';
  113. }
  114. btndom = '<button type="button" types="1" class="btn btn-block active">' + config.btn[0] + '</button>'
  115. if (btns === 2) {
  116. btndom = '<a href="javascript:void(0);" types="0" class="btn">' + config.btn[1] + '</a><button type="button" types="1" class="btn active">' + config.btn[0] + '</button>';
  117. }
  118. return '<div class="modal-footer clearfix">' + btndom + '</div>';
  119. }());
  120. if (!config.fixed) {
  121. config.top = config.hasOwnProperty('top') ? config.top : 100;
  122. config.style = config.style || '';
  123. config.style += ' top:' + (doc.body.scrollTop + config.top) + 'px';
  124. }
  125. if (config.type === 0) {
  126. config.shade = false;
  127. let content = template[config.template];
  128. layerbox.innerHTML = content(config.content);
  129. }
  130. if (config.type === 2) {
  131. config.content = '<div class="form-hairlines"><input type="text" id="prompt' + index + '" class="mbui-input" placeholder="请输入内容"></div>';
  132. }
  133. if (config.type === 3) {
  134. config.className = 'modal-view top';
  135. config.content = '<div style="padding:0 16px 16px;"><textarea rows="5" id="textarea' + index + '" class="mbui-textarea" placeholder="请输入内容"></textarea></div>';
  136. }
  137. if (config.type === 4) {
  138. config.className = 'modal-actionsheet bottom';
  139. }
  140. if (config.type === 5) {
  141. config.className = 'modal-view top';
  142. }
  143. if (config.type > 0) {
  144. layerbox.innerHTML = (config.shade ? '<div ' + (typeof config.shade === 'string' ? 'style="' + config.shade + '"' : '') + ' class="mbui-layershade"></div>' : '')
  145. + '<div class="mbui-layermain" ' + (!config.fixed ? 'style="position:static;"' : '') + '>'
  146. + '<div class="modal-dialog">'
  147. + '<div class="modal-content ' + (config.className ? config.className : '') + '" ' + (config.style ? 'style="' + config.style + '"' : '') + '>'
  148. + title
  149. + '<div class="modal-body">' + config.content + '</div>'
  150. + button
  151. + '</div>'
  152. + '</div>'
  153. + '</div>';
  154. }
  155. document.body.appendChild(layerbox);
  156. var elem = that.elem = S('#' + that.id)[0];
  157. config.success && config.success(elem,index);
  158. that.index = index++;
  159. that.action(config, elem);
  160. };
  161. Layer.prototype.action = function (config, elem) {
  162. var that = this;
  163. //自动关闭
  164. if (config.time && config.time > 0) {
  165. ready.timer[that.index] = setTimeout(function () {
  166. layer.close(that.index);
  167. }, config.time * 1000);
  168. }
  169. //确认取消
  170. var btn = function () {
  171. var type = this.getAttribute('types');
  172. if (type == 0) {
  173. config.no && config.no();
  174. layer.close(that.index);
  175. } else {
  176. config.yes ? config.yes(that.index) : layer.close(that.index);
  177. }
  178. };
  179. if (config.btn) {
  180. var btns = elem[claname]('modal-footer')[0].children, btnlen = btns.length;
  181. for (var ii = 0; ii < btnlen; ii++) {
  182. ready.touch(btns[ii], btn);
  183. }
  184. }
  185. //点遮罩关闭
  186. if (config.shade && config.shadeClose) {
  187. var shade = elem[claname]('mbui-layershade')[0];
  188. ready.touch(shade, function () {
  189. layer.close(that.index, config.end);
  190. });
  191. }
  192. config.end && (ready.end[that.index] = config.end);
  193. };
  194. var layer = {
  195. v: '1.0',
  196. index: index,
  197. //核心方法
  198. open: function (options) {
  199. var o = new Layer(options || {});
  200. return o.index;
  201. },
  202. alert: function (txt) {
  203. layer.open({
  204. type: 1,
  205. title: '提示',
  206. content: txt,
  207. btn: ['确定']
  208. });
  209. },
  210. confirm: function (txt, fun) {
  211. layer.open({
  212. type: 1,
  213. title: '温馨提示',
  214. content: txt,
  215. btn: ['确定', '取消'],
  216. yes: function (index) {
  217. fun ? fun(index) : layer.close(index);
  218. }
  219. });
  220. },
  221. prompt: function (title,fun,txt) {
  222. layer.open({
  223. type: 2,
  224. title: title,
  225. content: txt,
  226. btn: ['确定', '取消'],
  227. yes: function (index) {
  228. let val = $('#prompt' + index).val();
  229. fun ? fun(val,index) : layer.close(index);
  230. }
  231. });
  232. },
  233. textarea: function (title,fun,txt) {
  234. layer.open({
  235. type: 3,
  236. title: title,
  237. content: txt,
  238. btn: ['确定', '取消'],
  239. yes: function (index) {
  240. let val = $('#textarea' + index).val();
  241. fun ? fun(val,index) : layer.close(index);
  242. }
  243. });
  244. },
  245. msg: function (txt = '', time = 3, template = 'toast') {
  246. let msg = layer.open({
  247. type: 0,
  248. content: txt,
  249. template: template,
  250. time: time
  251. });
  252. return msg;
  253. },
  254. success: function (txt) {
  255. layer.msg(txt, 3, 'success');
  256. },
  257. warning: function (txt) {
  258. layer.msg(txt, 3, 'warning');
  259. },
  260. error: function (txt) {
  261. layer.msg(txt, 3, 'error');
  262. },
  263. loading: function (txt) {
  264. let loading = layer.msg(txt, 3, 'loading');
  265. return loading;
  266. },
  267. photo: function (url) {
  268. var $image = $('<div class="layer-image-photo"><img src="' + url + '"></div>');
  269. $('body').append($image);
  270. $image.fadeIn();
  271. $image.click(function () {
  272. $(this).fadeOut(function () {
  273. $(this).remove();
  274. });
  275. });
  276. },
  277. close: function (index) {
  278. var ibox = S('#' + classs[0] + index)[0];
  279. if (!ibox) return;
  280. ibox.innerHTML = '';
  281. doc.body.removeChild(ibox);
  282. clearTimeout(ready.timer[index]);
  283. delete ready.timer[index];
  284. typeof ready.end[index] === 'function' && ready.end[index]();
  285. delete ready.end[index];
  286. },
  287. //关闭所有layer层
  288. closeAll: function () {
  289. var boxs = doc[claname](classs[0]);
  290. for (var i = 0, len = boxs.length; i < len; i++) {
  291. layer.close((boxs[0].getAttribute('index') | 0));
  292. }
  293. },
  294. copy:function(content) {
  295. var save = function(e){
  296. e.clipboardData.setData('text/plain', content);
  297. e.preventDefault();
  298. }
  299. document.addEventListener('copy', save);
  300. document.execCommand('copy');
  301. document.removeEventListener('copy',save);
  302. if (content != '') {
  303. layer.msg('复制成功');
  304. }
  305. }
  306. };
  307. // 输出接口
  308. exports('layer', layer);
  309. });