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