a7caf10a403771660bdf3e9ea4ba4d6fea130669
[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                 },
12                 {
13                     "name": "alt",
14                     "type": "text",
15                 },
16                 {
17                     "name": "szer",
18                     "type": "percent",
19                 },
20                 {
21                     "name": "wyrownanie",
22                     "type": "select",
23                     "options": ["lewo", "srodek", "prawo"],
24                 },
25                 {
26                     "name": "oblew",
27                     "type": "bool",
28                 },
29             ],
30         },
31         "ref": {
32             "attributes": [
33                 {
34                     "name": "href",
35                 },
36             ],
37         }
38     };
39
40     function PropertiesPerspective(options) {
41         let oldCallback = options.callback || function() {};
42         this.vsplitbar = 'WŁAŚCIWOŚCI';
43
44         options.callback = function() {
45             let self = this;
46
47             self.$pane = $("#side-properties");
48             
49             $("#simple-editor").on('click', '[x-node]', function(e) {
50                 if (!e.redakcja_edited) {
51                     e.redakcja_edited = true;
52                     self.edit(this);
53                 }
54             });
55
56             self.$pane.on('click', '#parents li', function(e) {
57                 self.edit($(this).data('node'));
58             });
59
60             $(document).on('click', '#bubbles .badge', function(e) {
61                 self.edit($(this).data('node'));
62             });
63
64             self.$pane.on('change', '.form-control', function() {
65                 let $input = $(this);
66
67                 let inputval;
68                 if ($input.attr('type') == 'checkbox') {
69                     inputval = $input.is(':checked');
70                 } else {
71                     inputval = $input.val();
72                 }
73                 
74                 if ($input.data("edited")) {
75                     if ($input.data("edited-attr")) {
76                         $input.data("edited").attr($input.data("edited-attr"), inputval);
77                     } else {
78                         $input.data("edited").text(inputval);
79                     }
80                     return;
81                 }
82                 
83                 html2text({
84                     element: self.$edited[0],
85                     success: function(xml) {
86                         w(222)
87                         let $xmlelem = $($.parseXML(xml));
88                         w(333, $xmlelem)
89                         w($input.data('property'), $input.val());
90                         $xmlelem.contents().attr($input.data('property'), inputval);
91                         w(444, $xmlelem)
92                         let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
93                         w(555, newxml)
94                         xml2html({
95                             xml: newxml,
96                             base: self.doc.getBase(),
97                             success: function(html) {
98                                 let htmlElem = $(html);
99                                 self.$edited.replaceWith(htmlElem);
100                                 self.edit(htmlElem);
101                             }
102                         });
103                     },
104                     error: function(e) {console.log(e);},
105                 });
106                 self.$edited;
107             });
108
109             
110             self.$pane.on('click', '.meta-add', function() {
111                 // create a metadata item
112                 let $fg = $(this).parent();
113                 let ns = $fg.data('ns');
114                 let tag = $fg.data('tag');
115                 let field = $fg.data('field');
116                 let span = $('<span/>');
117                 span.attr('x-node', tag);
118                 span.attr('x-ns', ns)
119                 if (field.value_type.hasLanguage) {
120                     span.attr('x-a-xml-lang', 'pl');
121                 }
122                 span.appendTo(
123                     $("> [x-node='RDF'] > [x-node='Description']", self.$edited)
124                 );
125
126                 self.displayMetaProperty($fg);
127                 
128                 return false;
129             });
130             
131             self.$pane.on('click', '.meta-delete', function() {
132                 let $fg = $(this).closest('.form-group'); 
133                 $('input', $fg).data('edited').remove();
134                 self.displayMetaProperty($fg);
135                 return false;
136             });
137
138
139             
140             self.$pane.on('click', '.current-convert', function() {
141                 self.convert($(this).attr('data-to'));
142             });
143             self.$pane.on('click', '#current-delete', function() {
144                 self.delete();
145             });
146             
147             
148             oldCallback.call(this);
149         };
150
151         $.wiki.SidebarPerspective.call(this, options);
152     }
153
154     PropertiesPerspective.prototype = new $.wiki.SidebarPerspective();
155
156     PropertiesPerspective.prototype.edit = function(element) {
157         let self = this;
158
159         let $node = $(element);
160         $("#parents", self.$pane).empty();
161         $("#bubbles").empty();
162
163         let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
164         b.data('node', element);
165         $("#bubbles").append(b);
166
167         $node.parents('[x-node]').each(function() {
168             let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
169             a.data('node', this);
170             $("#parents", self.$pane).prepend(a)
171
172             let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
173             b.data('node', this);
174             $("#bubbles").append(b);
175         })
176
177         // It's a tag.
178         node = $(element).attr('x-node');
179         $("h1", self.$pane).text(node);
180
181         $f = $("#properties-form", self.$pane);
182         $f.empty();
183         self.$edited = $(element);
184
185         let nodeDef = elementDefs[node];
186         if (nodeDef && nodeDef.attributes) {
187             $.each(nodeDef.attributes, function(i, a) {
188                 self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
189             })
190         }
191
192         // Only utwor can has matadata now.
193         if (node == 'utwor') {
194             $('<hr>').appendTo($("#properties-form", self.$pane))
195             META_FIELDS.forEach(function(field) {
196                 let $fg = $("<div class='form-group'>");
197                 $("<label/>").text(field.name).appendTo($fg);
198
199                 // if multiple?
200                 $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
201
202                 let match = field.uri.match(/({(.*)})?(.*)/);
203                 ns = match[2];
204                 tag = match[3];
205
206                 let cont = $('<div class="c">');
207
208                 $fg.data('ns', ns);
209                 $fg.data('tag', tag);
210                 $fg.data('field', field);
211                 cont.appendTo($fg);
212
213                 self.displayMetaProperty($fg);
214
215                 $fg.appendTo( $("#properties-form", self.$pane));
216             });
217         }
218
219         // check node type, find relevant tags
220         if ($node[0].nodeName == 'DIV') {
221             $("#current-convert").attr("data-current-type", "div");
222         } else if ($node[0].nodeName == 'EM') {
223             $("#current-convert").attr("data-current-type", "span");
224         }
225     };
226
227     PropertiesPerspective.prototype.addMetaInput = function(cont, field, element) {
228         let self = this;
229
230         let ig = $('<div class="input-group">');
231         //ig.data('edited', element);
232         ig.appendTo(cont);
233
234         if (field.value_type.hasLanguage) {
235             let pp = $("<div class='input-group-prepend'>");
236             let lang_input = $("<input class='form-control' size='1' class='lang'>");
237             lang_input.data('edited', $(element));
238             lang_input.data('edited-attr', 'x-a-xml-lang');
239             lang_input.val(
240                 $(element).attr('x-a-xml-lang')
241             );
242             lang_input.appendTo(pp);
243             pp.appendTo(ig);
244         }
245
246         let $aninput = $("<input class='form-control'>");
247         $aninput.data('edited', $(element))
248         $aninput.val(
249             $(element).text()
250         );
251         $aninput.appendTo(ig);
252
253         let ap = $("<div class='input-group-append'>");
254         ap.appendTo(ig);
255         $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
256         
257         // lang
258     };
259     
260
261     PropertiesPerspective.prototype.displayMetaProperty = function($fg) {
262         let self = this;
263         let ns = $fg.data('ns');
264         let tag = $fg.data('tag');
265         let field = $fg.data('field');
266
267         //  clear container
268         $('.c', $fg).empty();
269
270         let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
271         if (ns) {
272             selector += "[x-ns='"+ns+"']";
273         }
274         $(selector, self.$edited).each(function() {
275             self.addMetaInput(
276                 $('.c', $fg),
277                 field,
278                 this);
279         });
280
281         count = $('.c > .input-group', $fg).length;
282         if (field.required) {
283             if (!count) {
284                 $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
285             }
286         }
287     };
288     
289
290
291     PropertiesPerspective.prototype.addEditField = function(defn, value, elem) {
292         let self = this;
293         let $form = $("#properties-form", self.$pane);
294
295         let $fg = $("<div class='form-group'>");
296         $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
297         let $input;
298         switch (defn.type) {
299         case 'text':
300             $input = $("<textarea>");
301             break;
302         case 'select':
303             $input = $("<select>");
304             $.each(defn.options, function(i, e) {
305                 $("<option>").text(e).appendTo($input);
306             });
307             break;
308         case 'bool':
309             $input = $("<input type='checkbox'>");
310             break;
311         default:
312             $input = $("<input>");
313         }
314
315         $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
316         if ($input.attr('type') == 'checkbox') {
317             $input.prop('checked', value == 'true');
318         } else {
319             $input.val(value);
320         }
321         
322         if (elem) {
323             $input.data("edited", elem);
324         }
325         $input.appendTo($fg);
326
327         $fg.appendTo($form);
328     }
329
330     PropertiesPerspective.prototype.convert = function(newtag) {
331         this.$edited.attr('x-node', newtag);
332         // TODO: take care of attributes?
333     }
334
335     PropertiesPerspective.prototype.delete = function(newtag) {
336         p = this.$edited.parent();
337         this.$edited.remove();
338         this.edit(p);
339     }
340
341     $.wiki.PropertiesPerspective = PropertiesPerspective;
342
343 })(jQuery);
344