1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { ButtonView, FocusCycler, LabeledFieldView, View, ViewCollection, createLabeledInputText, submitHandler } from 'ckeditor5/src/ui';
|
9 | import { FocusTracker, KeystrokeHandler } from 'ckeditor5/src/utils';
|
10 | import { icons } from 'ckeditor5/src/core';
|
11 |
|
12 |
|
13 | import '@ckeditor/ckeditor5-ui/theme/components/responsive-form/responsiveform.css';
|
14 | import '../../theme/mediaform.css';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export default class MediaFormView extends View {
|
21 | |
22 |
|
23 |
|
24 |
|
25 | constructor(validators, locale) {
|
26 | super(locale);
|
27 | const t = locale.t;
|
28 | this.focusTracker = new FocusTracker();
|
29 | this.keystrokes = new KeystrokeHandler();
|
30 | this.set('mediaURLInputValue', '');
|
31 | this.urlInputView = this._createUrlInput();
|
32 | this.saveButtonView = this._createButton(t('Save'), icons.check, 'ck-button-save');
|
33 | this.saveButtonView.type = 'submit';
|
34 | this.saveButtonView.bind('isEnabled').to(this, 'mediaURLInputValue', value => !!value);
|
35 | this.cancelButtonView = this._createButton(t('Cancel'), icons.cancel, 'ck-button-cancel', 'cancel');
|
36 | this._focusables = new ViewCollection();
|
37 | this._focusCycler = new FocusCycler({
|
38 | focusables: this._focusables,
|
39 | focusTracker: this.focusTracker,
|
40 | keystrokeHandler: this.keystrokes,
|
41 | actions: {
|
42 |
|
43 | focusPrevious: 'shift + tab',
|
44 |
|
45 | focusNext: 'tab'
|
46 | }
|
47 | });
|
48 | this._validators = validators;
|
49 | this.setTemplate({
|
50 | tag: 'form',
|
51 | attributes: {
|
52 | class: [
|
53 | 'ck',
|
54 | 'ck-media-form',
|
55 | 'ck-responsive-form'
|
56 | ],
|
57 | tabindex: '-1'
|
58 | },
|
59 | children: [
|
60 | this.urlInputView,
|
61 | this.saveButtonView,
|
62 | this.cancelButtonView
|
63 | ]
|
64 | });
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | render() {
|
70 | super.render();
|
71 | submitHandler({
|
72 | view: this
|
73 | });
|
74 | const childViews = [
|
75 | this.urlInputView,
|
76 | this.saveButtonView,
|
77 | this.cancelButtonView
|
78 | ];
|
79 | childViews.forEach(v => {
|
80 |
|
81 | this._focusables.add(v);
|
82 |
|
83 | this.focusTracker.add(v.element);
|
84 | });
|
85 |
|
86 | this.keystrokes.listenTo(this.element);
|
87 | const stopPropagation = (data) => data.stopPropagation();
|
88 |
|
89 |
|
90 |
|
91 | this.keystrokes.set('arrowright', stopPropagation);
|
92 | this.keystrokes.set('arrowleft', stopPropagation);
|
93 | this.keystrokes.set('arrowup', stopPropagation);
|
94 | this.keystrokes.set('arrowdown', stopPropagation);
|
95 | }
|
96 | |
97 |
|
98 |
|
99 | destroy() {
|
100 | super.destroy();
|
101 | this.focusTracker.destroy();
|
102 | this.keystrokes.destroy();
|
103 | }
|
104 | |
105 |
|
106 |
|
107 | focus() {
|
108 | this._focusCycler.focusFirst();
|
109 | }
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | get url() {
|
117 | return this.urlInputView.fieldView.element.value.trim();
|
118 | }
|
119 | set url(url) {
|
120 | this.urlInputView.fieldView.element.value = url.trim();
|
121 | }
|
122 | |
123 |
|
124 |
|
125 | isValid() {
|
126 | this.resetFormStatus();
|
127 | for (const validator of this._validators) {
|
128 | const errorText = validator(this);
|
129 |
|
130 | if (errorText) {
|
131 |
|
132 | this.urlInputView.errorText = errorText;
|
133 | return false;
|
134 | }
|
135 | }
|
136 | return true;
|
137 | }
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | resetFormStatus() {
|
145 | this.urlInputView.errorText = null;
|
146 | this.urlInputView.infoText = this._urlInputViewInfoDefault;
|
147 | }
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 | _createUrlInput() {
|
154 | const t = this.locale.t;
|
155 | const labeledInput = new LabeledFieldView(this.locale, createLabeledInputText);
|
156 | const inputField = labeledInput.fieldView;
|
157 | this._urlInputViewInfoDefault = t('Paste the media URL in the input.');
|
158 | this._urlInputViewInfoTip = t('Tip: Paste the URL into the content to embed faster.');
|
159 | labeledInput.label = t('Media URL');
|
160 | labeledInput.infoText = this._urlInputViewInfoDefault;
|
161 | inputField.on('input', () => {
|
162 |
|
163 | labeledInput.infoText = inputField.element.value ? this._urlInputViewInfoTip : this._urlInputViewInfoDefault;
|
164 | this.mediaURLInputValue = inputField.element.value.trim();
|
165 | });
|
166 | return labeledInput;
|
167 | }
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | _createButton(label, icon, className, eventName) {
|
178 | const button = new ButtonView(this.locale);
|
179 | button.set({
|
180 | label,
|
181 | icon,
|
182 | tooltip: true
|
183 | });
|
184 | button.extendTemplate({
|
185 | attributes: {
|
186 | class: className
|
187 | }
|
188 | });
|
189 | if (eventName) {
|
190 | button.delegate('execute').to(this, eventName);
|
191 | }
|
192 | return button;
|
193 | }
|
194 | }
|