| 1 |
/** |
| 2 |
* @package VikBooking |
| 3 |
* @subpackage vik-content-builder |
| 4 |
* @author Alessio Gaggii - E4J srl |
| 5 |
* @copyright Copyright (C) 2022 E4J srl. All rights reserved. |
| 6 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 7 |
* @link https://vikwp.com - https://e4j.com - https://e4jconnect.com |
| 8 |
*/ |
| 9 |
|
| 10 |
// class handler to store the editor instances and switch building modes |
| 11 |
class VikContentBuilder { |
| 12 |
|
| 13 |
static editors = new Array; |
| 14 |
|
| 15 |
static pushEditor(editor) { |
| 16 |
this.editors.push(editor); |
| 17 |
} |
| 18 |
|
| 19 |
// method for switching between building modes |
| 20 |
static switchMode(elem, mode) { |
| 21 |
var btn = jQuery(elem); |
| 22 |
if (mode) { |
| 23 |
btn = btn.closest('.vik-contentbuilder-wrapper').find('.vik-contentbuilder-switcher-btn[data-switch="' + mode + '"]'); |
| 24 |
} |
| 25 |
var switch_to = btn.attr("data-switch"); |
| 26 |
|
| 27 |
btn.parent(".vik-contentbuilder-switcher").find(".vik-contentbuilder-switcher-btn").removeClass("vik-contentbuilder-switcher-btn-active"); |
| 28 |
btn.addClass("vik-contentbuilder-switcher-btn-active"); |
| 29 |
|
| 30 |
var ed_conts = btn.closest(".vik-contentbuilder-wrapper"); |
| 31 |
ed_conts.find(".vik-contentbuilder-inner [data-switch]").not('[data-switch="' + switch_to + '"]').hide(); |
| 32 |
|
| 33 |
var target = ed_conts.find('.vik-contentbuilder-inner [data-switch="' + switch_to + '"]'); |
| 34 |
if (switch_to.indexOf('visual') >= 0 && !target.find('.vik-contentbuilder-editor-container').length) { |
| 35 |
// handle multiple visual modes (inline and modal) |
| 36 |
var append_to_child = target.attr('data-appendto'); |
| 37 |
if (append_to_child) { |
| 38 |
ed_conts.find('.vik-contentbuilder-editor-container').appendTo(target.find(append_to_child)); |
| 39 |
} else { |
| 40 |
ed_conts.find('.vik-contentbuilder-editor-container').appendTo(target); |
| 41 |
} |
| 42 |
} |
| 43 |
target.show(); |
| 44 |
|
| 45 |
// handle text mode special tags |
| 46 |
var text_mode_tags = jQuery('.vik-contentbuilder-textmode-sptags'); |
| 47 |
if (text_mode_tags.length && switch_to.indexOf('text') >= 0) { |
| 48 |
text_mode_tags.show(); |
| 49 |
} else if (text_mode_tags.length && switch_to.indexOf('visual') >= 0) { |
| 50 |
text_mode_tags.hide(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
// class handler for uploading an image file and display it through Quill |
| 57 |
class VikContentBuilderImageHandler { |
| 58 |
|
| 59 |
constructor(editor, endpoint) { |
| 60 |
this.editor = editor; |
| 61 |
this.endpoint = endpoint || null; |
| 62 |
} |
| 63 |
|
| 64 |
setEndpoint(endpoint) { |
| 65 |
if (endpoint && endpoint.length) { |
| 66 |
this.endpoint = endpoint; |
| 67 |
} |
| 68 |
return this; |
| 69 |
} |
| 70 |
|
| 71 |
present() { |
| 72 |
var input = document.createElement('input'); |
| 73 |
input.setAttribute('type', 'file'); |
| 74 |
input.onchange = () => { |
| 75 |
var file = input.files[0]; |
| 76 |
var rgxi = /^image\//; |
| 77 |
if (!file || !file.name || !rgxi.test(file.type)) { |
| 78 |
alert('Only images are allowed.'); |
| 79 |
return; |
| 80 |
} |
| 81 |
this.upload(file); |
| 82 |
}; |
| 83 |
input.click(); |
| 84 |
} |
| 85 |
|
| 86 |
upload(file) { |
| 87 |
if (!file instanceof File) { |
| 88 |
console.error('Invalid file'); |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
if (!this.endpoint) { |
| 93 |
console.error('Missing endpoint'); |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
// build form data |
| 98 |
var formd = new FormData(); |
| 99 |
formd.append('file', file); |
| 100 |
formd.append('type', 'image'); |
| 101 |
|
| 102 |
// start progress |
| 103 |
this.startProgress(); |
| 104 |
|
| 105 |
// perform the AJAX upload |
| 106 |
var that = this; |
| 107 |
var jqxhr = jQuery.ajax({ |
| 108 |
xhr: function() { |
| 109 |
var xhrobj = jQuery.ajaxSettings.xhr(); |
| 110 |
if (xhrobj.upload) { |
| 111 |
xhrobj.upload.addEventListener('progress', function(event) { |
| 112 |
var percent = 0; |
| 113 |
var position = event.loaded || event.position; |
| 114 |
var total = event.total; |
| 115 |
if (event.lengthComputable) { |
| 116 |
percent = Math.ceil(position / total * 100); |
| 117 |
} |
| 118 |
// update progress |
| 119 |
that.setProgress(percent); |
| 120 |
}, false); |
| 121 |
} |
| 122 |
return xhrobj; |
| 123 |
}, |
| 124 |
url: that.endpoint, |
| 125 |
type: 'POST', |
| 126 |
contentType: false, |
| 127 |
processData: false, |
| 128 |
cache: false, |
| 129 |
data: formd, |
| 130 |
success: function(resp) { |
| 131 |
try { |
| 132 |
resp = JSON.parse(resp); |
| 133 |
if (resp.status == 1) { |
| 134 |
that.complete(resp); |
| 135 |
} else { |
| 136 |
throw resp.error ? resp.error : 'An error occurred! Please try again.'; |
| 137 |
} |
| 138 |
} catch (err) { |
| 139 |
that.stopProgress(); |
| 140 |
console.warn(err, resp); |
| 141 |
alert(err); |
| 142 |
} |
| 143 |
}, |
| 144 |
error: function(err) { |
| 145 |
that.stopProgress(); |
| 146 |
console.error(err.responseText); |
| 147 |
alert('An error occurred. Please try again.'); |
| 148 |
}, |
| 149 |
}); |
| 150 |
} |
| 151 |
|
| 152 |
complete(resp) { |
| 153 |
this.stopProgress(); |
| 154 |
this.editor.insertEmbed(this.editor.getSelection().index, 'image', resp.url); |
| 155 |
} |
| 156 |
|
| 157 |
startProgress() { |
| 158 |
// access the toolbar module |
| 159 |
var toolbar = null; |
| 160 |
try { |
| 161 |
toolbar = this.editor.getModule('toolbar').container; |
| 162 |
} catch(e) { |
| 163 |
// do nothing |
| 164 |
} |
| 165 |
if (!toolbar) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
// create progress element |
| 170 |
this.progress_el = document.createElement("PROGRESS"); |
| 171 |
this.progress_el.setAttribute('max', 100); |
| 172 |
this.progress_el.setAttribute('value', 0); |
| 173 |
|
| 174 |
// append progress to toolbar |
| 175 |
toolbar.appendChild(this.progress_el); |
| 176 |
} |
| 177 |
|
| 178 |
setProgress(value) { |
| 179 |
if (!this.progress_el) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
this.progress_el.setAttribute('value', value); |
| 183 |
} |
| 184 |
|
| 185 |
stopProgress() { |
| 186 |
if (!this.progress_el) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
this.progress_el.remove(); |
| 190 |
this.progress_el = null; |
| 191 |
} |
| 192 |
|
| 193 |
} |
| 194 |
|