PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.9
VikBooking Hotel Booking Engine & PMS v1.6.9
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / resources / quill / vik-content-builder.js

vik-content-builder.js in VikBooking Hotel Booking Engine & PMS 1.6.9, at admin/resources/quill/vik-content-builder.js

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