index.js 18.9 KB
Newer Older
P
Peter Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright 2020 Baidu Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

P
Peter Pan 已提交
17 18
// cSpell:words actived nextcode

19
const view = require('./view');
R
RotPublic 已提交
20
const view2 = require('./view2');
21
const host = {};
P
Peter Pan 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
host.BrowserHost = class {
    constructor() {
        window.eval = () => {
            throw new Error('window.eval() not supported.');
        };
        this._document = window.document;
        this._meta = {};
        for (const element of Array.from(this._document.getElementsByTagName('meta'))) {
            if (element.content) {
                this._meta[element.name] = this._meta[element.name] || [];
                this._meta[element.name].push(element.content);
            }
        }
        this._type = this._meta.type ? this._meta.type[0] : 'Browser';
        this._version = this._meta.version ? this._meta.version[0] : null;
37
        this._ready = false;
P
Peter Pan 已提交
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
    }

    get document() {
        return this._document;
    }

    get version() {
        return this._version;
    }

    get type() {
        return this._type;
    }

    initialize(view) {
        this._view = view;
        return Promise.resolve();
    }
    start() {
        window.addEventListener(
            'message',
            event => {
                const originalData = event.data;
                if (originalData) {
                    const type = originalData.type;
                    const data = originalData.data;
                    switch (type) {
R
RotPublic 已提交
65
                        // 在此书添加一个this._view的事件传递Graph页面过来的数据
P
Peter Pan 已提交
66 67 68 69 70 71
                        case 'change-files':
                            return this._changeFiles(data);
                        case 'zoom-in':
                            return this._view.zoomIn();
                        case 'zoom-out':
                            return this._view.zoomOut();
R
RotPublic 已提交
72 73 74 75 76 77
                        case 'select-item':
                            return this._view.selectItem(data);
                        case 'toggle-Language':
                            return this._view.toggleLanguage(data);
                        case 'isAlt':
                            return this._view.changeAlt(data);
P
Peter Pan 已提交
78 79 80 81 82 83 84 85
                        case 'zoom-reset':
                            return this._view.resetZoom();
                        case 'toggle-attributes':
                            return this._view.toggleAttributes(data);
                        case 'toggle-initializers':
                            return this._view.toggleInitializers(data);
                        case 'toggle-names':
                            return this._view.toggleNames(data);
R
RotPublic 已提交
86 87
                        case 'toggle-KeepData':
                            return this._view.toggleKeepData(data);
P
Peter Pan 已提交
88 89
                        case 'toggle-direction':
                            return this._view.toggleDirection(data);
P
Peter Pan 已提交
90 91
                        case 'toggle-theme':
                            return this._view.toggleTheme(data);
P
Peter Pan 已提交
92 93
                        case 'export':
                            return this._view.export(`${document.title}.${data}`);
94 95
                        case 'change-graph':
                            return this._view.changeGraph(data);
R
RotPublic 已提交
96 97 98 99
                        case 'change-allGraph':
                            return this._view.changeAllGrap(data);
                        case 'change-select':
                            return this._view.changeSelect(data);
P
Peter Pan 已提交
100 101 102 103 104 105 106 107
                        case 'search':
                            return this._view.find(data);
                        case 'select':
                            return this._view.select(data);
                        case 'show-model-properties':
                            return this._view.showModelProperties();
                        case 'show-node-documentation':
                            return this._view.showNodeDocumentation(data);
108 109
                        case 'ready':
                            if (this._ready) {
R
RotPublic 已提交
110
                                debugger;
111 112 113
                                return this.status('ready');
                            }
                            return;
P
Peter Pan 已提交
114 115 116 117 118 119
                    }
                }
            },
            false
        );

120
        this._ready = true;
P
Peter Pan 已提交
121 122 123 124 125
        this.status('ready');
    }

    message(type, data) {
        if (window.parent) {
126
            window.parent.postMessage({type: type, data: data}, '*');
P
Peter Pan 已提交
127 128 129 130
        }
    }

    status(status) {
R
RotPublic 已提交
131
        // 反传回去
P
Peter Pan 已提交
132 133
        this.message('status', status);
    }
R
RotPublic 已提交
134 135 136 137 138 139 140 141
    selectNodeId(nodeInfo) {
        // 反传回去
        this.message('nodeId', nodeInfo);
    }
    selectItems(item) {
        // 反传回去
        this.message('selectItem', item);
    }
P
Peter Pan 已提交
142 143

    error(message, detail) {
144
        this.message('error', (message === 'Error' ? '' : message + ' ') + detail);
P
Peter Pan 已提交
145 146 147
    }

    confirm(message, detail) {
148 149 150 151 152
        const result = confirm(message + ' ' + detail);
        if (!result) {
            this.message('cancel');
        }
        return result;
P
Peter Pan 已提交
153 154 155 156 157 158 159 160 161 162
    }

    require(id) {
        const url = this._url(id + '.js');
        window.__modules__ = window.__modules__ || {};
        if (window.__modules__[url]) {
            return Promise.resolve(window.__exports__[url]);
        }
        return new Promise((resolve, reject) => {
            window.module = {exports: {}};
163
            const script = document.createElement('script');
P
Peter Pan 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
            script.setAttribute('id', id);
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('src', url);
            script.onload = () => {
                const exports = window.module.exports;
                delete window.module;
                window.__modules__[id] = exports;
                resolve(exports);
            };
            script.onerror = e => {
                delete window.module;
                reject(new Error("The script '" + e.target.src + "' failed to load."));
            };
            this.document.head.appendChild(script);
        });
    }

    save(name, extension, defaultPath, callback) {
        callback(defaultPath + '.' + extension);
    }

    export(file, blob) {
186
        const element = this.document.createElement('a');
P
Peter Pan 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199
        element.download = file;
        element.href = URL.createObjectURL(blob);
        this.document.body.appendChild(element);
        element.click();
        this.document.body.removeChild(element);
    }

    request(base, file, encoding) {
        const url = base ? base + '/' + file : this._url(file);
        return this._request(url, null, encoding);
    }

    _changeFiles(files) {
R
RotPublic 已提交
200 201 202
        console.log('files', files);
        if (files && files?.length) {
            console.log('files.length', files.length);
P
Peter Pan 已提交
203
            files = Array.from(files);
204 205 206 207 208
            const file = files.find(file => this._view.accept(file.name));
            if (!file) {
                this.error('Error opening file.', 'Cannot open file ' + files[0].name);
                return;
            }
P
Peter Pan 已提交
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
            this._open(
                files.find(file => this._view.accept(file.name)),
                files
            );
        }
    }

    _request(url, headers, encoding, timeout) {
        return new Promise((resolve, reject) => {
            const request = new XMLHttpRequest();
            if (!encoding) {
                request.responseType = 'arraybuffer';
            }
            if (timeout) {
                request.timeout = timeout;
            }
            const error = status => {
                const err = new Error('The web request failed with status code ' + status + " at '" + url + "'.");
                err.type = 'error';
                err.url = url;
                return err;
            };
            request.onload = () => {
                if (request.status == 200) {
                    if (request.responseType == 'arraybuffer') {
                        resolve(new Uint8Array(request.response));
                    } else {
                        resolve(request.responseText);
                    }
                } else {
                    reject(error(request.status));
                }
            };
            request.onerror = e => {
                const err = error(request.status);
                err.type = e.type;
                reject(err);
            };
            request.ontimeout = () => {
                request.abort();
                const err = new Error("The web request timed out in '" + url + "'.");
                err.type = 'timeout';
                err.url = url;
                reject(err);
            };
            request.open('GET', url, true);
            if (headers) {
                for (const name of Object.keys(headers)) {
                    request.setRequestHeader(name, headers[name]);
                }
            }
            request.send();
        });
    }

    _url(file) {
        let url = file;
        if (window && window.location && window.location.href) {
            let location = window.location.href.split('?').shift();
            if (location.endsWith('.html')) {
                location = location.split('/').slice(0, -1).join('/');
            }
            if (location.endsWith('/')) {
                location = location.slice(0, -1);
            }
            url = location + '/' + file;
        }
        return url;
    }

    _open(file, files) {
        this.status('loading');
        const context = new BrowserFileContext(file, files);
        context
            .open()
            .then(() => {
                return this._view.open(context).then(model => {
                    if (this._view.actived) {
                        this.status('rendered');
                    }
                    this.document.title = files[0].name;
                    return model;
                });
            })
            .catch(error => {
                this.error(error.name, error.message);
            });
    }
};

