|
| 1 | +/*globals JSONEditor:false */ |
| 2 | +/** |
| 3 | + * @file DrupalImageEditor class. |
| 4 | + * |
| 5 | + * @external Drupal |
| 6 | + * |
| 7 | + * @external JSONEditor |
| 8 | + */ |
| 9 | + |
| 10 | +export var DrupalImageEditor = JSONEditor.AbstractEditor.extend({ |
| 11 | + getNumColumns: function () { |
| 12 | + return 4; |
| 13 | + }, |
| 14 | + |
| 15 | + build: function () { |
| 16 | + this.title = this.header = this.label = this.theme.getFormInputLabel(this.getTitle(), this.isRequired()); |
| 17 | + // Don't show uploader if this is readonly |
| 18 | + if (!this.schema.readOnly && !this.schema.readonly) { |
| 19 | + this.urlfield = this.theme.getFormInputField('text'); |
| 20 | + let media_library_opener_parameters = { |
| 21 | + field_widget_id: this.urlfield.id |
| 22 | + }; |
| 23 | + let opener_encoded = encodeURIComponent(JSON.stringify(media_library_opener_parameters)); |
| 24 | + this.urlfield.addEventListener('change', function (e) { |
| 25 | + e.preventDefault(); |
| 26 | + e.stopPropagation(); |
| 27 | + Drupal.dialog(jQuery('<div>', {id: 'patternkit_jsonlibrary_image_dialog'}) |
| 28 | + .append(jQuery('<span>', {id: 'patternkit_image_dialog_loading'})), { |
| 29 | + title: Drupal.t('Choose Image'), |
| 30 | + width: 900, |
| 31 | + height: 900 |
| 32 | + }).showModal(); |
| 33 | + Drupal.ajax({ |
| 34 | + url: settings.imageUrl + "&media_library_opener_parameters=" + opener_encoded, |
| 35 | + base: 'patternkit_jsonlibrary_image_dialog', |
| 36 | + wrapper: 'patternkit_image_dialog_loading' |
| 37 | + }).execute({}); |
| 38 | + }); |
| 39 | + } |
| 40 | + let description = this.schema.description || ''; |
| 41 | + this.preview = this.theme.getFormInputDescription(description); |
| 42 | + this.container.appendChild(this.preview); |
| 43 | + this.control = this.theme.getFormControl(this.label, this.urlfield || this.input, this.preview); |
| 44 | + this.container.appendChild(this.control); |
| 45 | + window.requestAnimationFrame(() => { |
| 46 | + if (this.value) { |
| 47 | + let img = document.createElement('img'); |
| 48 | + img.style.maxWidth = '100%'; |
| 49 | + img.style.maxHeight = '100px'; |
| 50 | + img.onload = (event) => { |
| 51 | + this.preview.appendChild(img); |
| 52 | + }; |
| 53 | + img.onerror = (error) => { |
| 54 | + console.error('upload error', error); |
| 55 | + }; |
| 56 | + img.src = this.container.querySelector('a').href; |
| 57 | + } |
| 58 | + }); |
| 59 | + }, |
| 60 | + |
| 61 | + refreshPreview: function() { |
| 62 | + if (this.last_preview === this.preview_value) { |
| 63 | + return; |
| 64 | + } |
| 65 | + this.last_preview = this.preview_value; |
| 66 | + this.preview.innerHTML = ''; |
| 67 | + if (!this.preview_value) { |
| 68 | + return; |
| 69 | + } |
| 70 | + let mime = this.preview_value.match(/^data:([^;,]+)[;,]/); |
| 71 | + if (mime) { |
| 72 | + mime = mime[1]; |
| 73 | + } |
| 74 | + else { |
| 75 | + mime = 'unknown'; |
| 76 | + } |
| 77 | + let file = this.urlfield.files[0]; |
| 78 | + this.preview.innerHTML = '<strong>Type:</strong> ' + mime + ', <strong>Size:</strong> ' + file.size + ' bytes'; |
| 79 | + if (mime.substr(0, 5) === "image") { |
| 80 | + this.preview.innerHTML += '<br>'; |
| 81 | + let img = document.createElement('img'); |
| 82 | + img.style.maxWidth = '100%'; |
| 83 | + img.style.maxHeight = '100px'; |
| 84 | + img.src = this.preview_value; |
| 85 | + this.preview.appendChild(img); |
| 86 | + } |
| 87 | + this.preview.innerHTML += '<br>'; |
| 88 | + let uploadButton = this.getButton('Upload', 'upload', 'Upload'); |
| 89 | + this.preview.appendChild(uploadButton); |
| 90 | + uploadButton.addEventListener('click', (event) => { |
| 91 | + event.preventDefault(); |
| 92 | + uploadButton.setAttribute("disabled", "disabled"); |
| 93 | + this.theme.removeInputError(this.uploader); |
| 94 | + if (this.theme.getProgressBar) { |
| 95 | + this.progressBar = this.theme.getProgressBar(); |
| 96 | + this.preview.appendChild(this.progressBar); |
| 97 | + } |
| 98 | + this.jsoneditor.options.upload(this.path, file, { |
| 99 | + success: (url) => { |
| 100 | + this.setValue(url); |
| 101 | + if (this.parent) { |
| 102 | + this.parent.onChildEditorChange(this); |
| 103 | + } |
| 104 | + else { |
| 105 | + this.jsoneditor.onChange(); |
| 106 | + } |
| 107 | + if (this.progressBar) { |
| 108 | + this.preview.removeChild(this.progressBar); |
| 109 | + } |
| 110 | + uploadButton.removeAttribute("disabled"); |
| 111 | + }, |
| 112 | + failure: (error) => { |
| 113 | + this.theme.addInputError(this.uploader, error); |
| 114 | + if (this.progressBar) { |
| 115 | + this.preview.removeChild(this.progressBar); |
| 116 | + } |
| 117 | + uploadButton.removeAttribute("disabled"); |
| 118 | + }, |
| 119 | + updateProgress: (progress) => { |
| 120 | + if (this.progressBar) { |
| 121 | + if (progress) { |
| 122 | + this.theme.updateProgressBar(this.progressBar, progress); |
| 123 | + } |
| 124 | + else { |
| 125 | + this.theme.updateProgressBarUnknown(this.progressBar); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + }); |
| 131 | + if (this.jsoneditor.options.auto_upload || this.schema.options.auto_upload) { |
| 132 | + uploadButton.dispatchEvent(new MouseEvent('click')); |
| 133 | + this.preview.removeChild(uploadButton); |
| 134 | + } |
| 135 | + }, |
| 136 | + |
| 137 | + enable: function () { |
| 138 | + if (!this.always_disabled) { |
| 139 | + if (this.urlfield) { |
| 140 | + this.urlfield.disabled = false; |
| 141 | + } |
| 142 | + this._super(); |
| 143 | + } |
| 144 | + }, |
| 145 | + |
| 146 | + disable: function (always_disabled) { |
| 147 | + if (always_disabled) { |
| 148 | + this.always_disabled = true; |
| 149 | + } |
| 150 | + if (this.urlfield) { |
| 151 | + this.urlfield.disabled = true; |
| 152 | + } |
| 153 | + this._super(); |
| 154 | + }, |
| 155 | + |
| 156 | + setValue: function (val) { |
| 157 | + if (this.value !== val) { |
| 158 | + this.value = val; |
| 159 | + this.urlfield.value = this.value; |
| 160 | + this.onChange(); |
| 161 | + } |
| 162 | + }, |
| 163 | + |
| 164 | + destroy: function () { |
| 165 | + if (this.preview && this.preview.parentNode) { |
| 166 | + this.preview.parentNode.removeChild(this.preview); |
| 167 | + } |
| 168 | + if (this.title && this.title.parentNode) { |
| 169 | + this.title.parentNode.removeChild(this.title); |
| 170 | + } |
| 171 | + if (this.input && this.input.parentNode) { |
| 172 | + this.input.parentNode.removeChild(this.input); |
| 173 | + } |
| 174 | + if (this.urlfield && this.urlfield.parentNode) { |
| 175 | + this.urlfield.parentNode.removeChild(this.urlfield); |
| 176 | + } |
| 177 | + this._super(); |
| 178 | + } |
| 179 | +}); |
0 commit comments