Undo
[redakcja.git] / src / redakcja / static / js / wiki / view_properties.js
1 (function($){
2
3     let w = function() {};
4     w = console.log;
5
6     const elementDefs = {
7         "ilustr": {
8             "attributes": [
9                 {
10                     "name": "src",
11                     "type": "media",
12                 },
13                 {
14                     "name": "alt",
15                     "type": "text",
16                 },
17                 {
18                     "name": "szer",
19                     "type": "percent",
20                 },
21                 {
22                     "name": "wyrownanie",
23                     "type": "select",
24                     "options": ["lewo", "srodek", "prawo"],
25                 },
26                 {
27                     "name": "oblew",
28                     "type": "bool",
29                 },
30             ],
31         },
32         "ref": {
33             "attributes": [
34                 {
35                     "name": "href",
36                 },
37             ],
38         }
39     };
40
41     class PropertiesPerspective extends $.wiki.SidebarPerspective {
42         vsplitbar = 'WŁAŚCIWOŚCI';
43         $edited = null;
44
45         constructor(options) {
46             super(options);
47                 let self = this;
48
49                 self.$pane = $("#side-properties");
50
51                 $("#simple-editor").on('click', '[x-node]', function(e) {
52                     if (!e.redakcja_edited) {
53                         e.redakcja_edited = true;
54                         self.edit(this);
55                     }
56                 });
57
58                 self.$pane.on('click', '#parents li', function(e) {
59                     self.edit($(this).data('node'));
60                 });
61
62                 $(document).on('click', '#bubbles .badge', function(e) {
63                     self.edit($(this).data('node'));
64                 });
65
66                 self.$pane.on('change', '.form-control', function() {
67                     let $input = $(this);
68
69                     let inputval;
70                     if ($input.attr('type') == 'checkbox') {
71                         inputval = $input.is(':checked');
72                     } else {
73                         inputval = $input.val();
74                     }
75
76                     if ($input.data("edited")) {
77                         if ($input.data("edited-attr")) {
78                             $input.data("edited").attr($input.data("edited-attr"), inputval);
79                         } else {
80                             $input.data("edited").text(inputval);
81                         }
82                         $.wiki.perspectives.VisualPerspective.flush();
83                         return;
84                     }
85
86                     html2text({
87                         element: self.$edited[0],
88                         success: function(xml) {
89                             w(222)
90                             let $xmlelem = $($.parseXML(xml));
91                             w(333, $xmlelem)
92                             w($input.data('property'), $input.val());
93                             $xmlelem.contents().attr($input.data('property'), inputval);
94                             w(444, $xmlelem)
95                             let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
96                             w(555, newxml)
97                             xml2html({
98                                 xml: newxml,
99                                 base: self.doc.getBase(),
100                                 success: function(html) {
101                                     let htmlElem = $(html);
102                                     self.$edited.replaceWith(htmlElem);
103                                     self.edit(htmlElem);
104                                     $.wiki.activePerspective().flush();
105                                 }
106                             });
107                         },
108                         error: function(e) {console.log(e);},
109                     });
110                     self.$edited;
111                 });
112
113
114                 self.$pane.on('click', '.meta-add', function() {
115                     // create a metadata item
116                     let $fg = $(this).parent();
117                     let ns = $fg.data('ns');
118                     let tag = $fg.data('tag');
119                     let field = $fg.data('field');
120                     let span = $('<span/>');
121                     span.attr('x-node', tag);
122                     span.attr('x-ns', ns)
123                     if (field.value_type.hasLanguage) {
124                         span.attr('x-a-xml-lang', 'pl');
125                     }
126
127                     let rdf = $("> [x-node='RDF']", self.$edited);
128                     if (!rdf.length) {
129                         rdf = $("<span x-node='RDF' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>");
130                         self.$edited.prepend(rdf);
131                         self.$edited.prepend('\n  ');
132
133                     }
134                     let rdfdesc = $("> [x-node='Description']", rdf);
135                     if (!rdfdesc.length) {
136                         rdfdesc = $("<span x-node='Description' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#' x-a-rdf-about='" + self.doc.fullUri + "'>");
137                         rdf.prepend(rdfdesc);
138                         rdf.prepend('\n    ');
139                         rdfdesc.append('\n    ');
140                         rdf.append('\n  ');
141                     }
142                     span.appendTo(rdfdesc);
143                     rdfdesc.append('\n    ');
144
145                     self.displayMetaProperty($fg);
146
147                     return false;
148                 });
149
150                 self.$pane.on('click', '.meta-delete', function() {
151                     let $fg = $(this).closest('.form-group');
152                     $('input', $fg).data('edited').remove();
153                     self.displayMetaProperty($fg);
154                     $.wiki.perspectives.VisualPerspective.flush();
155                     return false;
156                 });
157
158                 $('#media-chooser').on('show.bs.modal', function (event) {
159                     var input = $("input", $(event.relatedTarget).parent());
160                     var modal = $(this);
161                     modal.data('target-input', input);
162                     var imglist = modal.find('.modal-body');
163                     imglist.html('');
164                     $.each(self.doc.galleryImages, (i, imgItem) => {
165                         img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
166                             imglist.find('img').removeClass('active');
167                             $(this).addClass('active');
168                         });
169                         imglist.append(img);
170                     });
171                 })
172                 $('#media-chooser .ctrl-ok').on('click', function (event) {
173                     $('#media-chooser').data('target-input')
174                         .val(
175                             (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
176                         ).trigger('change');
177                     $('#media-chooser').modal('hide');
178                 });
179
180                 self.$pane.on('click', '.current-convert', function() {
181                     self.convert($(this).attr('data-to'));
182                 });
183                 self.$pane.on('click', '#current-delete', function() {
184                     self.delete();
185                 });
186         }
187
188         edit(element) {
189             let self = this;
190
191             $("#parents", self.$pane).empty();
192             $("#bubbles").empty();
193
194             let $f = $("#properties-form", self.$pane);
195             $f.empty();
196
197             if (element === null) {
198                 self.$edited = null;
199                 $("h1", self.$pane).text('');
200                 return;
201             }
202
203             let $node = $(element);
204             let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
205             b.data('node', element);
206             $("#bubbles").append(b);
207
208             $node.parents('[x-node]').each(function() {
209                 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
210                 a.data('node', this);
211                 $("#parents", self.$pane).prepend(a)
212
213                 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
214                 b.data('node', this);
215                 $("#bubbles").append(b);
216             })
217
218             // It's a tag.
219             let node = $(element).attr('x-node');
220             $("h1", self.$pane).text(node);
221
222             self.$edited = $(element);
223
224             let nodeDef = elementDefs[node];
225             if (nodeDef && nodeDef.attributes) {
226                 $.each(nodeDef.attributes, function(i, a) {
227                     self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
228                 })
229             }
230
231             // Only utwor can has matadata now.
232             if (node == 'utwor') {
233                 $('<hr>').appendTo($("#properties-form", self.$pane))
234                 META_FIELDS.forEach(function(field) {
235                     let $fg = $("<div class='form-group'>");
236                     $("<label/>").text(field.name).appendTo($fg);
237
238                     // if multiple?
239                     $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
240
241                     let match = field.uri.match(/({(.*)})?(.*)/);
242                     let ns = match[2];
243                     let tag = match[3];
244
245                     let cont = $('<div class="c">');
246
247                     $fg.data('ns', ns);
248                     $fg.data('tag', tag);
249                     $fg.data('field', field);
250                     cont.appendTo($fg);
251
252                     self.displayMetaProperty($fg);
253
254                     $fg.appendTo( $("#properties-form", self.$pane));
255                 });
256             }
257
258             // check node type, find relevant tags
259             if ($node[0].nodeName == 'DIV') {
260                 $("#current-convert").attr("data-current-type", "div");
261             } else if ($node[0].nodeName == 'EM') {
262                 $("#current-convert").attr("data-current-type", "span");
263             }
264         }
265
266         addMetaInput(cont, field, element) {
267             let self = this;
268
269             let ig = $('<div class="input-group">');
270             //ig.data('edited', element);
271             ig.appendTo(cont);
272
273             if (field.value_type.hasLanguage) {
274                 let pp = $("<div class='input-group-prepend'>");
275                 let lang_input = $("<input class='form-control' size='1' class='lang'>");
276                 lang_input.data('edited', $(element));
277                 lang_input.data('edited-attr', 'x-a-xml-lang');
278                 lang_input.val(
279                     $(element).attr('x-a-xml-lang')
280                 );
281                 lang_input.appendTo(pp);
282                 pp.appendTo(ig);
283             }
284
285             let $aninput;
286             if (field.value_type.widget == 'select') {
287                 $aninput = $("<select class='form-control'>");
288                 $.each(field.value_type.options, function() {
289                     $("<option>").text(this).appendTo($aninput);
290                 })
291             } else {
292                 $aninput = $("<input class='form-control'>");
293                 if (field.value_type.autocomplete) {
294                     let autoOptions = field.value_type.autocomplete;
295                     $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
296                         let t = item.label;
297                         if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
298                         if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
299                         return $( "<li>" )
300                             .append( "<div>" + t + "</div>" )
301                             .appendTo( ul );
302                     };
303                 }
304             }
305             $aninput.data('edited', $(element))
306             $aninput.val(
307                 $(element).text()
308             );
309             $aninput.appendTo(ig);
310
311             let ap = $("<div class='input-group-append'>");
312             ap.appendTo(ig);
313             $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
314
315             // lang
316         }
317
318         displayMetaProperty($fg) {
319             let self = this;
320             let ns = $fg.data('ns');
321             let tag = $fg.data('tag');
322             let field = $fg.data('field');
323
324             //  clear container
325             $('.c', $fg).empty();
326
327             let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
328             if (ns) {
329                 selector += "[x-ns='"+ns+"']";
330             }
331             $(selector, self.$edited).each(function() {
332                 self.addMetaInput(
333                     $('.c', $fg),
334                     field,
335                     this);
336             });
337
338             let count = $('.c > .input-group', $fg).length;
339             if (field.required) {
340                 if (!count) {
341                     $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
342                 }
343             }
344         }
345
346         addEditField(defn, value, elem) {
347             let self = this;
348             let $form = $("#properties-form", self.$pane);
349
350             let $fg = $("<div class='form-group'>");
351             $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
352             let $input, $inputCnt;
353             switch (defn.type) {
354             case 'text':
355                 $inputCnt =$input = $("<textarea>");
356                 break;
357             case 'select':
358                 $inputCnt = $input = $("<select>");
359                 $.each(defn.options, function(i, e) {
360                     $("<option>").text(e).appendTo($input);
361                 });
362                 break;
363             case 'bool':
364                 $inputCnt = $input = $("<input type='checkbox'>");
365                 break;
366             case 'media':
367                 $inputCnt = $("<div class='media-input input-group'>");
368                 $input = $("<input type='text'>");
369                 $inputCnt.append($input);
370                 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
371                 break;
372             default:
373                 $inputCnt = $input = $("<input>");
374             }
375
376             $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
377             if ($input.attr('type') == 'checkbox') {
378                 $input.prop('checked', value == 'true');
379             } else {
380                 $input.val(value);
381             }
382
383             if (elem) {
384                 $input.data("edited", elem);
385             }
386             $inputCnt.appendTo($fg);
387
388             $fg.appendTo($form);
389         }
390
391         convert(newtag) {
392             this.$edited.attr('x-node', newtag);
393             // TODO: take care of attributes?
394         }
395
396         delete(newtag) {
397             p = this.$edited.parent();
398             this.$edited.remove();
399             this.edit(p);
400         }
401
402         onEnter(success, failure) {
403             var self = this;
404             super.onEnter();
405
406             if ($.wiki.activePerspective() != 'VisualPerspective')
407                 $.wiki.switchToTab('#VisualPerspective');
408
409             this.enable();
410         }
411
412         enable() {
413             if (this.$edited === null) {
414                 this.edit($('[x-node="utwor"]')[0]);
415             }
416         }
417         disable() {
418             this.edit(null);
419         }
420     }
421     $.wiki.PropertiesPerspective = PropertiesPerspective;
422
423 })(jQuery);