bootstrap.js 26.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
window.UEDITOR_HOME_URL = Config.__CDN__ + "/assets/addons/ueditor/";
require.config({
    paths: {
        'ueditor.config': '../addons/ueditor/ueditor.config',
        'ueditor': '../addons/ueditor/ueditor.all.min',
        'ueditor.zh': '../addons/ueditor/lang/zh-cn/zh-cn',
        'zeroclipboard': '../addons/ueditor/third-party/zeroclipboard/ZeroClipboard.min',
    },
    shim: {
        'ueditor': {
            deps: ['zeroclipboard', 'ueditor.config'],
            exports: 'UE',
            init: function (ZeroClipboard) {
                //导出到全局变量,供ueditor使用
                window.ZeroClipboard = ZeroClipboard;
            },
        },
        'ueditor.zh': ['ueditor']
    }
});
require(['form', 'upload', 'ueditor', 'ueditor.zh'], function (Form, Upload, UE, undefined) {
    UE.plugin.register('simpleupload', function () {
        var me = this,
            isLoaded = false,
            containerBtn;

        function initUploadBtn() {
            var w = containerBtn.offsetWidth || 20,
                h = containerBtn.offsetHeight || 20,
                btnIframe = document.createElement('iframe'),
                btnStyle = 'display:block;width:' + w + 'px;height:' + h + 'px;overflow:hidden;border:0;margin:0;padding:0;position:absolute;top:0;left:0;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;cursor:pointer;';

            UE.dom.domUtils.on(btnIframe, 'load', function () {

                var timestrap = (+new Date()).toString(36),
                    wrapper,
                    btnIframeDoc,
                    btnIframeBody;

                btnIframeDoc = (btnIframe.contentDocument || btnIframe.contentWindow.document);
                btnIframeBody = btnIframeDoc.body;
                wrapper = btnIframeDoc.createElement('div');

                wrapper.innerHTML = '<form id="edui_form_' + timestrap + '" target="edui_iframe_' + timestrap + '" method="POST" enctype="multipart/form-data" action="' + me.getOpt('serverUrl') + '" ' +
                    'style="' + btnStyle + '">' +
                    '<input id="edui_input_' + timestrap + '" type="file" accept="image/*" name="' + me.options.imageFieldName + '" ' +
                    'style="' + btnStyle + '">' +
                    '</form>' +
                    '<iframe id="edui_iframe_' + timestrap + '" name="edui_iframe_' + timestrap + '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>';

                wrapper.className = 'edui-' + me.options.theme;
                wrapper.id = me.ui.id + '_iframeupload';
                btnIframeBody.style.cssText = btnStyle;
                btnIframeBody.style.width = w + 'px';
                btnIframeBody.style.height = h + 'px';
                btnIframeBody.appendChild(wrapper);

                if (btnIframeBody.parentNode) {
                    btnIframeBody.parentNode.style.width = w + 'px';
                    btnIframeBody.parentNode.style.height = w + 'px';
                }

                var form = btnIframeDoc.getElementById('edui_form_' + timestrap);
                var input = btnIframeDoc.getElementById('edui_input_' + timestrap);
                var iframe = btnIframeDoc.getElementById('edui_iframe_' + timestrap);

                UE.dom.domUtils.on(input, 'change', function () {
                    if (!input.value) return;
                    var loadingId = 'loading_' + (+new Date()).toString(36);
                    var params = UE.utils.serializeParam(me.queryCommandValue('serverparam')) || '';

                    var imageActionUrl = me.getActionUrl(me.getOpt('imageActionName'));
                    var allowFiles = me.getOpt('imageAllowFiles');

                    me.focus();
                    me.execCommand('inserthtml', '<img class="loadingclass" id="' + loadingId + '" src="' + me.options.themePath + me.options.theme + '/images/spacer.gif" title="' + (me.getLang('simpleupload.loading') || '') + '" >');

                    function showErrorLoader(title) {
                        if (loadingId) {
                            var loader = me.document.getElementById(loadingId);
                            loader && UE.dom.domUtils.remove(loader);
                            me.fireEvent('showmessage', {
                                'id': loadingId,
                                'content': title,
                                'type': 'error',
                                'timeout': 4000
                            });
                        }
                    }

                    // 判断文件格式是否错误
                    var filename = input.value,
                        fileext = filename ? filename.substr(filename.lastIndexOf('.')) : '';
                    if (!fileext || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
                        showErrorLoader(me.getLang('simpleupload.exceedTypeError'));
                        return;
                    }
                    for (var i = 0; i < this.files.length; i++) {
                        Upload.api.send(this.files[i], function (data) {
                            var url = Fast.api.cdnurl(data.url);
                            loader = me.document.getElementById(loadingId);
                            loader.setAttribute('src', url);
                            loader.setAttribute('_src', url);
                            loader.setAttribute('title', '');
                            loader.setAttribute('alt', '');
                            loader.removeAttribute('id');
                            UE.dom.domUtils.removeClasses(loader, 'loadingclass');
                            form.reset();
                        });
                    }
                });

                var stateTimer;
                me.addListener('selectionchange', function () {
                    clearTimeout(stateTimer);
                    stateTimer = setTimeout(function () {
                        var state = me.queryCommandState('simpleupload');
                        if (state == -1) {
                            input.disabled = 'disabled';
                        } else {
                            input.disabled = false;
                        }
                    }, 400);
                });
                isLoaded = true;
            });

            btnIframe.style.cssText = btnStyle;
            containerBtn.appendChild(btnIframe);
        }

        return {
            bindEvents: {
                'ready': function () {
                    //设置loading的样式
                    UE.utils.cssRule('loading',
                        '.loadingclass{display:inline-block;cursor:default;background: url(\'' +
                        this.options.themePath +
                        this.options.theme + '/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n' +
                        '.loaderrorclass{display:inline-block;cursor:default;background: url(\'' +
                        this.options.themePath +
                        this.options.theme + '/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
                        '}',
                        this.document);
                },
                /* 初始化简单上传按钮 */
                'simpleuploadbtnready': function (type, container) {
                    containerBtn = container;
                    me.afterConfigReady(initUploadBtn);
                }
            },
            outputRule: function (root) {
                UE.utils.each(root.getNodesByTagName('img'), function (n) {
                    if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
                        n.parentNode.removeChild(n);
                    }
                });
            },
            commands: {
                'simpleupload': {
                    queryCommandState: function () {
                        return isLoaded ? 0 : -1;
                    }
                }
            }
        }
    });
    UE.plugin.register('autoupload', function () {

        function sendAndInsertFile(file, editor) {
            var me = editor;
            //模拟数据
            var fieldName, urlPrefix, maxSize, allowFiles, actionUrl,
                loadingHtml, errorHandler, successHandler,
                filetype = /image\/\w+/i.test(file.type) ? 'image' : 'file',
                loadingId = 'loading_' + (+new Date()).toString(36);

            fieldName = me.getOpt(filetype + 'FieldName');
            urlPrefix = me.getOpt(filetype + 'UrlPrefix');
            maxSize = me.getOpt(filetype + 'MaxSize');
            allowFiles = me.getOpt(filetype + 'AllowFiles');
            actionUrl = me.getActionUrl(me.getOpt(filetype + 'ActionName'));
            errorHandler = function (title) {
                var loader = me.document.getElementById(loadingId);
                loader && UE.dom.domUtils.remove(loader);
                me.fireEvent('showmessage', {
                    'id': loadingId,
                    'content': title,
                    'type': 'error',
                    'timeout': 4000
                });
            };

            if (filetype == 'image') {
                loadingHtml = '<img class="loadingclass" id="' + loadingId + '" src="' +
                    me.options.themePath + me.options.theme +
                    '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >';
                successHandler = function (data) {
                    var link = urlPrefix + data.url,
                        loader = me.document.getElementById(loadingId);
                    if (loader) {
                        loader.setAttribute('src', link);
                        loader.setAttribute('_src', link);
                        loader.setAttribute('title', data.title || '');
                        loader.setAttribute('alt', data.original || '');
                        loader.removeAttribute('id');
                        UE.dom.domUtils.removeClasses(loader, 'loadingclass');
                    }
                };
            } else {
                loadingHtml = '<p>' +
                    '<img class="loadingclass" id="' + loadingId + '" src="' +
                    me.options.themePath + me.options.theme +
                    '/images/spacer.gif" title="' + (me.getLang('autoupload.loading') || '') + '" >' +
                    '</p>';
                successHandler = function (data) {
                    var link = urlPrefix + data.url,
                        loader = me.document.getElementById(loadingId);

                    var rng = me.selection.getRange(),
                        bk = rng.createBookmark();
                    rng.selectNode(loader).select();
                    me.execCommand('insertfile', {
                        'url': link
                    });
                    rng.moveToBookmark(bk).select();
                };
            }

            /* 插入loading的占位符 */
            me.execCommand('inserthtml', loadingHtml);

            /* 判断后端配置是否没有加载成功 */
            if (!me.getOpt(filetype + 'ActionName')) {
                errorHandler(me.getLang('autoupload.errorLoadConfig'));
                return;
            }
            /* 判断文件大小是否超出限制 */
            if (file.size > maxSize) {
                errorHandler(me.getLang('autoupload.exceedSizeError'));
                return;
            }
            /* 判断文件格式是否超出允许 */
            var fileext = file.name ? file.name.substr(file.name.lastIndexOf('.')) : '';
            if ((fileext && filetype != 'image') || (allowFiles && (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') == -1)) {
                errorHandler(me.getLang('autoupload.exceedTypeError'));
                return;
            }
            try {
                Upload.api.send(file, function (data) {
                    var url = Fast.api.cdnurl(data.url);
                    successHandler({
                        "state": "SUCCESS",
                        "url": url,
                        "title": file.name,
                        "original": file.name,
                        "type": fileext,
                        "size": file['size']
                    });
                });
            } catch (er) {
                errorHandler(me.getLang('autoupload.loadError'));
            }
        }

        function getPasteImage(e) {
            return e.clipboardData && e.clipboardData.items && e.clipboardData.items.length == 1 && /^image\//.test(e.clipboardData.items[0].type) ? e.clipboardData.items : null;
        }

        function getDropImage(e) {
            return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
        }

        return {
            outputRule: function (root) {
                UE.utils.each(root.getNodesByTagName('img'), function (n) {
                    if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))) {
                        n.parentNode.removeChild(n);
                    }
                });
                UE.utils.each(root.getNodesByTagName('p'), function (n) {
                    if (/\bloadpara\b/.test(n.getAttr('class'))) {
                        n.parentNode.removeChild(n);
                    }
                });
            },
            bindEvents: {
                //插入粘贴板的图片,拖放插入图片
                'ready': function (e) {
                    var me = this;
                    if (window.FormData && window.FileReader) {
                        UE.dom.domUtils.on(me.body, 'paste drop', function (e) {
                            var hasImg = false,
                                items;
                            //获取粘贴板文件列表或者拖放文件列表
                            items = e.type == 'paste' ? getPasteImage(e) : getDropImage(e);
                            if (items) {
                                var len = items.length,
                                    file;
                                while (len--) {
                                    file = items[len];
                                    if (file.getAsFile) file = file.getAsFile();
                                    if (file && file.size > 0) {
                                        sendAndInsertFile(file, me);
                                        hasImg = true;
                                    }
                                }
                                hasImg && e.preventDefault();
                            }

                        });
                        //取消拖放图片时出现的文字光标位置提示
                        UE.dom.domUtils.on(me.body, 'dragover', function (e) {
                            if (e.dataTransfer.types[0] == 'Files') {
                                e.preventDefault();
                            }
                        });

                        //设置loading的样式
                        UE.utils.cssRule('loading',
                            '.loadingclass{display:inline-block;cursor:default;background: url(\'' +
                            this.options.themePath +
                            this.options.theme + '/images/loading.gif\') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n' +
                            '.loaderrorclass{display:inline-block;cursor:default;background: url(\'' +
                            this.options.themePath +
                            this.options.theme + '/images/loaderror.png\') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;' +
                            '}',
                            this.document);
                    }
                }
            }
        }
    });

    /**
     * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
     */
    UE.plugins['catchremoteimage'] = function () {
        var me = this,
            ajax = UE.ajax;

        /* 设置默认值 */
        if (me.options.catchRemoteImageEnable === false) return;
        me.setOpt({
            catchRemoteImageEnable: false
        });

        me.addListener("afterpaste", function () {
            me.fireEvent("catchRemoteImage");
        });

        me.addListener("catchRemoteImage", function () {

            var catcherLocalDomain = me.getOpt('catcherLocalDomain'),
                catcherActionUrl = me.getActionUrl(me.getOpt('catcherActionName')),
                catcherUrlPrefix = me.getOpt('catcherUrlPrefix'),
                catcherFieldName = me.getOpt('catcherFieldName');
                var remoteImages = [],
                imgs = UE.dom.domUtils.getElementsByTagName(me.document, "img"),
                test = function (src, urls) {
                    if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
                        return true;
                    }
                    if (urls) {
                        for (var j = 0, url; url = urls[j++];) {
                            if (src.indexOf(url) !== -1) {
                                return true;
                            }
                        }
                    }
                    return false;
                };
    
            for (var i = 0, ci; ci = imgs[i++];) {
                if (ci.getAttribute("word_img")) {
                    continue;
                }
                var src = ci.getAttribute("_src") || ci.src || "";
                if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
                    remoteImages.push(src);
                }
            }
    
            if (remoteImages.length) {
                catchremoteimage(remoteImages, {
                    //成功抓取
                    success:async function (r) {
                        try {
                            var info = r.state !== undefined ? r:eval("(" + r.responseText + ")");
                        } catch (e) {
                            return;
                        }
    
                        /* 获取源路径和新路径 */
                        var i, j, ci, cj, oldSrc, newSrc, list = info.list;
    
                        for (i = 0; ci = imgs[i++];) {
                            oldSrc = ci.getAttribute("_src") || ci.src || "";
                            for (j = 0; cj = list[j++];) {
                                //抓取失败时不做替换处理
                                if (oldSrc == cj.source && cj.state == "SUCCESS") {  
                                    var file = dataURLtoFile(cj.base64Data, (new Date()).valueOf() + '.jpg');
                                    var imgUrl=await getLoaclImg(file);
                                    newSrc = Fast.api.cdnurl(imgUrl);
                                    UE.dom.domUtils.setAttributes(ci, {
                                        "src": newSrc,
                                        "_src": newSrc
                                    });
                                    break;
                                }
                            }
                        }
                        me.fireEvent('catchremotesuccess')
                    },
                    //回调失败,本次请求超时
                    error: function () {
                        me.fireEvent("catchremoteerror");
                    }
                });
            }

            async function getLoaclImg(file){
                const promise =new Promise((resolve,reject) => {
                    Upload.api.send(file, function (res) {
                        resolve(res.url);
                    })
                })
                return promise;
            }
            
            /**
             * base64转file
             * @param {base64数据} data 
             * @param {文件名} filename 
             */
            function dataURLtoFile(data, filename) {
                var arr = data.split(','),
                    mime = arr[0].match(/:(.*?);/)[1],
                    bstr = atob(arr[1]),
                    n = bstr.length,
                    u8arr = new Uint8Array(n);
                while (n--) {
                    u8arr[n] = bstr.charCodeAt(n);
                }
                return new File([u8arr], filename, {
                    type: mime
                });
            }
            /**
             *获取base64
             *
             * @param {*} img
             * @param {*} callbacks
             */
            function catchremoteimage(img, callbacks) {
                var params = UE.utils.serializeParam(me.queryCommandValue('serverparam')) || '',
                    url = UE.utils.formatUrl(catcherActionUrl + (catcherActionUrl.indexOf('?') == -1 ? '?':'&') + params),
                    isJsonp = UE.utils.isCrossDomainUrl(url),
                    opt = {
                        'method': 'POST',
                        'dataType': isJsonp ? 'jsonp':'',
                        'timeout': 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
                        'onsuccess': callbacks["success"],
                        'onerror': callbacks["error"]
                    };
                opt[catcherFieldName] = img;
                ajax.request(url, opt);
            }
        });
    };

    $(".editor").each(function () {
        var id = $(this).attr("id");
        $(this).removeClass('form-control');
        UE.list[id] = UE.getEditor(id, {
            serverUrl: Fast.api.fixurl('/addons/ueditor/api/'),
            allowDivTransToP: false, //阻止div自动转p标签
            initialFrameWidth: '100%',
            zIndex: 90,
            xssFilterRules: false,
            outputXssFilter: false,
            inputXssFilter: false,
            catchRemoteImageEnable: true
        });
        //监听图片上传事件
        UE.list[id].addListener("uploadBtn.click", function (e, up, editor) {
            var filesObj = up.getFiles();
            for (var i = 0; i < filesObj.length; i++) {
                (function (j) {
                    var file = filesObj[j];
                    var id = filesObj[j].id;
                    var name = filesObj[j].name;
                    Upload.api.send(file.source.source, function (data) {
                        var pic = {
                            url: Fast.api.cdnurl(data.url),
                            state: "SUCCESS",
                            title: name
                        };
                        editor.fireEvent("upload.success", id, pic, file);
                    });
                })(i);
            }
        });
        //打开图片管理
        UE.list[id].addListener("upload.online", function (e, editor, dialog) {
            dialog.close(false);
            Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=image/*", "选择", {
                callback: function (data) {
                    var urlArr = data.url.split(/\,/);
                    urlArr.forEach(function (item, index) {
                        var url = Fast.api.cdnurl(item);
                        editor.execCommand('insertimage', {
                            src: url
                        });
                    });
                }
            });
        });
        // 涂画
        UE.list[id].addListener("upload.scrawl", function (e, editor, base64, dialog) {
            function dataURLtoFile(dataurl, filename) {
                var arr = dataurl.split(','),
                    mime = arr[0].match(/:(.*?);/)[1],
                    bstr = atob(arr[1]),
                    n = bstr.length,
                    u8arr = new Uint8Array(n);
                while (n--) {
                    u8arr[n] = bstr.charCodeAt(n);
                }
                return new File([u8arr], filename, {
                    type: mime
                });
            }

            var file = dataURLtoFile('data:image/png;base64,' + base64, editor.getOpt('scrawlFieldName') + '.png');
            Upload.api.send(file, function (data) {
                editor.execCommand('insertimage', {
                    src: Fast.api.cdnurl(data.url)
                });
                dialog.close(false);
            })
        })
        //视频上传
        UE.list[id].addListener("upload.video", function (e, up, editor) {
            var filesObj = up.getFiles();
            for (var i = 0; i < filesObj.length; i++) {
                (function (j) {
                    var file = filesObj[j];
                    var id = filesObj[j].id;
                    var name = filesObj[j].name;
                    Upload.api.send(file.source.source, function (data) {
                        var pic = {
                            url: Fast.api.cdnurl(data.url),
                            state: "SUCCESS",
                            title: name
                        };
                        editor.fireEvent("video.file.success", id, pic, file);
                    });
                })(i);
            }
        });
        // 附件上传
        UE.list[id].addListener("upload.attachment", function (e, up, editor) {
            var filesObj = up.getFiles();
            for (var i = 0; i < filesObj.length; i++) {
                (function (j) {
                    var file = filesObj[j];
                    var id = filesObj[j].id;
                    var name = filesObj[j].name;
                    Upload.api.send(file.source.source, function (data) {
                        var pic = {
                            url: Fast.api.cdnurl(data.url),
                            state: "SUCCESS",
                            title: name
                        };
                        editor.fireEvent("attachment.file.success", id, pic, file);
                    });
                })(i);
            }
        });
        //打开附件管理
        UE.list[id].addListener("file.online", function (e, editor, dialog) {
            dialog.close(false);
            Fast.api.open("general/attachment/select?element_id=&multiple=true&mimetype=application/*", "选择", {
                callback: function (data) {
                    var urlArr = data.url.split(/\,/);
                    urlArr.forEach(function (item, index) {
                        var url = Fast.api.cdnurl(item);
                        editor.execCommand('insertfile', {
                            url: url
                        });
                    });
                }
            });
        });
        // 修复cms提取关键词和违禁词检测
        UE.list[id].addListener("contentChange", function () {
            $('#' + id).val(this.getContent());
        })
    });
});