all files / document-editor/implementation/dialogs/ hyperlink-dialog.js

88.02% Statements 213/242
73.96% Branches 71/96
76% Functions 19/25
88.02% Lines 213/242
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   151× 151× 151× 151× 151× 151× 151× 151× 151× 151× 151×           151×   151×     151×   151×   151×                                           151× 17× 17×   151×                           151×         151×   14486×                   11× 11× 11×   11× 11× 11× 11× 11×               11× 11× 11× 11×   12×               17× 17×       17×                                   18× 18× 18× 18× 18× 18× 18×   213×   213×   213×   213× 213×     213× 213×   213×     213×       213×       213×   213×   213×   213×   213×   213×   213×   213×   213×          
define(["require", "exports", "@syncfusion/ej2-base", "../editor/editor-helper", "@syncfusion/ej2-dropdowns", "@syncfusion/ej2-buttons", "@syncfusion/ej2-inputs", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1, editor_helper_1, ej2_dropdowns_1, ej2_buttons_1, ej2_inputs_1, ej2_base_2) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var HyperlinkDialog = (function () {
        function HyperlinkDialog(documentHelper) {
            var _this = this;
            this.displayText = '';
            this.navigationUrl = undefined;
            this.screenTipText = '';
            this.bookmarkDropdown = undefined;
            this.bookmarkCheckbox = undefined;
            this.keyUpOnDisplayBoxClickHandler = this.onKeyUpOnDisplayBoxClick.bind(this);
            this.onKeyUpOnUrlBoxClickHandler = this.onKeyUpOnUrlBoxClick.bind(this);
            this.onScreenTipTextBoxClickHandler = this.onScreenTipTextBoxClick.bind(this);
            this.bookmarks = [];
            this.onKeyUpOnUrlBox = function (event) {
                if (event.keyCode === 13) {
                    if (_this.displayTextBox.value !== '' && _this.urlTextBox.value !== '') {
                        _this.onInsertHyperlink();
                    }
                    return;
                }
                var urlValue = _this.urlTextBox.value;
                if (urlValue.substring(0, 4).toLowerCase() === 'www.') {
                    _this.urlTextBox.value = 'http://' + urlValue;
                }
                if (_this.displayText === '') {
                    _this.displayTextBox.value = urlValue;
                }
                _this.enableOrDisableInsertButton();
            };
            this.onKeyUpOnDisplayBox = function () {
                _this.displayText = _this.displayTextBox.value;
                _this.enableOrDisableInsertButton();
            };
            this.onScreenTipTextBox = function () {
                _this.screenTipText = _this.screenTipTextBox.value;
            };
            this.onInsertButtonClick = function () {
                _this.onInsertHyperlink();
            };
            this.onCancelButtonClick = function () {
                _this.documentHelper.dialog.hide();
                _this.clearValue();
                _this.documentHelper.updateFocus();
            };
            this.loadHyperlinkDialog = function () {
                _this.documentHelper.updateFocus();
                _this.bookmarks = [];
                for (var i = 0; i < _this.documentHelper.bookmarks.keys.length; i++) {
                    var bookmark = _this.documentHelper.bookmarks.keys[parseInt(i.toString(), 10)];
                    if (bookmark.indexOf('_') !== 0) {
                        _this.bookmarks.push(bookmark);
                    }
                }
                var fieldBegin = _this.documentHelper.selection.getHyperlinkField();
                if (!ej2_base_1.isNullOrUndefined(fieldBegin)) {
                    Eif (!ej2_base_1.isNullOrUndefined(fieldBegin.fieldSeparator)) {
                        var format = undefined;
                        var fieldObj = _this.documentHelper.selection.getHyperlinkDisplayText(fieldBegin.fieldSeparator.line.paragraph, fieldBegin.fieldSeparator, fieldBegin.fieldEnd, false, format);
                        _this.displayText = fieldObj.displayText;
                        _this.displayTextBox.disabled = fieldObj.isNestedField;
                    }
                    _this.displayTextBox.value = _this.displayText;
                    _this.screenTipTextBox.value = _this.documentHelper.selection.getLinkText(fieldBegin, false);
                    var link = _this.documentHelper.selection.getLinkText(fieldBegin, true);
                    _this.urlTextBox.value = _this.navigationUrl = link;
                    _this.documentHelper.dialog.header = _this.localObj.getConstant('Edit Hyperlink');
                }
                else {
                    _this.displayText = _this.documentHelper.selection.getText(true);
                    Iif (_this.displayText !== '') {
                        if (_this.displayText.indexOf(String.fromCharCode(65532)) !== -1 ||
                            _this.displayText.indexOf('\r') !== -1 && (_this.displayText.lastIndexOf('\r') !== -1 &&
                                _this.displayText.slice(0, -1).indexOf('\r') !== -1)) {
                            _this.displayTextBox.value = '<<Selection in document>>';
                            _this.displayTextBox.disabled = true;
                        }
                        else {
                            _this.displayTextBox.value = _this.displayText;
                        }
                    }
                }
                _this.bookmarkDiv.style.display = 'none';
                _this.addressText.style.display = 'block';
                _this.urlTextBox.style.display = 'block';
                _this.bookmarkCheckbox.checked = false;
                _this.bookmarkDropdown.dataSource = _this.documentHelper.bookmarks.keys;
                _this.insertButton = document.getElementsByClassName('e-hyper-insert')[0];
                _this.enableOrDisableInsertButton();
                _this.urlTextBox.focus();
                if (_this.documentHelper.selection.caret.style.display !== 'none') {
                    _this.documentHelper.selection.caret.style.display = 'none';
                }
            };
            this.closeHyperlinkDialog = function () {
                _this.clearValue();
                _this.documentHelper.updateFocus();
            };
            this.onUseBookmarkChange = function (args) {
                if (args.checked) {
                    _this.bookmarkDiv.style.display = 'block';
                    _this.bookmarkDropdown.dataSource = _this.bookmarks;
                    _this.addressText.style.display = 'none';
                    _this.urlTextBox.style.display = 'none';
                }
                else {
                    _this.bookmarkDiv.style.display = 'none';
                    _this.addressText.style.display = 'block';
                    _this.urlTextBox.style.display = 'block';
                }
                _this.enableOrDisableInsertButton();
            };
            this.onBookmarkchange = function () {
                if (_this.bookmarkDropdown.value !== '') {
                    _this.insertButton.disabled = false;
                }
            };
            this.documentHelper = documentHelper;
        }
        HyperlinkDialog.prototype.getModuleName = function () {
            return 'HyperlinkDialog';
        };
        HyperlinkDialog.prototype.initHyperlinkDialog = function (localValue, isRtl) {
            this.target = ej2_base_1.createElement('div', { className: 'e-de-hyperlink' });
            this.container = ej2_base_1.createElement('div');
            this.displayText1 = ej2_base_1.createElement('div', { className: 'e-de-dlg-container' });
            this.displayTextBox = ej2_base_1.createElement('input', { className: 'e-input' });
            this.displayTextBox.addEventListener('keyup', this.keyUpOnDisplayBoxClickHandler);
            this.displayText1.appendChild(this.displayTextBox);
            this.container.appendChild(this.displayText1);
            this.addressText = ej2_base_1.createElement('div', { className: 'e-de-dlg-container' });
            this.urlTextBox = ej2_base_1.createElement('input', { className: 'e-input', attrs: { autofocus: 'true' } });
            this.urlTextBox.addEventListener('input', this.onKeyUpOnUrlBoxClickHandler);
            this.urlTextBox.addEventListener('keyup', this.onKeyUpOnUrlBoxClickHandler);
            this.addressText.appendChild(this.urlTextBox);
            this.container.appendChild(this.addressText);
            this.screenTipText1 = ej2_base_1.createElement('div', { className: 'e-de-dlg-container' });
            this.screenTipTextBox = ej2_base_1.createElement('input', { className: 'e-input' });
            this.screenTipTextBox.addEventListener('keyup', this.onScreenTipTextBoxClickHandler);
            this.screenTipText1.appendChild(this.screenTipTextBox);
            this.container.appendChild(this.screenTipText1);
            this.bookmarkDiv = ej2_base_1.createElement('div', { styles: 'display:none;' });
            this.bookmarkText = ej2_base_1.createElement('div', { className: 'e-de-dlg-container' });
            this.bookmarkValue = ej2_base_1.createElement('input');
            this.bookmarkText.appendChild(this.bookmarkValue);
            this.bookmarkDropdown = new ej2_dropdowns_1.DropDownList({
                dataSource: [], change: this.onBookmarkchange,
                noRecordsTemplate: localValue.getConstant('No bookmarks found'),
                placeholder: localValue.getConstant('Bookmark'), floatLabelType: 'Always'
            });
            this.bookmarkDropdown.appendTo(this.bookmarkValue);
            this.bookmarkDiv.appendChild(this.bookmarkText);
            this.container.appendChild(this.bookmarkDiv);
            this.bookmarkCheckDiv = ej2_base_1.createElement('div');
            this.bookmarkCheck = ej2_base_1.createElement('input', { attrs: { type: 'checkbox' } });
            this.bookmarkCheckDiv.appendChild(this.bookmarkCheck);
            this.bookmarkCheckbox = new ej2_buttons_1.CheckBox({
                label: localValue.getConstant('Use bookmarks'),
                enableRtl: isRtl, change: this.onUseBookmarkChange
            });
            this.bookmarkCheckbox.appendTo(this.bookmarkCheck);
            this.container.appendChild(this.bookmarkCheckDiv);
            this.target.appendChild(this.container);
            new ej2_inputs_1.TextBox({ placeholder: localValue.getConstant('Text to display'), floatLabelType: 'Always' }, this.displayTextBox);
            new ej2_inputs_1.TextBox({ placeholder: localValue.getConstant('Address'), floatLabelType: 'Always' }, this.urlTextBox);
            new ej2_inputs_1.TextBox({ placeholder: localValue.getConstant('ScreenTip text'), floatLabelType: 'Always' }, this.screenTipTextBox);
        };
        HyperlinkDialog.prototype.show = function () {
            this.localObj = new ej2_base_1.L10n('documenteditor', this.documentHelper.owner.defaultLocale);
            this.localObj.setLocale(this.documentHelper.owner.locale);
            if (!this.target) {
                this.initHyperlinkDialog(this.localObj, this.documentHelper.owner.enableRtl);
            }
            this.documentHelper.dialog.header = this.localObj.getConstant('Insert Hyperlink');
            this.documentHelper.dialog.height = 'auto';
            this.documentHelper.dialog.width = 'auto';
            this.documentHelper.dialog.content = this.target;
            this.documentHelper.dialog.buttons = [{
                    click: this.onInsertButtonClick,
                    buttonModel: { content: this.localObj.getConstant('Ok'), cssClass: 'e-flat e-hyper-insert', isPrimary: true }
                },
                {
                    click: this.onCancelButtonClick,
                    buttonModel: { content: this.localObj.getConstant('Cancel'), cssClass: 'e-flat e-hyper-cancel' }
                }];
            this.documentHelper.dialog.dataBind();
            this.documentHelper.dialog.beforeOpen = this.loadHyperlinkDialog;
            this.documentHelper.dialog.close = this.closeHyperlinkDialog;
            this.documentHelper.dialog.show();
        };
        HyperlinkDialog.prototype.hide = function () {
            this.closeHyperlinkDialog();
        };
        HyperlinkDialog.prototype.onKeyUpOnUrlBoxClick = function (event) {
            this.onKeyUpOnUrlBox(event);
        };
        HyperlinkDialog.prototype.onKeyUpOnDisplayBoxClick = function () {
            this.onKeyUpOnDisplayBox();
        };
        HyperlinkDialog.prototype.onScreenTipTextBoxClick = function () {
            this.onScreenTipTextBox();
        };
        HyperlinkDialog.prototype.enableOrDisableInsertButton = function () {
            Eif (!ej2_base_1.isNullOrUndefined(this.insertButton)) {
                Iif (this.bookmarkCheckbox.checked) {
                    this.insertButton.disabled = this.bookmarkDropdown.value === '' || this.bookmarkDropdown.value == null;
                }
                else {
                    this.insertButton.disabled = this.urlTextBox.value === '' || this.displayTextBox.value === '';
                }
            }
        };
        HyperlinkDialog.prototype.onInsertHyperlink = function () {
            var displayText = ej2_base_2.SanitizeHtmlHelper.sanitize(this.displayTextBox.value.trim());
            var address = this.urlTextBox.value.trim();
            if (editor_helper_1.HelperMethods.startsWith(address, 'http://') || editor_helper_1.HelperMethods.startsWith(address, 'https://')) {
                address = ej2_base_2.SanitizeHtmlHelper.sanitize(address.replace(/\s/g, ''));
            }
            var screenTipText = ej2_base_2.SanitizeHtmlHelper.sanitize(this.screenTipTextBox.value.trim());
            var isBookmark = false;
            Iif (!ej2_base_1.isNullOrUndefined(this.bookmarkDropdown.value) && this.bookmarkDropdown.value !== '' && this.bookmarkCheckbox.checked === true) {
                address = this.bookmarkDropdown.value;
                isBookmark = true;
            }
            if (address === '') {
                this.documentHelper.hideDialog();
                return;
            }
            if (screenTipText !== '') {
                address = address + '\"\\o \"' + screenTipText;
            }
            if (displayText === '' && address !== '') {
                displayText = address;
            }
            else {
                displayText = this.displayTextBox.value;
            }
            if (!ej2_base_1.isNullOrUndefined(this.navigationUrl)) {
                this.documentHelper.owner.editorModule.editHyperlink(this.documentHelper.selection, address, displayText, isBookmark);
            }
            else {
                var remove = (this.documentHelper.selection.text !== displayText ||
                    this.documentHelper.selection.text.indexOf('\r') === -1) && !this.displayTextBox.disabled;
                this.documentHelper.owner.editorModule.insertHyperlinkInternal(address, displayText, remove, isBookmark);
            }
            this.documentHelper.hideDialog();
            this.navigationUrl = undefined;
        };
        HyperlinkDialog.prototype.clearValue = function () {
            this.displayTextBox.value = '';
            this.urlTextBox.value = '';
            this.screenTipText = '';
            this.screenTipTextBox.value = '';
            this.displayText = '';
            this.displayTextBox.disabled = false;
            this.bookmarks = [];
        };
        HyperlinkDialog.prototype.destroy = function () {
            if (this.displayTextBox) {
                this.displayTextBox.innerHTML = '';
                this.displayTextBox = undefined;
            }
            if (this.urlTextBox) {
                this.urlTextBox.parentElement.removeChild(this.urlTextBox);
                this.urlTextBox = undefined;
            }
            if (this.screenTipTextBox) {
                this.screenTipTextBox.parentElement.removeChild(this.screenTipTextBox);
                this.screenTipTextBox = undefined;
            }
            this.documentHelper = undefined;
            if (!ej2_base_1.isNullOrUndefined(this.target)) {
                if (this.target.parentElement) {
                    this.target.parentElement.removeChild(this.target);
                }
                this.target.innerHTML = '';
                this.target = undefined;
            }
            this.removeEvents();
            this.removeElements();
        };
        HyperlinkDialog.prototype.removeEvents = function () {
            Iif (this.displayTextBox) {
                this.displayTextBox.removeEventListener('keyup', this.keyUpOnDisplayBoxClickHandler);
            }
            Iif (this.urlTextBox) {
                this.urlTextBox.removeEventListener('input', this.onKeyUpOnUrlBoxClickHandler);
                this.urlTextBox.removeEventListener('keyup', this.onKeyUpOnUrlBoxClickHandler);
            }
            Iif (this.screenTipTextBox) {
                this.screenTipTextBox.removeEventListener('keyup', this.onScreenTipTextBoxClickHandler);
            }
        };
        HyperlinkDialog.prototype.removeElements = function () {
            if (this.container) {
                this.container.remove();
                this.container = undefined;
            }
            if (this.displayText1) {
                this.displayText1.remove();
                this.displayText1 = undefined;
            }
            if (this.addressText) {
                this.addressText.remove();
                this.addressText = undefined;
            }
            if (this.screenTipText1) {
                this.screenTipText1.remove();
                this.screenTipText1 = undefined;
            }
            if (this.bookmarkDiv) {
                this.bookmarkDiv.remove();
                this.bookmarkDiv = undefined;
            }
            if (this.bookmarkText) {
                this.bookmarkText.remove();
                this.bookmarkText = undefined;
            }
            if (this.bookmarkValue) {
                this.bookmarkValue.remove();
                this.bookmarkValue = undefined;
            }
            if (this.bookmarkCheckDiv) {
                this.bookmarkCheckDiv.remove();
                this.bookmarkCheckDiv = undefined;
            }
            if (this.bookmarkCheck) {
                this.bookmarkCheck.remove();
                this.bookmarkCheck = undefined;
            }
        };
        return HyperlinkDialog;
    }());
    exports.HyperlinkDialog = HyperlinkDialog;
});