fix
[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                     let $ig = $(this).closest('.input-group');
153                     $('input', $ig).data('edited').remove();
154                     self.displayMetaProperty($fg);
155                     $.wiki.perspectives.VisualPerspective.flush();
156                     return false;
157                 });
158
159                 $('#media-chooser').on('show.bs.modal', function (event) {
160                     var input = $("input", $(event.relatedTarget).parent());
161                     var modal = $(this);
162                     modal.data('target-input', input);
163                     var imglist = modal.find('.modal-body');
164                     imglist.html('');
165                     $.each(self.doc.galleryImages, (i, imgItem) => {
166                         img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
167                             imglist.find('img').removeClass('active');
168                             $(this).addClass('active');
169                         });
170                         imglist.append(img);
171                     });
172                 })
173                 $('#media-chooser .ctrl-ok').on('click', function (event) {
174                     $('#media-chooser').data('target-input')
175                         .val(
176                             (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
177                         ).trigger('change');
178                     $('#media-chooser').modal('hide');
179                 });
180
181             /* Meta chooser */
182             $('#meta-chooser').on('show.bs.modal', function (event) {
183                 let input = $("input", $(event.relatedTarget).closest('.input-group'));
184                 let $fg = $(event.relatedTarget).closest('.form-group');
185                 let field = $fg.data('field');
186                 let modal = $(this);
187                 modal.data('target-input', input);
188                 let body = modal.find('.modal-body');
189                 body.html('');
190
191                 let add_options = function(cnt, options, value) {
192                     $.each(options, (i, item) => {
193                         let elem = $('<div class="form-check"><label class="form-check-label"><input class="form-check-input" type="radio" name="metachoose"><div class="value"></div><div class="name"></div><div class="description"></div></label></div>');
194                         if (!item.usable) {
195                             $('input', elem).remove();
196                         }
197                         if (item.hidden) {
198                             $('input', elem).prop('disabled', 'disabled');
199                         }
200                         $('input', elem).val(item.value);
201                         $('input', elem).val(item.value);
202                         $('.value', elem).text(item.value);
203                         $('.name', elem).append(item.name);
204                         $('.description', elem).append(item.description);
205                         let valueMatch = value && value.startsWith(item.value);
206                         if (valueMatch) {
207                             $('label', elem).addClass('text-primary')
208                             if (value == item.value) {
209                                 $('input', elem).prop('checked', true);
210                             }
211                         }
212                         if (item.sub) {
213                             let subT = $('<div class="meta-chooser_toggle">+</div>');
214                             let sub = $('<div>');
215                             elem.append(subT);
216                             elem.append(sub);
217                             subT.on('click', () => {
218                                 sub.toggle()
219                             });
220                             add_options(sub, item.sub, valueMatch ? value : null);
221                         }
222                         elem.appendTo(cnt);
223                     });
224                 };
225
226                 $.ajax({
227                     url: field.value_type.chooser.source,
228                     success: function(data) {
229                         add_options(body, data, input.val());
230                     }
231                 });
232             })
233             $('#meta-chooser .ctrl-ok').on('click', function (event) {
234                 $('#meta-chooser').data('target-input').val(
235                     $('#meta-chooser :checked').val()
236                 ).trigger('change');
237                 $('#meta-chooser').modal('hide');
238             });
239
240                 self.$pane.on('click', '.current-convert', function() {
241                     self.convert($(this).attr('data-to'));
242                 });
243                 self.$pane.on('click', '#current-delete', function() {
244                     self.delete();
245                 });
246         }
247
248         edit(element) {
249             let self = this;
250
251             $("#parents", self.$pane).empty();
252             $("#bubbles").empty();
253
254             let $f = $("#properties-form", self.$pane);
255             $f.empty();
256
257             if (element === null) {
258                 self.$edited = null;
259                 $("h1", self.$pane).text('');
260                 return;
261             }
262
263             let $node = $(element);
264             let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
265             b.data('node', element);
266             $("#bubbles").append(b);
267
268             $node.parents('[x-node]').each(function() {
269                 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
270                 a.data('node', this);
271                 $("#parents", self.$pane).prepend(a)
272
273                 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
274                 b.data('node', this);
275                 $("#bubbles").append(b);
276             })
277
278             // It's a tag.
279             let node = $(element).attr('x-node');
280             $("h1", self.$pane).text(node);
281
282             self.$edited = $(element);
283
284             let nodeDef = elementDefs[node];
285             if (nodeDef && nodeDef.attributes) {
286                 $.each(nodeDef.attributes, function(i, a) {
287                     self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
288                 })
289             }
290
291             // Only utwor can has matadata now.
292             if (node == 'utwor') {
293                 $('<hr>').appendTo($("#properties-form", self.$pane))
294                 META_FIELDS.forEach(function(field) {
295                     let $fg = $("<div class='form-group'>");
296                     $("<label/>").text(field.name).appendTo($fg);
297
298                     // if multiple?
299                     $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
300
301                     let match = field.uri.match(/({(.*)})?(.*)/);
302                     let ns = match[2];
303                     let tag = match[3];
304
305                     let cont = $('<div class="c">');
306
307                     $fg.data('ns', ns);
308                     $fg.data('tag', tag);
309                     $fg.data('field', field);
310                     cont.appendTo($fg);
311
312                     self.displayMetaProperty($fg);
313
314                     $fg.appendTo( $("#properties-form", self.$pane));
315                 });
316             }
317
318             // check node type, find relevant tags
319             if ($node[0].nodeName == 'DIV') {
320                 $("#current-convert").attr("data-current-type", "div");
321             } else if ($node[0].nodeName == 'EM') {
322                 $("#current-convert").attr("data-current-type", "span");
323             }
324         }
325
326         addMetaInput(cont, field, element) {
327             let self = this;
328
329             let ig = $('<div class="input-group">');
330             //ig.data('edited', element);
331             ig.appendTo(cont);
332
333             if (field.value_type.hasLanguage) {
334                 let pp = $("<div class='input-group-prepend'>");
335                 let lang_input = $("<input class='form-control' size='1' class='lang'>");
336                 lang_input.data('edited', $(element));
337                 lang_input.data('edited-attr', 'x-a-xml-lang');
338                 lang_input.val(
339                     $(element).attr('x-a-xml-lang')
340                 );
341                 lang_input.appendTo(pp);
342                 pp.appendTo(ig);
343             }
344
345             let $aninput;
346             if (field.value_type.widget == 'select') {
347                 $aninput = $("<select class='form-control'>");
348                 $.each(field.value_type.options, function() {
349                     $("<option>").text(this).appendTo($aninput);
350                 })
351             } else {
352                 $aninput = $("<input class='form-control'>");
353                 if (field.value_type.autocomplete) {
354                     let autoOptions = field.value_type.autocomplete;
355                     $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
356                         let t = item.label;
357                         if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
358                         if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
359                         return $( "<li>" )
360                             .append( "<div>" + t + "</div>" )
361                             .appendTo( ul );
362                     };
363                 }
364             }
365             $aninput.data('edited', $(element))
366             $aninput.val(
367                 $(element).text()
368             );
369             $aninput.appendTo(ig);
370
371             let ap = $("<div class='input-group-append'>");
372             ap.appendTo(ig);
373
374             if (field.value_type.chooser) {
375                 ap.append($("<button type='button' class='btn btn-outline-secondary' data-toggle='modal' data-target='#meta-chooser'>…</button>"));
376             }
377             $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
378
379             // lang
380         }
381
382         displayMetaProperty($fg) {
383             let self = this;
384             let ns = $fg.data('ns');
385             let tag = $fg.data('tag');
386             let field = $fg.data('field');
387
388             //  clear container
389             $('.c', $fg).empty();
390
391             let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
392             if (ns) {
393                 selector += "[x-ns='"+ns+"']";
394             }
395             $(selector, self.$edited).each(function() {
396                 self.addMetaInput(
397                     $('.c', $fg),
398                     field,
399                     this);
400             });
401
402             let count = $('.c > .input-group', $fg).length;
403             if (field.required) {
404                 if (!count) {
405                     $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
406                 }
407             }
408         }
409
410         addEditField(defn, value, elem) {
411             let self = this;
412             let $form = $("#properties-form", self.$pane);
413
414             let $fg = $("<div class='form-group'>");
415             $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
416             let $input, $inputCnt;
417             switch (defn.type) {
418             case 'text':
419                 $inputCnt =$input = $("<textarea>");
420                 break;
421             case 'select':
422                 $inputCnt = $input = $("<select>");
423                 $.each(defn.options, function(i, e) {
424                     $("<option>").text(e).appendTo($input);
425                 });
426                 break;
427             case 'bool':
428                 $inputCnt = $input = $("<input type='checkbox'>");
429                 break;
430             case 'media':
431                 $inputCnt = $("<div class='media-input input-group'>");
432                 $input = $("<input type='text'>");
433                 $inputCnt.append($input);
434                 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
435                 break;
436             default:
437                 $inputCnt = $input = $("<input>");
438             }
439
440             $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
441             if ($input.attr('type') == 'checkbox') {
442                 $input.prop('checked', value == 'true');
443             } else {
444                 $input.val(value);
445             }
446
447             if (elem) {
448                 $input.data("edited", elem);
449             }
450             $inputCnt.appendTo($fg);
451
452             $fg.appendTo($form);
453         }
454
455         convert(newtag) {
456             this.$edited.attr('x-node', newtag);
457             // TODO: take care of attributes?
458         }
459
460         delete(newtag) {
461             p = this.$edited.parent();
462             this.$edited.remove();
463             this.edit(p);
464         }
465
466         onEnter(success, failure) {
467             var self = this;
468             super.onEnter();
469
470             if ($.wiki.activePerspective() != 'VisualPerspective')
471                 $.wiki.switchToTab('#VisualPerspective');
472
473             this.enable();
474         }
475
476         enable() {
477             if (this.$edited === null) {
478                 this.edit($('[x-node="utwor"]')[0]);
479             }
480         }
481         disable() {
482             this.edit(null);
483         }
484     }
485     $.wiki.PropertiesPerspective = PropertiesPerspective;
486
487 })(jQuery);