if (typeof TextDecoder === 'undefined') {
    TextDecoder = function TextDecoder(encoding) {
        this._encoding = encoding;
    };
    TextDecoder.prototype.decode = function decode(buffer) {
        let result = '';
        const length = buffer.length;
        let i = 0;
        switch (this._encoding) {
            case 'utf-8':
                while (i < length) {
                    const c = buffer[i++];
                    switch (c >> 4) {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7: {
                            result += String.fromCharCode(c);
                            break;
                        }
                        case 12:
                        case 13: {
                            const c2 = buffer[i++];
                            result += String.fromCharCode(((c & 0x1f) << 6) | (c2 & 0x3f));
                            break;
                        }
                        case 14: {
                            const c2 = buffer[i++];
                            const c3 = buffer[i++];
                            result += String.fromCharCode(((c & 0x0f) << 12) | ((c2 & 0x3f) << 6) | ((c3 & 0x3f) << 0));
                            break;
                        }
                    }
                }
                break;
            case 'ascii':
                while (i < length) {
                    result += String.fromCharCode(buffer[i++]);
                }
                break;
        }
        return result;
    };
}

if (typeof TextEncoder === 'undefined') {
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    TextEncoder = function TextEncoder() {};
    TextEncoder.prototype.encode = function encode(str) {
        'use strict';
        const length = str.length;
        let resPos = -1;
        const resArr = typeof Uint8Array === 'undefined' ? new Array(length * 2) : new Uint8Array(length * 3);
        for (let point = 0, nextcode = 0, i = 0; i !== length; ) {
            point = str.charCodeAt(i);
            i += 1;
            if (point >= 0xd800 && point <= 0xdbff) {
                if (i === length) {
                    resArr[(resPos += 1)] = 0xef;
                    resArr[(resPos += 1)] = 0xbf;
                    resArr[(resPos += 1)] = 0xbd;
                    break;
                }
                nextcode = str.charCodeAt(i);
                if (nextcode >= 0xdc00 && nextcode <= 0xdfff) {
                    point = (point - 0xd800) * 0x400 + nextcode - 0xdc00 + 0x10000;
                    i += 1;
                    if (point > 0xffff) {
                        resArr[(resPos += 1)] = (0x1e << 3) | (point >>> 18);
                        resArr[(resPos += 1)] = (0x2 << 6) | ((point >>> 12) & 0x3f);
                        resArr[(resPos += 1)] = (0x2 << 6) | ((point >>> 6) & 0x3f);
                        resArr[(resPos += 1)] = (0x2 << 6) | (point & 0x3f);
                        continue;
                    }
                } else {
                    resArr[(resPos += 1)] = 0xef;
                    resArr[(resPos += 1)] = 0xbf;
                    resArr[(resPos += 1)] = 0xbd;
                    continue;
                }
            }
            if (point <= 0x007f) {
                resArr[(resPos += 1)] = (0x0 << 7) | point;
            } else if (point <= 0x07ff) {
                resArr[(resPos += 1)] = (0x6 << 5) | (point >>> 6);
                resArr[(resPos += 1)] = (0x2 << 6) | (point & 0x3f);
            } else {
                resArr[(resPos += 1)] = (0xe << 4) | (point >>> 12);
                resArr[(resPos += 1)] = (0x2 << 6) | ((point >>> 6) & 0x3f);
                resArr[(resPos += 1)] = (0x2 << 6) | (point & 0x3f);
            }
        }
        if (typeof Uint8Array !== 'undefined') {
            return new Uint8Array(resArr.buffer.slice(0, resPos + 1));
        } else {
            return resArr.length === resPos + 1 ? resArr : resArr.slice(0, resPos + 1);
        }
    };
    TextEncoder.prototype.toString = function () {
        return '[object TextEncoder]';
    };
    try {
        Object.defineProperty(TextEncoder.prototype, 'encoding', {
            get: function () {
                if (Object.prototype.isPrototypeOf.call(TextEncoder.prototype, this)) {
                    return 'utf-8';
                } else {
                    throw TypeError('Illegal invocation');
                }
            }
        });
    } catch (e) {
        TextEncoder.prototype.encoding = 'utf-8';
    }
    if (typeof Symbol !== 'undefined') {
        TextEncoder.prototype[Symbol.toStringTag] = 'TextEncoder';
    }
}

