|
| 1 | +/* eslint-disable */ |
| 2 | +/* tslint:disable */ |
| 3 | +// @ts-ignore |
| 4 | + |
| 5 | +let callbacks: any = []; |
| 6 | + |
| 7 | +function loadedTinymce() { |
| 8 | + // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2144 |
| 9 | + // check is successfully downloaded script |
| 10 | + return window.tinymce; |
| 11 | +} |
| 12 | + |
| 13 | +const dynamicLoadScript = (src, callback) => { |
| 14 | + const existingScript = document.getElementById(src); |
| 15 | + const cb = callback || function () { |
| 16 | + }; |
| 17 | + |
| 18 | + if (!existingScript) { |
| 19 | + const script = document.createElement('script'); |
| 20 | + script.src = src; // src url for the third-party library being loaded. |
| 21 | + script.id = src; |
| 22 | + document.body.appendChild(script); |
| 23 | + callbacks.push(cb); |
| 24 | + const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd; |
| 25 | + onEnd(script); |
| 26 | + } |
| 27 | + |
| 28 | + if (existingScript && cb) { |
| 29 | + if (loadedTinymce()) { |
| 30 | + cb(null, existingScript); |
| 31 | + } else { |
| 32 | + callbacks.push(cb); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + function stdOnEnd(script) { |
| 37 | + // tslint:disable-next-line:space-before-function-paren |
| 38 | + script.onload = function () { |
| 39 | + // this.onload = null here is necessary |
| 40 | + // because even IE9 works not like others |
| 41 | + this.onerror = this.onload = null; |
| 42 | + for (const cb of callbacks) { |
| 43 | + cb(null, script); |
| 44 | + } |
| 45 | + callbacks = null; |
| 46 | + }; |
| 47 | + // tslint:disable-next-line:space-before-function-paren |
| 48 | + script.onerror = function () { |
| 49 | + this.onerror = this.onload = null; |
| 50 | + cb(new Error('Failed to load ' + src), script); |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + function ieOnEnd(script) { |
| 55 | + // tslint:disable-next-line:space-before-function-paren |
| 56 | + script.onreadystatechange = function () { |
| 57 | + if (this.readyState !== 'complete' && this.readyState !== 'loaded') return; |
| 58 | + this.onreadystatechange = null; |
| 59 | + for (const cb of callbacks) { |
| 60 | + cb(null, script); // there is no way to catch loading errors in IE8 |
| 61 | + } |
| 62 | + callbacks = null; |
| 63 | + }; |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +export default dynamicLoadScript; |
0 commit comments