notice.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /**
  2. * 消息通知模块
  3. * date:2019-04-21 License By http://easyweb.vip
  4. */
  5. layui.define([], function (exports) {
  6. var PLUGIN_NAME = 'iziToast'; // 样式类名
  7. var BODY = document.querySelector('body');
  8. var ISMOBILE = (/Mobi/.test(navigator.userAgent)) ? true : false;
  9. var MOBILEWIDTH = 568;
  10. var ISCHROME = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  11. var ISFIREFOX = typeof InstallTrigger !== 'undefined';
  12. var ACCEPTSTOUCH = 'ontouchstart' in document.documentElement;
  13. // 显示区域
  14. var POSITIONS = ['bottomRight', 'bottomLeft', 'bottomCenter', 'topRight', 'topLeft', 'topCenter', 'center'];
  15. // 默认主题
  16. var THEMES = {
  17. info: {
  18. color: 'blue',
  19. icon: 'ico-info'
  20. },
  21. success: {
  22. color: 'green',
  23. icon: 'ico-success'
  24. },
  25. warning: {
  26. color: 'orange',
  27. icon: 'ico-warning'
  28. },
  29. error: {
  30. color: 'red',
  31. icon: 'ico-error'
  32. },
  33. question: {
  34. color: 'yellow',
  35. icon: 'ico-question'
  36. }
  37. };
  38. var CONFIG = {}; // 全局配置
  39. // 默认配置
  40. var defaults = {
  41. id: null,
  42. className: '', // 自定义class,用空格分割
  43. title: '', // 标题
  44. titleColor: '', // 标题文字颜色
  45. titleSize: '', // 标题文字大小
  46. titleLineHeight: '', // 标题高度
  47. message: '', // 内容
  48. messageColor: '', // 内容文字颜色
  49. messageSize: '', // 内容文字大小
  50. messageLineHeight: '', // 内容高度
  51. backgroundColor: '', // 背景颜色
  52. theme: 'light', // dark
  53. color: '', // 背景颜色
  54. icon: '', // 图标
  55. iconText: '', // 图标文字
  56. iconColor: '', // 图标颜色
  57. iconUrl: null, // 图标地址
  58. image: '', // 是否显示图片
  59. imageWidth: 60, // 图片宽度
  60. maxWidth: null, // 最大宽度
  61. zindex: null, //
  62. layout: 2, // 布局类型
  63. balloon: false, // 气泡
  64. close: true, // 是否显示关闭按钮
  65. closeOnEscape: false,
  66. closeOnClick: false, // 点击关闭
  67. displayMode: 0, // 0无限制,1存在就不发出,2销毁之前
  68. position: 'topRight', // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter, center
  69. target: '', // 显示位置
  70. targetFirst: null, // 插入顺序
  71. timeout: 5000, // 关闭时间,false不自动关闭
  72. rtl: false, // 内容居右
  73. animateInside: false, // 进入动画效果
  74. drag: true, // 是否可滑动移除
  75. pauseOnHover: true, // 鼠标移入暂停进度条时间
  76. resetOnHover: false, // 鼠标移入重置进度条时间
  77. progressBar: true, // 是否显示进度条
  78. progressBarColor: '', // 进度条颜色
  79. progressBarEasing: 'linear', // 进度条动画效果
  80. overlay: false, // 是否显示遮罩层
  81. overlayClose: false, // 点击遮罩层是否关闭
  82. overlayColor: 'rgba(0, 0, 0, 0.1)', // 遮罩层颜色
  83. transitionIn: 'fadeInLeft', // bounceInLeft, bounceInRight, bounceInUp, bounceInDown, fadeIn, fadeInDown, fadeInUp, fadeInLeft, fadeInRight, flipInX
  84. transitionOut: 'fadeOutRight', // fadeOut, fadeOutUp, fadeOutDown, fadeOutLeft, fadeOutRight, flipOutX
  85. transitionInMobile: 'bounceInDown', // 移动端进入动画
  86. transitionOutMobile: 'fadeOutUp', // 移动端退出动画
  87. buttons: {}, // 操作按钮
  88. inputs: {}, // 输入框
  89. audio: '', // 音效
  90. onOpening: function () {
  91. },
  92. onOpened: function () {
  93. },
  94. onClosing: function () {
  95. },
  96. onClosed: function () {
  97. }
  98. };
  99. var $iziToast = {
  100. children: {},
  101. setSetting: function (ref, option, value) {
  102. $iziToast.children[ref][option] = value;
  103. },
  104. getSetting: function (ref, option) {
  105. return $iziToast.children[ref][option];
  106. },
  107. // 全局设置
  108. settings: function (options) {
  109. $iziToast.destroy(); // 全部销毁之前的通知
  110. CONFIG = options;
  111. defaults = extend(defaults, options || {});
  112. },
  113. // 关闭所有通知
  114. destroy: function () {
  115. forEach(document.querySelectorAll('.' + PLUGIN_NAME + '-overlay'), function (element, index) {
  116. element.remove();
  117. });
  118. forEach(document.querySelectorAll('.' + PLUGIN_NAME + '-wrapper'), function (element, index) {
  119. element.remove();
  120. });
  121. forEach(document.querySelectorAll('.' + PLUGIN_NAME), function (element, index) {
  122. element.remove();
  123. });
  124. this.children = {};
  125. // 移除事件监听
  126. document.removeEventListener(PLUGIN_NAME + '-opened', {}, false);
  127. document.removeEventListener(PLUGIN_NAME + '-opening', {}, false);
  128. document.removeEventListener(PLUGIN_NAME + '-closing', {}, false);
  129. document.removeEventListener(PLUGIN_NAME + '-closed', {}, false);
  130. document.removeEventListener('keyup', {}, false);
  131. CONFIG = {}; // 移除全局配置
  132. },
  133. // msg类型
  134. msg: function (msg, options) {
  135. if (options.icon == 4) {
  136. options.overlay = true;
  137. options.timeout = false;
  138. options.drag = false;
  139. options.displayMode = 0;
  140. }
  141. var icons = ['ico-success', 'ico-error', 'ico-warning', 'ico-load', 'ico-info'];
  142. options.icon = icons[options.icon - 1];
  143. var theme = {
  144. message: msg,
  145. position: 'topCenter',
  146. transitionIn: 'bounceInDown',
  147. transitionOut: 'fadeOut',
  148. transitionOutMobile: 'fadeOut',
  149. progressBar: false,
  150. close: false,
  151. layout: 1,
  152. audio: ''
  153. };
  154. var settings = extend(CONFIG, options || {});
  155. settings = extend(theme, settings || {});
  156. this.show(settings);
  157. }
  158. };
  159. // 关闭指定的通知
  160. $iziToast.hide = function (options, $toast, closedBy) {
  161. if (typeof $toast != 'object') {
  162. $toast = document.querySelector($toast);
  163. }
  164. var that = this;
  165. var settings = extend(this.children[$toast.getAttribute('data-iziToast-ref')], options || {});
  166. settings.closedBy = closedBy || null;
  167. delete settings.time.REMAINING;
  168. $toast.classList.add(PLUGIN_NAME + '-closing');
  169. // 移除遮罩层
  170. (function () {
  171. var $overlay = document.querySelector('.' + PLUGIN_NAME + '-overlay');
  172. if ($overlay !== null) {
  173. var refs = $overlay.getAttribute('data-iziToast-ref');
  174. refs = refs.split(',');
  175. var index = refs.indexOf(String(settings.ref));
  176. if (index !== -1) {
  177. refs.splice(index, 1);
  178. }
  179. $overlay.setAttribute('data-iziToast-ref', refs.join());
  180. if (refs.length === 0) {
  181. $overlay.classList.remove('fadeIn');
  182. $overlay.classList.add('fadeOut');
  183. setTimeout(function () {
  184. $overlay.remove();
  185. }, 700);
  186. }
  187. }
  188. })();
  189. // 移除动画
  190. if (settings.transitionIn) {
  191. $toast.classList.remove(settings.transitionIn);
  192. }
  193. if (settings.transitionInMobile) {
  194. $toast.classList.remove(settings.transitionInMobile);
  195. }
  196. if (ISMOBILE || window.innerWidth <= MOBILEWIDTH) {
  197. if (settings.transitionOutMobile)
  198. $toast.classList.add(settings.transitionOutMobile);
  199. } else {
  200. if (settings.transitionOut)
  201. $toast.classList.add(settings.transitionOut);
  202. }
  203. var H = $toast.parentNode.offsetHeight;
  204. $toast.parentNode.style.height = H + 'px';
  205. $toast.style.pointerEvents = 'none';
  206. if (!ISMOBILE || window.innerWidth > MOBILEWIDTH) {
  207. $toast.parentNode.style.transitionDelay = '0.2s';
  208. }
  209. try {
  210. var event = new CustomEvent(PLUGIN_NAME + '-closing', {detail: settings, bubbles: true, cancelable: true});
  211. document.dispatchEvent(event);
  212. } catch (ex) {
  213. console.warn(ex);
  214. }
  215. setTimeout(function () {
  216. $toast.parentNode.style.height = '0px';
  217. $toast.parentNode.style.overflow = '';
  218. setTimeout(function () {
  219. delete that.children[settings.ref];
  220. $toast.parentNode.remove();
  221. try {
  222. var event = new CustomEvent(PLUGIN_NAME + '-closed', {
  223. detail: settings,
  224. bubbles: true,
  225. cancelable: true
  226. });
  227. document.dispatchEvent(event);
  228. } catch (ex) {
  229. console.warn(ex);
  230. }
  231. if (typeof settings.onClosed !== 'undefined') {
  232. settings.onClosed.apply(null, [settings, $toast, closedBy]);
  233. }
  234. }, 1000);
  235. }, 200);
  236. // 回调关闭事件
  237. if (typeof settings.onClosing !== 'undefined') {
  238. settings.onClosing.apply(null, [settings, $toast, closedBy]);
  239. }
  240. };
  241. // 显示通知
  242. $iziToast.show = function (options) {
  243. var that = this;
  244. // Merge user options with defaults
  245. var settings = extend(CONFIG, options || {});
  246. settings = extend(defaults, settings);
  247. settings.time = {};
  248. if (settings.id === null) {
  249. settings.id = generateId(settings.title + settings.message + settings.color);
  250. }
  251. if (settings.displayMode == 1 || settings.displayMode == 'once') {
  252. try {
  253. if (document.querySelectorAll('.' + PLUGIN_NAME + '#' + settings.id).length > 0) {
  254. return false;
  255. }
  256. } catch (exc) {
  257. console.warn('[' + PLUGIN_NAME + '] Could not find an element with this selector: ' + '#' + settings.id + '. Try to set an valid id.');
  258. }
  259. }
  260. if (settings.displayMode == 2 || settings.displayMode == 'replace') {
  261. try {
  262. forEach(document.querySelectorAll('.' + PLUGIN_NAME + '#' + settings.id), function (element, index) {
  263. that.hide(settings, element, 'replaced');
  264. });
  265. } catch (exc) {
  266. console.warn('[' + PLUGIN_NAME + '] Could not find an element with this selector: ' + '#' + settings.id + '. Try to set an valid id.');
  267. }
  268. }
  269. settings.ref = new Date().getTime() + Math.floor((Math.random() * 10000000) + 1);
  270. $iziToast.children[settings.ref] = settings;
  271. var $DOM = {
  272. body: document.querySelector('body'),
  273. overlay: document.createElement('div'),
  274. toast: document.createElement('div'),
  275. toastBody: document.createElement('div'),
  276. toastTexts: document.createElement('div'),
  277. toastCapsule: document.createElement('div'),
  278. cover: document.createElement('div'),
  279. buttons: document.createElement('div'),
  280. inputs: document.createElement('div'),
  281. icon: !settings.iconUrl ? document.createElement('i') : document.createElement('img'),
  282. wrapper: null
  283. };
  284. $DOM.toast.setAttribute('data-iziToast-ref', settings.ref);
  285. $DOM.toast.appendChild($DOM.toastBody);
  286. $DOM.toastCapsule.appendChild($DOM.toast);
  287. // CSS Settings
  288. (function () {
  289. $DOM.toast.classList.add(PLUGIN_NAME);
  290. $DOM.toast.classList.add(PLUGIN_NAME + '-opening');
  291. $DOM.toastCapsule.classList.add(PLUGIN_NAME + '-capsule');
  292. $DOM.toastBody.classList.add(PLUGIN_NAME + '-body');
  293. $DOM.toastTexts.classList.add(PLUGIN_NAME + '-texts');
  294. if (ISMOBILE || window.innerWidth <= MOBILEWIDTH) {
  295. if (settings.transitionInMobile)
  296. $DOM.toast.classList.add(settings.transitionInMobile);
  297. } else {
  298. if (settings.transitionIn)
  299. $DOM.toast.classList.add(settings.transitionIn);
  300. }
  301. if (settings.className) {
  302. var classes = settings.className.split(' ');
  303. forEach(classes, function (value, index) {
  304. $DOM.toast.classList.add(value);
  305. });
  306. }
  307. if (settings.id) {
  308. $DOM.toast.id = settings.id;
  309. }
  310. if (settings.rtl) {
  311. $DOM.toast.classList.add(PLUGIN_NAME + '-rtl');
  312. $DOM.toast.setAttribute('dir', 'rtl');
  313. }
  314. if (settings.layout > 1) {
  315. $DOM.toast.classList.add(PLUGIN_NAME + '-layout' + settings.layout);
  316. }
  317. if (settings.balloon) {
  318. $DOM.toast.classList.add(PLUGIN_NAME + '-balloon');
  319. }
  320. if (settings.maxWidth) {
  321. if (!isNaN(settings.maxWidth)) {
  322. $DOM.toast.style.maxWidth = settings.maxWidth + 'px';
  323. } else {
  324. $DOM.toast.style.maxWidth = settings.maxWidth;
  325. }
  326. }
  327. if (settings.theme !== '' || settings.theme !== 'light') {
  328. $DOM.toast.classList.add(PLUGIN_NAME + '-theme-' + settings.theme);
  329. }
  330. if (settings.color) { //#, rgb, rgba, hsl
  331. if (isColor(settings.color)) {
  332. $DOM.toast.style.background = settings.color;
  333. } else {
  334. $DOM.toast.classList.add(PLUGIN_NAME + '-color-' + settings.color);
  335. }
  336. }
  337. if (settings.backgroundColor) {
  338. $DOM.toast.style.background = settings.backgroundColor;
  339. if (settings.balloon) {
  340. $DOM.toast.style.borderColor = settings.backgroundColor;
  341. }
  342. }
  343. })();
  344. // Cover image
  345. (function () {
  346. if (settings.image) {
  347. $DOM.cover.classList.add(PLUGIN_NAME + '-cover');
  348. $DOM.cover.style.width = settings.imageWidth + 'px';
  349. if (isBase64(settings.image.replace(/ /g, ''))) {
  350. $DOM.cover.style.backgroundImage = 'url(data:image/png;base64,' + settings.image.replace(/ /g, '') + ')';
  351. } else {
  352. $DOM.cover.style.backgroundImage = 'url(' + settings.image + ')';
  353. }
  354. if (settings.rtl) {
  355. $DOM.toastBody.style.marginRight = (settings.imageWidth) + 'px';
  356. } else {
  357. $DOM.toastBody.style.marginLeft = (settings.imageWidth) + 'px';
  358. }
  359. $DOM.toast.appendChild($DOM.cover);
  360. }
  361. })();
  362. // Button close
  363. (function () {
  364. if (settings.close) {
  365. $DOM.buttonClose = document.createElement('button');
  366. // $DOM.buttonClose.type = 'button';
  367. $DOM.buttonClose.setAttribute('type', 'button');
  368. $DOM.buttonClose.classList.add(PLUGIN_NAME + '-close');
  369. $DOM.buttonClose.addEventListener('click', function (e) {
  370. var button = e.target;
  371. that.hide(settings, $DOM.toast, 'button');
  372. });
  373. $DOM.toast.appendChild($DOM.buttonClose);
  374. } else {
  375. if (settings.rtl) {
  376. $DOM.toast.style.paddingLeft = '18px';
  377. } else {
  378. $DOM.toast.style.paddingRight = '18px';
  379. }
  380. }
  381. })();
  382. // Progress Bar & Timeout
  383. (function () {
  384. if (settings.progressBar) {
  385. $DOM.progressBar = document.createElement('div');
  386. $DOM.progressBarDiv = document.createElement('div');
  387. $DOM.progressBar.classList.add(PLUGIN_NAME + '-progressbar');
  388. $DOM.progressBarDiv.style.background = settings.progressBarColor;
  389. $DOM.progressBar.appendChild($DOM.progressBarDiv);
  390. $DOM.toast.appendChild($DOM.progressBar);
  391. }
  392. if (settings.timeout) {
  393. if (settings.pauseOnHover && !settings.resetOnHover) {
  394. $DOM.toast.addEventListener('mouseenter', function (e) {
  395. that.progress(settings, $DOM.toast).pause();
  396. });
  397. $DOM.toast.addEventListener('mouseleave', function (e) {
  398. that.progress(settings, $DOM.toast).resume();
  399. });
  400. }
  401. if (settings.resetOnHover) {
  402. $DOM.toast.addEventListener('mouseenter', function (e) {
  403. that.progress(settings, $DOM.toast).reset();
  404. });
  405. $DOM.toast.addEventListener('mouseleave', function (e) {
  406. that.progress(settings, $DOM.toast).start();
  407. });
  408. }
  409. }
  410. })();
  411. // Icon
  412. (function () {
  413. if (settings.iconUrl) {
  414. $DOM.icon.setAttribute('class', PLUGIN_NAME + '-icon');
  415. $DOM.icon.setAttribute('src', settings.iconUrl);
  416. } else if (settings.icon) {
  417. $DOM.icon.setAttribute('class', PLUGIN_NAME + '-icon ' + settings.icon);
  418. if (settings.iconText) {
  419. $DOM.icon.appendChild(document.createTextNode(settings.iconText));
  420. }
  421. if (settings.iconColor) {
  422. $DOM.icon.style.color = settings.iconColor;
  423. }
  424. }
  425. if (settings.icon || settings.iconUrl) {
  426. if (settings.rtl) {
  427. $DOM.toastBody.style.paddingRight = '33px';
  428. } else {
  429. $DOM.toastBody.style.paddingLeft = '33px';
  430. }
  431. $DOM.toastBody.appendChild($DOM.icon);
  432. }
  433. })();
  434. // Title & Message
  435. (function () {
  436. if (settings.title.length > 0) {
  437. $DOM.strong = document.createElement('strong');
  438. $DOM.strong.classList.add(PLUGIN_NAME + '-title');
  439. $DOM.strong.appendChild(createFragElem(settings.title));
  440. $DOM.toastTexts.appendChild($DOM.strong);
  441. if (settings.titleColor) {
  442. $DOM.strong.style.color = settings.titleColor;
  443. }
  444. if (settings.titleSize) {
  445. if (!isNaN(settings.titleSize)) {
  446. $DOM.strong.style.fontSize = settings.titleSize + 'px';
  447. } else {
  448. $DOM.strong.style.fontSize = settings.titleSize;
  449. }
  450. }
  451. if (settings.titleLineHeight) {
  452. if (!isNaN(settings.titleSize)) {
  453. $DOM.strong.style.lineHeight = settings.titleLineHeight + 'px';
  454. } else {
  455. $DOM.strong.style.lineHeight = settings.titleLineHeight;
  456. }
  457. }
  458. }
  459. if (settings.message.length > 0) {
  460. $DOM.p = document.createElement('p');
  461. $DOM.p.classList.add(PLUGIN_NAME + '-message');
  462. $DOM.p.appendChild(createFragElem(settings.message));
  463. $DOM.toastTexts.appendChild($DOM.p);
  464. if (settings.messageColor) {
  465. $DOM.p.style.color = settings.messageColor;
  466. }
  467. if (settings.messageSize) {
  468. if (!isNaN(settings.titleSize)) {
  469. $DOM.p.style.fontSize = settings.messageSize + 'px';
  470. } else {
  471. $DOM.p.style.fontSize = settings.messageSize;
  472. }
  473. }
  474. if (settings.messageLineHeight) {
  475. if (!isNaN(settings.titleSize)) {
  476. $DOM.p.style.lineHeight = settings.messageLineHeight + 'px';
  477. } else {
  478. $DOM.p.style.lineHeight = settings.messageLineHeight;
  479. }
  480. }
  481. }
  482. if (settings.title.length > 0 && settings.message.length > 0) {
  483. if (settings.rtl) {
  484. $DOM.strong.style.marginLeft = '10px';
  485. } else if (settings.layout != 2 && !settings.rtl) {
  486. $DOM.strong.style.marginRight = '10px';
  487. $DOM.strong.style.marginBottom = '0px';
  488. }
  489. }
  490. })();
  491. $DOM.toastBody.appendChild($DOM.toastTexts);
  492. // Inputs
  493. var $inputs;
  494. (function () {
  495. if (settings.inputs.length > 0) {
  496. $DOM.inputs.classList.add(PLUGIN_NAME + '-inputs');
  497. forEach(settings.inputs, function (value, index) {
  498. $DOM.inputs.appendChild(createFragElem(value[0]));
  499. $inputs = $DOM.inputs.childNodes;
  500. $inputs[index].classList.add(PLUGIN_NAME + '-inputs-child');
  501. if (value[3]) {
  502. setTimeout(function () {
  503. $inputs[index].focus();
  504. }, 300);
  505. }
  506. $inputs[index].addEventListener(value[1], function (e) {
  507. var ts = value[2];
  508. return ts(that, $DOM.toast, this, e);
  509. });
  510. });
  511. $DOM.toastBody.appendChild($DOM.inputs);
  512. }
  513. })();
  514. // Buttons
  515. (function () {
  516. if (settings.buttons.length > 0) {
  517. $DOM.buttons.classList.add(PLUGIN_NAME + '-buttons');
  518. forEach(settings.buttons, function (value, index) {
  519. $DOM.buttons.appendChild(createFragElem(value[0]));
  520. var $btns = $DOM.buttons.childNodes;
  521. $btns[index].classList.add(PLUGIN_NAME + '-buttons-child');
  522. if (value[2]) {
  523. setTimeout(function () {
  524. $btns[index].focus();
  525. }, 300);
  526. }
  527. $btns[index].addEventListener('click', function (e) {
  528. e.preventDefault();
  529. var ts = value[1];
  530. return ts(that, $DOM.toast, this, e, $inputs);
  531. });
  532. });
  533. }
  534. $DOM.toastTexts.appendChild($DOM.buttons);
  535. })();
  536. if (settings.message.length > 0 && (settings.inputs.length > 0 || settings.buttons.length > 0)) {
  537. $DOM.p.style.marginBottom = '0';
  538. }
  539. if (settings.inputs.length > 0 || settings.buttons.length > 0) {
  540. if (settings.rtl) {
  541. $DOM.toastTexts.style.marginLeft = '10px';
  542. } else {
  543. $DOM.toastTexts.style.marginRight = '10px';
  544. }
  545. if (settings.inputs.length > 0 && settings.buttons.length > 0) {
  546. if (settings.rtl) {
  547. $DOM.inputs.style.marginLeft = '8px';
  548. } else {
  549. $DOM.inputs.style.marginRight = '8px';
  550. }
  551. }
  552. }
  553. // Wrap
  554. (function () {
  555. $DOM.toastCapsule.style.visibility = 'hidden';
  556. setTimeout(function () {
  557. var H = $DOM.toast.offsetHeight;
  558. var style = $DOM.toast.currentStyle || window.getComputedStyle($DOM.toast);
  559. var marginTop = style.marginTop;
  560. marginTop = marginTop.split('px');
  561. marginTop = parseInt(marginTop[0]);
  562. var marginBottom = style.marginBottom;
  563. marginBottom = marginBottom.split('px');
  564. marginBottom = parseInt(marginBottom[0]);
  565. $DOM.toastCapsule.style.visibility = '';
  566. $DOM.toastCapsule.style.height = (H + marginBottom + marginTop) + 'px';
  567. setTimeout(function () {
  568. $DOM.toastCapsule.style.height = 'auto';
  569. if (settings.target) {
  570. $DOM.toastCapsule.style.overflow = 'visible';
  571. }
  572. }, 500);
  573. if (settings.timeout) {
  574. that.progress(settings, $DOM.toast).start();
  575. }
  576. }, 100);
  577. })();
  578. // Target
  579. (function () {
  580. var position = settings.position;
  581. if (settings.target) {
  582. $DOM.wrapper = document.querySelector(settings.target);
  583. $DOM.wrapper.classList.add(PLUGIN_NAME + '-target');
  584. if (settings.targetFirst) {
  585. $DOM.wrapper.insertBefore($DOM.toastCapsule, $DOM.wrapper.firstChild);
  586. } else {
  587. $DOM.wrapper.appendChild($DOM.toastCapsule);
  588. }
  589. } else {
  590. if (POSITIONS.indexOf(settings.position) == -1) {
  591. console.warn('[' + PLUGIN_NAME + '] Incorrect position.\nIt can be › ' + POSITIONS);
  592. return;
  593. }
  594. if (ISMOBILE || window.innerWidth <= MOBILEWIDTH) {
  595. if (settings.position == 'bottomLeft' || settings.position == 'bottomRight' || settings.position == 'bottomCenter') {
  596. position = PLUGIN_NAME + '-wrapper-bottomCenter';
  597. } else if (settings.position == 'topLeft' || settings.position == 'topRight' || settings.position == 'topCenter') {
  598. position = PLUGIN_NAME + '-wrapper-topCenter';
  599. } else {
  600. position = PLUGIN_NAME + '-wrapper-center';
  601. }
  602. } else {
  603. position = PLUGIN_NAME + '-wrapper-' + position;
  604. }
  605. $DOM.wrapper = document.querySelector('.' + PLUGIN_NAME + '-wrapper.' + position);
  606. if (!$DOM.wrapper) {
  607. $DOM.wrapper = document.createElement('div');
  608. $DOM.wrapper.classList.add(PLUGIN_NAME + '-wrapper');
  609. $DOM.wrapper.classList.add(position);
  610. document.body.appendChild($DOM.wrapper);
  611. }
  612. var targetFirst = settings.targetFirst;
  613. if ((targetFirst == undefined || targetFirst == null) && (settings.position == 'topLeft' || settings.position == 'topCenter' || settings.position == 'topRight')) {
  614. targetFirst = true;
  615. }
  616. if (targetFirst) {
  617. $DOM.wrapper.insertBefore($DOM.toastCapsule, $DOM.wrapper.firstChild);
  618. } else {
  619. $DOM.wrapper.appendChild($DOM.toastCapsule);
  620. }
  621. }
  622. if (!isNaN(settings.zindex)) {
  623. $DOM.wrapper.style.zIndex = settings.zindex;
  624. } else {
  625. console.warn('[' + PLUGIN_NAME + '] Invalid zIndex.');
  626. }
  627. })();
  628. // Overlay
  629. (function () {
  630. if (settings.overlay) {
  631. if (document.querySelector('.' + PLUGIN_NAME + '-overlay.fadeIn') !== null) {
  632. $DOM.overlay = document.querySelector('.' + PLUGIN_NAME + '-overlay');
  633. $DOM.overlay.setAttribute('data-iziToast-ref', $DOM.overlay.getAttribute('data-iziToast-ref') + ',' + settings.ref);
  634. if (!isNaN(settings.zindex) && settings.zindex !== null) {
  635. $DOM.overlay.style.zIndex = settings.zindex - 1;
  636. }
  637. } else {
  638. $DOM.overlay.classList.add(PLUGIN_NAME + '-overlay');
  639. $DOM.overlay.classList.add('fadeIn');
  640. $DOM.overlay.style.background = settings.overlayColor;
  641. $DOM.overlay.setAttribute('data-iziToast-ref', settings.ref);
  642. if (!isNaN(settings.zindex) && settings.zindex !== null) {
  643. $DOM.overlay.style.zIndex = settings.zindex - 1;
  644. }
  645. document.querySelector('body').appendChild($DOM.overlay);
  646. }
  647. if (settings.overlayClose) {
  648. $DOM.overlay.removeEventListener('click', {});
  649. $DOM.overlay.addEventListener('click', function (e) {
  650. that.hide(settings, $DOM.toast, 'overlay');
  651. });
  652. } else {
  653. $DOM.overlay.removeEventListener('click', {});
  654. }
  655. }
  656. })();
  657. // Inside animations
  658. (function () {
  659. if (settings.animateInside) {
  660. $DOM.toast.classList.add(PLUGIN_NAME + '-animateInside');
  661. var animationTimes = [200, 100, 300];
  662. if (settings.transitionIn == 'bounceInLeft' || settings.transitionIn == 'bounceInRight') {
  663. animationTimes = [400, 200, 400];
  664. }
  665. if (settings.title.length > 0) {
  666. setTimeout(function () {
  667. $DOM.strong.classList.add('slideIn');
  668. }, animationTimes[0]);
  669. }
  670. if (settings.message.length > 0) {
  671. setTimeout(function () {
  672. $DOM.p.classList.add('slideIn');
  673. }, animationTimes[1]);
  674. }
  675. if (settings.icon || settings.iconUrl) {
  676. setTimeout(function () {
  677. $DOM.icon.classList.add('revealIn');
  678. }, animationTimes[2]);
  679. }
  680. var counter = 150;
  681. if (settings.buttons.length > 0 && $DOM.buttons) {
  682. setTimeout(function () {
  683. forEach($DOM.buttons.childNodes, function (element, index) {
  684. setTimeout(function () {
  685. element.classList.add('revealIn');
  686. }, counter);
  687. counter = counter + 150;
  688. });
  689. }, settings.inputs.length > 0 ? 150 : 0);
  690. }
  691. if (settings.inputs.length > 0 && $DOM.inputs) {
  692. counter = 150;
  693. forEach($DOM.inputs.childNodes, function (element, index) {
  694. setTimeout(function () {
  695. element.classList.add('revealIn');
  696. }, counter);
  697. counter = counter + 150;
  698. });
  699. }
  700. }
  701. })();
  702. settings.onOpening.apply(null, [settings, $DOM.toast]);
  703. try {
  704. var event = new CustomEvent(PLUGIN_NAME + '-opening', {detail: settings, bubbles: true, cancelable: true});
  705. document.dispatchEvent(event);
  706. } catch (ex) {
  707. console.warn(ex);
  708. }
  709. setTimeout(function () {
  710. $DOM.toast.classList.remove(PLUGIN_NAME + '-opening');
  711. $DOM.toast.classList.add(PLUGIN_NAME + '-opened');
  712. try {
  713. var event = new CustomEvent(PLUGIN_NAME + '-opened', {
  714. detail: settings,
  715. bubbles: true,
  716. cancelable: true
  717. });
  718. document.dispatchEvent(event);
  719. } catch (ex) {
  720. console.warn(ex);
  721. }
  722. settings.onOpened.apply(null, [settings, $DOM.toast]);
  723. }, 1000);
  724. if (settings.drag) {
  725. if (ACCEPTSTOUCH) {
  726. $DOM.toast.addEventListener('touchstart', function (e) {
  727. drag.startMoving(this, that, settings, e);
  728. }, false);
  729. $DOM.toast.addEventListener('touchend', function (e) {
  730. drag.stopMoving(this, e);
  731. }, false);
  732. } else {
  733. $DOM.toast.addEventListener('mousedown', function (e) {
  734. e.preventDefault();
  735. drag.startMoving(this, that, settings, e);
  736. }, false);
  737. $DOM.toast.addEventListener('mouseup', function (e) {
  738. e.preventDefault();
  739. drag.stopMoving(this, e);
  740. }, false);
  741. }
  742. }
  743. if (settings.closeOnEscape) {
  744. document.addEventListener('keyup', function (evt) {
  745. evt = evt || window.event;
  746. if (evt.keyCode == 27) {
  747. that.hide(settings, $DOM.toast, 'esc');
  748. }
  749. });
  750. }
  751. if (settings.closeOnClick) {
  752. $DOM.toast.addEventListener('click', function (evt) {
  753. that.hide(settings, $DOM.toast, 'toast');
  754. });
  755. }
  756. // 播放声音
  757. if (settings.audio) {
  758. that.playSound(settings.audio);
  759. }
  760. that.toast = $DOM.toast;
  761. };
  762. // 控制进度条
  763. $iziToast.progress = function (options, $toast, callback) {
  764. var that = this,
  765. ref = $toast.getAttribute('data-iziToast-ref'),
  766. settings = extend(this.children[ref], options || {}),
  767. $elem = $toast.querySelector('.' + PLUGIN_NAME + '-progressbar div');
  768. return {
  769. start: function () {
  770. if (typeof settings.time.REMAINING == 'undefined') {
  771. $toast.classList.remove(PLUGIN_NAME + '-reseted');
  772. if ($elem !== null) {
  773. $elem.style.transition = 'width ' + settings.timeout + 'ms ' + settings.progressBarEasing;
  774. $elem.style.width = '0%';
  775. }
  776. settings.time.START = new Date().getTime();
  777. settings.time.END = settings.time.START + settings.timeout;
  778. settings.time.TIMER = setTimeout(function () {
  779. clearTimeout(settings.time.TIMER);
  780. if (!$toast.classList.contains(PLUGIN_NAME + '-closing')) {
  781. that.hide(settings, $toast, 'timeout');
  782. if (typeof callback === 'function') {
  783. callback.apply(that);
  784. }
  785. }
  786. }, settings.timeout);
  787. that.setSetting(ref, 'time', settings.time);
  788. }
  789. },
  790. pause: function () {
  791. if (typeof settings.time.START !== 'undefined' && !$toast.classList.contains(PLUGIN_NAME + '-paused') && !$toast.classList.contains(PLUGIN_NAME + '-reseted')) {
  792. $toast.classList.add(PLUGIN_NAME + '-paused');
  793. settings.time.REMAINING = settings.time.END - new Date().getTime();
  794. clearTimeout(settings.time.TIMER);
  795. that.setSetting(ref, 'time', settings.time);
  796. if ($elem !== null) {
  797. var computedStyle = window.getComputedStyle($elem),
  798. propertyWidth = computedStyle.getPropertyValue('width');
  799. $elem.style.transition = 'none';
  800. $elem.style.width = propertyWidth;
  801. }
  802. if (typeof callback === 'function') {
  803. setTimeout(function () {
  804. callback.apply(that);
  805. }, 10);
  806. }
  807. }
  808. },
  809. resume: function () {
  810. if (typeof settings.time.REMAINING !== 'undefined') {
  811. $toast.classList.remove(PLUGIN_NAME + '-paused');
  812. if ($elem !== null) {
  813. $elem.style.transition = 'width ' + settings.time.REMAINING + 'ms ' + settings.progressBarEasing;
  814. $elem.style.width = '0%';
  815. }
  816. settings.time.END = new Date().getTime() + settings.time.REMAINING;
  817. settings.time.TIMER = setTimeout(function () {
  818. clearTimeout(settings.time.TIMER);
  819. if (!$toast.classList.contains(PLUGIN_NAME + '-closing')) {
  820. that.hide(settings, $toast, 'timeout');
  821. if (typeof callback === 'function') {
  822. callback.apply(that);
  823. }
  824. }
  825. }, settings.time.REMAINING);
  826. that.setSetting(ref, 'time', settings.time);
  827. } else {
  828. this.start();
  829. }
  830. },
  831. reset: function () {
  832. clearTimeout(settings.time.TIMER);
  833. delete settings.time.REMAINING;
  834. that.setSetting(ref, 'time', settings.time);
  835. $toast.classList.add(PLUGIN_NAME + '-reseted');
  836. $toast.classList.remove(PLUGIN_NAME + '-paused');
  837. if ($elem !== null) {
  838. $elem.style.transition = 'none';
  839. $elem.style.width = '100%';
  840. }
  841. if (typeof callback === 'function') {
  842. setTimeout(function () {
  843. callback.apply(that);
  844. }, 10);
  845. }
  846. }
  847. };
  848. };
  849. // 判断是否是ie9以下版本
  850. var isIE9_ = function () {
  851. var userAgent = navigator.userAgent;
  852. if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
  853. var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
  854. reIE.test(userAgent);
  855. var fIEVersion = parseFloat(RegExp["$1"]);
  856. if (fIEVersion != 10) {
  857. return true;
  858. }
  859. }
  860. return false;
  861. };
  862. // 给Element添加remove方法
  863. if (!('remove' in Element.prototype)) {
  864. Element.prototype.remove = function () {
  865. if (this.parentNode) {
  866. this.parentNode.removeChild(this);
  867. }
  868. };
  869. }
  870. // 自定义事件
  871. if (typeof window.CustomEvent !== 'function') {
  872. var CustomEventPolyfill = function (event, params) {
  873. params = params || {bubbles: false, cancelable: false, detail: undefined};
  874. var evt = document.createEvent('CustomEvent');
  875. evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
  876. return evt;
  877. };
  878. CustomEventPolyfill.prototype = window.Event.prototype;
  879. window.CustomEvent = CustomEventPolyfill;
  880. }
  881. // 遍历数据
  882. var forEach = function (collection, callback, scope) {
  883. if (Object.prototype.toString.call(collection) === '[object Object]') {
  884. for (var prop in collection) {
  885. if (Object.prototype.hasOwnProperty.call(collection, prop)) {
  886. callback.call(scope, collection[prop], prop, collection);
  887. }
  888. }
  889. } else {
  890. if (collection) {
  891. for (var i = 0, len = collection.length; i < len; i++) {
  892. callback.call(scope, collection[i], i, collection);
  893. }
  894. }
  895. }
  896. };
  897. // 合并自定义参数和默认参数
  898. var extend = function (defaults, options) {
  899. var extended = {};
  900. forEach(defaults, function (value, prop) {
  901. extended[prop] = defaults[prop];
  902. });
  903. forEach(options, function (value, prop) {
  904. extended[prop] = options[prop];
  905. });
  906. return extended;
  907. };
  908. // 创建新的文档片段
  909. var createFragElem = function (htmlStr) {
  910. var frag = document.createDocumentFragment(),
  911. temp = document.createElement('div');
  912. temp.innerHTML = htmlStr;
  913. while (temp.firstChild) {
  914. frag.appendChild(temp.firstChild);
  915. }
  916. return frag;
  917. };
  918. // 生成ID
  919. var generateId = function (params) {
  920. var newId = btoa(encodeURIComponent(params));
  921. return newId.replace(/=/g, "");
  922. };
  923. // 判断是否是颜色字符串
  924. var isColor = function (color) {
  925. if (color.substring(0, 1) == '#' || color.substring(0, 3) == 'rgb' || color.substring(0, 3) == 'hsl') {
  926. return true;
  927. } else {
  928. return false;
  929. }
  930. };
  931. // 判断是否是base64字符串
  932. var isBase64 = function (str) {
  933. try {
  934. return btoa(atob(str)) == str;
  935. } catch (err) {
  936. return false;
  937. }
  938. };
  939. // 拖拽方法
  940. var drag = function () {
  941. return {
  942. move: function (toast, instance, settings, xpos) {
  943. var opacity,
  944. opacityRange = 0.3,
  945. distance = 180;
  946. if (xpos !== 0) {
  947. toast.classList.add(PLUGIN_NAME + '-dragged');
  948. toast.style.transform = 'translateX(' + xpos + 'px)';
  949. if (xpos > 0) {
  950. opacity = (distance - xpos) / distance;
  951. if (opacity < opacityRange) {
  952. instance.hide(extend(settings, {
  953. transitionOut: 'fadeOutRight',
  954. transitionOutMobile: 'fadeOutRight'
  955. }), toast, 'drag');
  956. }
  957. } else {
  958. opacity = (distance + xpos) / distance;
  959. if (opacity < opacityRange) {
  960. instance.hide(extend(settings, {
  961. transitionOut: 'fadeOutLeft',
  962. transitionOutMobile: 'fadeOutLeft'
  963. }), toast, 'drag');
  964. }
  965. }
  966. toast.style.opacity = opacity;
  967. if (opacity < opacityRange) {
  968. if (ISCHROME || ISFIREFOX)
  969. toast.style.left = xpos + 'px';
  970. toast.parentNode.style.opacity = opacityRange;
  971. this.stopMoving(toast, null);
  972. }
  973. }
  974. },
  975. startMoving: function (toast, instance, settings, e) {
  976. e = e || window.event;
  977. var posX = ((ACCEPTSTOUCH) ? e.touches[0].clientX : e.clientX),
  978. toastLeft = toast.style.transform.replace('px)', '');
  979. toastLeft = toastLeft.replace('translateX(', '');
  980. var offsetX = posX - toastLeft;
  981. if (settings.transitionIn) {
  982. toast.classList.remove(settings.transitionIn);
  983. }
  984. if (settings.transitionInMobile) {
  985. toast.classList.remove(settings.transitionInMobile);
  986. }
  987. toast.style.transition = '';
  988. if (ACCEPTSTOUCH) {
  989. document.ontouchmove = function (e) {
  990. e.preventDefault();
  991. e = e || window.event;
  992. var posX = e.touches[0].clientX,
  993. finalX = posX - offsetX;
  994. drag.move(toast, instance, settings, finalX);
  995. };
  996. } else {
  997. document.onmousemove = function (e) {
  998. e.preventDefault();
  999. e = e || window.event;
  1000. var posX = e.clientX,
  1001. finalX = posX - offsetX;
  1002. drag.move(toast, instance, settings, finalX);
  1003. };
  1004. }
  1005. },
  1006. stopMoving: function (toast, e) {
  1007. if (ACCEPTSTOUCH) {
  1008. document.ontouchmove = function () {
  1009. };
  1010. } else {
  1011. document.onmousemove = function () {
  1012. };
  1013. }
  1014. toast.style.opacity = '';
  1015. toast.style.transform = '';
  1016. if (toast.classList.contains(PLUGIN_NAME + '-dragged')) {
  1017. toast.classList.remove(PLUGIN_NAME + '-dragged');
  1018. toast.style.transition = 'transform 0.4s ease, opacity 0.4s ease';
  1019. setTimeout(function () {
  1020. toast.style.transition = '';
  1021. }, 400);
  1022. }
  1023. }
  1024. };
  1025. }();
  1026. // 兼容IE
  1027. var Base64 = {
  1028. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function (e) {
  1029. var t = "";
  1030. var n, r, i, s, o, u, a;
  1031. var f = 0;
  1032. e = Base64._utf8_encode(e);
  1033. while (f < e.length) {
  1034. n = e.charCodeAt(f++);
  1035. r = e.charCodeAt(f++);
  1036. i = e.charCodeAt(f++);
  1037. s = n >> 2;
  1038. o = (n & 3) << 4 | r >> 4;
  1039. u = (r & 15) << 2 | i >> 6;
  1040. a = i & 63;
  1041. if (isNaN(r)) {
  1042. u = a = 64
  1043. } else if (isNaN(i)) {
  1044. a = 64
  1045. }
  1046. t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
  1047. }
  1048. return t
  1049. }, decode: function (e) {
  1050. var t = "";
  1051. var n, r, i;
  1052. var s, o, u, a;
  1053. var f = 0;
  1054. e = e.replace(/[^A-Za-z0-9+/=]/g, "");
  1055. while (f < e.length) {
  1056. s = this._keyStr.indexOf(e.charAt(f++));
  1057. o = this._keyStr.indexOf(e.charAt(f++));
  1058. u = this._keyStr.indexOf(e.charAt(f++));
  1059. a = this._keyStr.indexOf(e.charAt(f++));
  1060. n = s << 2 | o >> 4;
  1061. r = (o & 15) << 4 | u >> 2;
  1062. i = (u & 3) << 6 | a;
  1063. t = t + String.fromCharCode(n);
  1064. if (u != 64) {
  1065. t = t + String.fromCharCode(r)
  1066. }
  1067. if (a != 64) {
  1068. t = t + String.fromCharCode(i)
  1069. }
  1070. }
  1071. t = Base64._utf8_decode(t);
  1072. return t
  1073. }, _utf8_encode: function (e) {
  1074. e = e.replace(/rn/g, "n");
  1075. var t = "";
  1076. for (var n = 0; n < e.length; n++) {
  1077. var r = e.charCodeAt(n);
  1078. if (r < 128) {
  1079. t += String.fromCharCode(r)
  1080. } else if (r > 127 && r < 2048) {
  1081. t += String.fromCharCode(r >> 6 | 192);
  1082. t += String.fromCharCode(r & 63 | 128)
  1083. } else {
  1084. t += String.fromCharCode(r >> 12 | 224);
  1085. t += String.fromCharCode(r >> 6 & 63 | 128);
  1086. t += String.fromCharCode(r & 63 | 128)
  1087. }
  1088. }
  1089. return t
  1090. }, _utf8_decode: function (e) {
  1091. var t = "";
  1092. var n = 0;
  1093. var r = c1 = c2 = 0;
  1094. while (n < e.length) {
  1095. r = e.charCodeAt(n);
  1096. if (r < 128) {
  1097. t += String.fromCharCode(r);
  1098. n++
  1099. } else if (r > 191 && r < 224) {
  1100. c2 = e.charCodeAt(n + 1);
  1101. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  1102. n += 2
  1103. } else {
  1104. c2 = e.charCodeAt(n + 1);
  1105. c3 = e.charCodeAt(n + 2);
  1106. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  1107. n += 3
  1108. }
  1109. }
  1110. return t
  1111. }
  1112. };
  1113. if (isIE9_()) {
  1114. // 兼容btoa和atob方法
  1115. window.btoa = function (str) {
  1116. return Base64.encode(str);
  1117. };
  1118. window.atob = function (str) {
  1119. return Base64.decode(str);
  1120. };
  1121. // 兼容classList属性
  1122. if (!("classList" in document.documentElement)) {
  1123. Object.defineProperty(window.Element.prototype, 'classList', {
  1124. get: function () {
  1125. var self = this;
  1126. function update(fn) {
  1127. return function () {
  1128. var className = self.className.replace(/^\s+|\s+$/g, ''),
  1129. valArr = arguments;
  1130. return fn(className, valArr)
  1131. }
  1132. }
  1133. function add_rmv(className, valArr, tag) {
  1134. for (var i in valArr) {
  1135. if (typeof valArr[i] !== 'string' || !!~valArr[i].search(/\s+/g)) throw TypeError('the type of value is error')
  1136. var temp = valArr[i]
  1137. var flag = !!~className.search(new RegExp('(\\s+)?' + temp + '(\\s+)?'))
  1138. if (tag === 1) {
  1139. !flag ? className += ' ' + temp : ''
  1140. } else if (tag === 2) {
  1141. flag ? className = className.replace(new RegExp('(\\s+)?' + temp), '') : ''
  1142. }
  1143. }
  1144. self.className = className;
  1145. return tag;
  1146. }
  1147. return {
  1148. add: update(function (className, valArr) {
  1149. add_rmv(className, valArr, 1)
  1150. }),
  1151. remove: update(function (className, valArr) {
  1152. add_rmv(className, valArr, 2)
  1153. }),
  1154. toggle: function (value) {
  1155. if (typeof value !== 'string' || arguments.length === 0) throw TypeError("Failed to execute 'toggle' on 'DOMTokenList': 1 argument(string) required, but only 0 present.")
  1156. if (arguments.length === 1) {
  1157. this.contains(value) ? this.remove(value) : this.add(value)
  1158. return
  1159. }
  1160. !arguments[1] ? this.remove(value) : this.add(value)
  1161. },
  1162. contains: update(function (className, valArr) {
  1163. if (valArr.length === 0) throw TypeError("Failed to execute 'contains' on 'DOMTokenList': 1 argument required, but only 0 present.")
  1164. if (typeof valArr[0] !== 'string' || !!~valArr[0].search(/\s+/g)) return false
  1165. return !!~className.search(new RegExp(valArr[0]))
  1166. }),
  1167. item: function (index) {
  1168. typeof index === 'string' ? index = parseInt(index) : ''
  1169. if (arguments.length === 0 || typeof index !== 'number') throw TypeError("Failed to execute 'toggle' on 'DOMTokenList': 1 argument required, but only 0 present.")
  1170. var claArr = self.className.replace(/^\s+|\s+$/, '').split(/\s+/)
  1171. var len = claArr.length
  1172. if (index < 0 || index >= len) return null
  1173. return claArr[index]
  1174. }
  1175. }
  1176. }
  1177. });
  1178. }
  1179. }
  1180. // 播放声音
  1181. $iziToast.playSound = function (src) {
  1182. if (!(src.indexOf('http') == 0)) {
  1183. src = layui.cache.base + 'notice/' + src + '.wav';
  1184. }
  1185. if (!!window.ActiveXObject || "ActiveXObject" in window) { // IE
  1186. var embed = document.noticePlay;
  1187. if (embed) {
  1188. embed.remove();
  1189. }
  1190. embed = document.createElement('embed');
  1191. embed.setAttribute('name', 'noticePlay');
  1192. embed.setAttribute('src', src);
  1193. embed.setAttribute('autostart', true);
  1194. embed.setAttribute('loop', false);
  1195. embed.setAttribute('hidden', true);
  1196. document.body.appendChild(embed);
  1197. embed = document.noticePlay;
  1198. embed.volume = 100;
  1199. } else { // 非IE
  1200. var audio = document.createElement('audio');
  1201. audio.setAttribute('hidden', true);
  1202. audio.setAttribute('src', src);
  1203. document.body.appendChild(audio);
  1204. audio.addEventListener('ended', function () {
  1205. audio.parentNode.removeChild(audio);
  1206. }, false);
  1207. audio.play();
  1208. }
  1209. };
  1210. // 不同主题的通知
  1211. forEach(THEMES, function (theme, name) {
  1212. $iziToast[name] = function (options) {
  1213. var settings = extend(CONFIG, options || {});
  1214. settings = extend(theme, settings || {});
  1215. this.show(settings);
  1216. };
  1217. });
  1218. layui.link(layui.cache.base + 'notice/notice.css'); // 加载css
  1219. exports('notice', $iziToast);
  1220. });