if (typeof URLSearchParams === 'undefined') {
    URLSearchParams = function URLSearchParams(search) {
        const decode = str => {
            return str.replace(/[ +]/g, '%20').replace(/(%[a-f0-9]{2})+/gi, match => {
                return decodeURIComponent(match);
            });
        };
        this._dict = {};
        if (typeof search === 'string') {
            search = search.indexOf('?') === 0 ? search.substring(1) : search;
            const properties = search.split('&');
            for (const property of properties) {
                const index = property.indexOf('=');
                const name = index > -1 ? decode(property.substring(0, index)) : decode(property);
                const value = index > -1 ? decode(property.substring(index + 1)) : '';
                if (!Object.prototype.hasOwnProperty.call(this._dict, name)) {
                    this._dict[name] = [];
                }
                this._dict[name].push(value);
            }
        }
    };
    URLSearchParams.prototype.get = function (name) {
        return Object.prototype.hasOwnProperty.call(this._dict, name) ? this._dict[name][0] : null;
    };
}

if (!HTMLCanvasElement.prototype.toBlob) {
    HTMLCanvasElement.prototype.toBlob = function (callback, type, quality) {
        setTimeout(() => {
            const data = atob(this.toDataURL(type, quality).split(',')[1]);
            const length = data.length;
            const buffer = new Uint8Array(length);
            for (let i = 0; i < length; i++) {
                buffer[i] = data.charCodeAt(i);
            }
            callback(new Blob([buffer], {type: type || 'image/png'}));
        }, 0);
    };
}

