24 "options": ["lewo", "srodek", "prawo"],
38 "tab": {"attributes": [{"name": "szer"}]},
39 "naglowek_parsza": {"attributes": [{"name": "nazwa"}]},
40 "naglowek_czytanie": {"attributes": [{"name": "nazwa"}]}
44 class PropertiesPerspective extends $.wiki.SidebarPerspective {
45 vsplitbar = 'WŁAŚCIWOŚCI';
48 constructor(options) {
52 self.$pane = $("#side-properties");
54 $("#simple-editor").on('click', '[x-node]', function(e) {
55 if (!e.redakcja_edited) {
56 e.redakcja_edited = true;
61 self.$pane.on('click', '#parents li', function(e) {
62 self.edit($(this).data('node'));
65 $(document).on('click', '#bubbles .badge', function(e) {
66 self.edit($(this).data('node'));
69 self.$pane.on('change', '.form-control', function() {
73 if ($input.attr('type') == 'checkbox') {
74 inputval = $input.is(':checked');
76 inputval = $input.val();
79 if ($input.data("edited")) {
80 if ($input.data("edited-attr")) {
81 $input.data("edited").attr($input.data("edited-attr"), inputval);
83 $input.data("edited").text(inputval);
85 $.wiki.perspectives.VisualPerspective.flush();
90 element: self.$edited[0],
91 success: function(xml) {
93 let $xmlelem = $($.parseXML(xml));
95 w($input.data('property'), $input.val());
96 $xmlelem.contents().attr($input.data('property'), inputval);
98 let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
102 base: self.doc.getBase(),
103 success: function(html) {
104 let htmlElem = $(html);
105 self.$edited.replaceWith(htmlElem);
107 $.wiki.activePerspective().flush();
111 error: function(e) {console.log(e);},
117 self.$pane.on('click', '.meta-add', function() {
118 // create a metadata item
119 let $fg = $(this).parent();
120 let ns = $fg.data('ns');
121 let tag = $fg.data('tag');
122 let field = $fg.data('field');
123 let span = $('<span/>');
124 span.attr('x-node', tag);
125 span.attr('x-ns', ns)
126 if (field.value_type.hasLanguage) {
127 span.attr('x-a-xml-lang', 'pl');
130 let rdf = $("> [x-node='RDF']", self.$edited);
132 rdf = $("<span x-node='RDF' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>");
133 self.$edited.prepend(rdf);
134 self.$edited.prepend('\n ');
137 let rdfdesc = $("> [x-node='Description']", rdf);
138 if (!rdfdesc.length) {
139 rdfdesc = $("<span x-node='Description' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#' x-a-rdf-about='" + self.doc.fullUri + "'>");
140 rdf.prepend(rdfdesc);
142 rdfdesc.append('\n ');
145 if (field.filter && field.filter.startswith) {
146 span.text(field.filter.startswith[0]);
148 span.appendTo(rdfdesc);
149 rdfdesc.append('\n ');
151 self.displayMetaProperty($fg);
156 self.$pane.on('click', '.meta-delete', function() {
157 let $fg = $(this).closest('.form-group');
158 let $ig = $(this).closest('.input-group');
159 $('input', $ig).data('edited').remove();
160 self.displayMetaProperty($fg);
161 $.wiki.perspectives.VisualPerspective.flush();
165 $('#media-chooser').on('show.bs.modal', function (event) {
166 var input = $("input", $(event.relatedTarget).parent());
168 modal.data('target-input', input);
169 var imglist = modal.find('.modal-body');
172 self.doc.refreshImageGallery({
173 success: function(galleryImages) {
174 $.each(self.doc.galleryImages, (i, imgItem) => {
175 let img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
176 imglist.find('img').removeClass('active');
177 $(this).addClass('active');
184 $('#media-chooser .ctrl-ok').on('click', function (event) {
185 $('#media-chooser').data('target-input')
187 (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
189 $('#media-chooser').modal('hide');
193 $('#meta-chooser').on('show.bs.modal', function (event) {
194 let input = $("input", $(event.relatedTarget).closest('.input-group'));
195 let $fg = $(event.relatedTarget).closest('.form-group');
196 let field = $fg.data('field');
198 modal.data('target-input', input);
199 let body = modal.find('.modal-body');
202 let add_options = function(cnt, options, value) {
203 $.each(options, (i, item) => {
204 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>');
206 $('input', elem).remove();
209 $('input', elem).prop('disabled', 'disabled');
211 $('input', elem).val(item.value);
212 $('input', elem).val(item.value);
213 $('.value', elem).text(item.value);
214 $('.name', elem).append(item.name);
215 $('.description', elem).append(item.description);
216 let valueMatch = value && value.startsWith(item.value);
218 $('label', elem).addClass('text-primary')
219 if (value == item.value) {
220 $('input', elem).prop('checked', true);
224 let subT = $('<div class="meta-chooser_toggle">+</div>');
225 let sub = $('<div>');
228 subT.on('click', () => {
231 add_options(sub, item.sub, valueMatch ? value : null);
238 url: field.value_type.chooser.source,
239 data: field.filter ? {'filter': JSON.stringify(field.filter)} : '',
240 success: function(data) {
241 add_options(body, data, input.val());
245 $('#meta-chooser .ctrl-ok').on('click', function (event) {
246 $('#meta-chooser').data('target-input').val(
247 $('#meta-chooser :checked').val()
249 $('#meta-chooser').modal('hide');
252 self.$pane.on('click', '.current-convert', function() {
253 self.convert($(this).attr('data-to'));
255 self.$pane.on('click', '#current-delete', function() {
263 $("#parents", self.$pane).empty();
264 $("#bubbles").empty();
266 let $f = $("#properties-form", self.$pane);
269 if (element === null) {
271 $("h1", self.$pane).text('');
275 let $node = $(element);
276 let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
277 b.data('node', element);
278 $("#bubbles").append(b);
280 $node.parents('[x-node]').each(function() {
281 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
282 a.data('node', this);
283 $("#parents", self.$pane).prepend(a)
285 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
286 b.data('node', this);
287 $("#bubbles").append(b);
291 let node = $(element).attr('x-node');
292 $("h1", self.$pane).text(node);
294 self.$edited = $(element);
296 let nodeDef = elementDefs[node];
297 if (nodeDef && nodeDef.attributes) {
298 $.each(nodeDef.attributes, function(i, a) {
299 self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
303 // Only utwor can has matadata now.
304 if (node == 'utwor') {
305 $('<hr>').appendTo($("#properties-form", self.$pane))
306 META_FIELDS.forEach(function(field) {
307 let $fg = $("<div class='form-group'>");
308 $("<label/>").text(field.name).appendTo($fg);
311 $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
313 let match = field.uri.match(/({(.*)})?(.*)/);
317 let cont = $('<div class="c">');
320 $fg.data('tag', tag);
321 $fg.data('field', field);
324 self.displayMetaProperty($fg);
326 $fg.appendTo( $("#properties-form", self.$pane));
330 // check node type, find relevant tags
331 if ($node[0].nodeName == 'DIV') {
332 $("#current-convert").attr("data-current-type", "div");
333 } else if ($node[0].nodeName == 'EM') {
334 $("#current-convert").attr("data-current-type", "span");
338 addMetaInput(cont, field, element) {
341 let ig = $('<div class="input-group">');
342 //ig.data('edited', element);
345 if (field.value_type.hasLanguage) {
346 let pp = $("<div class='input-group-prepend'>");
347 let lang_input = $("<input class='form-control' size='1' class='lang'>");
348 lang_input.data('edited', $(element));
349 lang_input.data('edited-attr', 'x-a-xml-lang');
351 $(element).attr('x-a-xml-lang')
353 lang_input.appendTo(pp);
358 if (field.value_type.widget == 'select') {
359 $aninput = $("<select class='form-control'>");
360 $.each(field.value_type.options, function() {
361 $("<option>").text(this).appendTo($aninput);
364 $aninput = $("<input class='form-control'>");
365 if (field.value_type.autocomplete) {
366 let autoOptions = field.value_type.autocomplete;
367 $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
369 if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
370 if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
372 .append( "<div>" + t + "</div>" )
377 $aninput.data('edited', $(element))
381 $aninput.appendTo(ig);
383 let ap = $("<div class='input-group-append'>");
386 if (field.value_type.chooser) {
387 ap.append($("<button type='button' class='btn btn-outline-secondary' data-toggle='modal' data-target='#meta-chooser'>…</button>"));
389 $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
394 applyFilter(filter, text) {
396 return !this.applyFilter(filter.not, text)
397 } else if (filter.startswith) {
398 for (prefix of filter.startswith) {
399 if (text.startsWith(prefix))
406 displayMetaProperty($fg) {
408 let ns = $fg.data('ns');
409 let tag = $fg.data('tag');
410 let field = $fg.data('field');
413 $('.c', $fg).empty();
415 let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
417 selector += "[x-ns='"+ns+"']";
419 $(selector, self.$edited).each(function() {
421 let text = $(this).text();
422 if (!self.applyFilter(field.filter, text))
431 let count = $('.c > .input-group', $fg).length;
432 if (field.required) {
434 $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
439 addEditField(defn, value, elem) {
441 let $form = $("#properties-form", self.$pane);
443 let $fg = $("<div class='form-group'>");
444 $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
445 let $input, $inputCnt;
448 $inputCnt =$input = $("<textarea>");
451 $inputCnt = $input = $("<select>");
452 $.each(defn.options, function(i, e) {
453 $("<option>").text(e).appendTo($input);
457 $inputCnt = $input = $("<input type='checkbox'>");
460 $inputCnt = $("<div class='media-input input-group'>");
461 $input = $("<input type='text'>");
462 $inputCnt.append($input);
463 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
466 $inputCnt = $input = $("<input>");
469 $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
470 if ($input.attr('type') == 'checkbox') {
471 $input.prop('checked', value == 'true');
477 $input.data("edited", elem);
479 $inputCnt.appendTo($fg);
485 this.$edited.attr('x-node', newtag);
486 // TODO: take care of attributes?
490 p = this.$edited.parent();
491 this.$edited.remove();
495 onEnter(success, failure) {
499 if ($.wiki.activePerspective() != 'VisualPerspective')
500 $.wiki.switchToTab('#VisualPerspective');
506 if (this.$edited === null) {
507 this.edit($('[x-node="utwor"]')[0]);
514 $.wiki.PropertiesPerspective = PropertiesPerspective;