class BrowserFileContext {
    constructor(file, blobs) {
        this._file = file;
        this._blobs = {};
        for (const blob of blobs) {
            this._blobs[blob.name] = blob;
        }
    }

    get identifier() {
        return this._file.name;
    }

    get buffer() {
        return this._buffer;
    }

    open() {
        return this.request(this._file.name, null).then(data => {
            this._buffer = data;
        });
    }

    request(file, encoding) {
        const blob = this._blobs[file];
        if (!blob) {
            return Promise.reject(new Error("File not found '" + file + "'."));
        }
        return new Promise((resolve, reject) => {
492
            const reader = new FileReader();
P
Peter Pan 已提交
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
            reader.onload = e => {
                resolve(encoding ? e.target.result : new Uint8Array(e.target.result));
            };
            reader.onerror = e => {
                e = e || window.event;
                let message = '';
                switch (e.target.error.code) {
                    case e.target.error.NOT_FOUND_ERR:
                        message = "File not found '" + file + "'.";
                        break;
                    case e.target.error.NOT_READABLE_ERR:
                        message = "File not readable '" + file + "'.";
                        break;
                    case e.target.error.SECURITY_ERR:
                        message = "File access denied '" + file + "'.";
                        break;
                    default:
                        message = "File read '" + e.target.error.code.toString() + "' error '" + file + "'.";
                        break;
                }
                reject(new Error(message));
            };
            if (encoding === 'utf-8') {
                reader.readAsText(blob, encoding);
            } else {
                reader.readAsArrayBuffer(blob);
            }
        });
    }
}

R
RotPublic 已提交
524 525 526 527 528 529
function getCaption(obj) {
    let index = obj.lastIndexOf('/'); //获取-后边的字符串
    let newObj = obj.substring(index + 1, obj.length);
    return newObj;
}
const hash = getCaption(document.referrer);
R
RotPublic 已提交
530
if (hash === 'static_graph' || hash === 'x2paddle') {
R
RotPublic 已提交
531 532 533 534
    window.__view__ = new view2.View(new host.BrowserHost());
} else {
    window.__view__ = new view.View(new host.BrowserHost());
}