24 "options": ["lewo", "srodek", "prawo"],
41 class PropertiesPerspective extends $.wiki.SidebarPerspective {
42 vsplitbar = 'WŁAŚCIWOŚCI';
45 constructor(options) {
49 self.$pane = $("#side-properties");
51 $("#simple-editor").on('click', '[x-node]', function(e) {
52 if (!e.redakcja_edited) {
53 e.redakcja_edited = true;
58 self.$pane.on('click', '#parents li', function(e) {
59 self.edit($(this).data('node'));
62 $(document).on('click', '#bubbles .badge', function(e) {
63 self.edit($(this).data('node'));
66 self.$pane.on('change', '.form-control', function() {
70 if ($input.attr('type') == 'checkbox') {
71 inputval = $input.is(':checked');
73 inputval = $input.val();
76 if ($input.data("edited")) {
77 if ($input.data("edited-attr")) {
78 $input.data("edited").attr($input.data("edited-attr"), inputval);
80 $input.data("edited").text(inputval);
82 $.wiki.perspectives.VisualPerspective.flush();
87 element: self.$edited[0],
88 success: function(xml) {
90 let $xmlelem = $($.parseXML(xml));
92 w($input.data('property'), $input.val());
93 $xmlelem.contents().attr($input.data('property'), inputval);
95 let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
99 base: self.doc.getBase(),
100 success: function(html) {
101 let htmlElem = $(html);
102 self.$edited.replaceWith(htmlElem);
104 $.wiki.activePerspective().flush();
108 error: function(e) {console.log(e);},
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');
127 let rdf = $("> [x-node='RDF']", self.$edited);
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 ');
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);
139 rdfdesc.append('\n ');
142 if (field.filter && field.filter.startswith) {
143 span.text(field.filter.startswith[0]);
145 span.appendTo(rdfdesc);
146 rdfdesc.append('\n ');
148 self.displayMetaProperty($fg);
153 self.$pane.on('click', '.meta-delete', function() {
154 let $fg = $(this).closest('.form-group');
155 let $ig = $(this).closest('.input-group');
156 $('input', $ig).data('edited').remove();
157 self.displayMetaProperty($fg);
158 $.wiki.perspectives.VisualPerspective.flush();
162 $('#media-chooser').on('show.bs.modal', function (event) {
163 var input = $("input", $(event.relatedTarget).parent());
165 modal.data('target-input', input);
166 var imglist = modal.find('.modal-body');
169 self.doc.refreshImageGallery({
170 success: function(galleryImages) {
171 $.each(self.doc.galleryImages, (i, imgItem) => {
172 let img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
173 imglist.find('img').removeClass('active');
174 $(this).addClass('active');
181 $('#media-chooser .ctrl-ok').on('click', function (event) {
182 $('#media-chooser').data('target-input')
184 (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
186 $('#media-chooser').modal('hide');
190 $('#meta-chooser').on('show.bs.modal', function (event) {
191 let input = $("input", $(event.relatedTarget).closest('.input-group'));
192 let $fg = $(event.relatedTarget).closest('.form-group');
193 let field = $fg.data('field');
195 modal.data('target-input', input);
196 let body = modal.find('.modal-body');
199 let add_options = function(cnt, options, value) {
200 $.each(options, (i, item) => {
201 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>');
203 $('input', elem).remove();
206 $('input', elem).prop('disabled', 'disabled');
208 $('input', elem).val(item.value);
209 $('input', elem).val(item.value);
210 $('.value', elem).text(item.value);
211 $('.name', elem).append(item.name);
212 $('.description', elem).append(item.description);
213 let valueMatch = value && value.startsWith(item.value);
215 $('label', elem).addClass('text-primary')
216 if (value == item.value) {
217 $('input', elem).prop('checked', true);
221 let subT = $('<div class="meta-chooser_toggle">+</div>');
222 let sub = $('<div>');
225 subT.on('click', () => {
228 add_options(sub, item.sub, valueMatch ? value : null);
235 url: field.value_type.chooser.source,
236 data: field.filter ? {'filter': JSON.stringify(field.filter)} : '',
237 success: function(data) {
238 add_options(body, data, input.val());
242 $('#meta-chooser .ctrl-ok').on('click', function (event) {
243 $('#meta-chooser').data('target-input').val(
244 $('#meta-chooser :checked').val()
246 $('#meta-chooser').modal('hide');
249 self.$pane.on('click', '.current-convert', function() {
250 self.convert($(this).attr('data-to'));
252 self.$pane.on('click', '#current-delete', function() {
260 $("#parents", self.$pane).empty();
261 $("#bubbles").empty();
263 let $f = $("#properties-form", self.$pane);
266 if (element === null) {
268 $("h1", self.$pane).text('');
272 let $node = $(element);
273 let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
274 b.data('node', element);
275 $("#bubbles").append(b);
277 $node.parents('[x-node]').each(function() {
278 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
279 a.data('node', this);
280 $("#parents", self.$pane).prepend(a)
282 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
283 b.data('node', this);
284 $("#bubbles").append(b);
288 let node = $(element).attr('x-node');
289 $("h1", self.$pane).text(node);
291 self.$edited = $(element);
293 let nodeDef = elementDefs[node];
294 if (nodeDef && nodeDef.attributes) {
295 $.each(nodeDef.attributes, function(i, a) {
296 self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
300 // Only utwor can has matadata now.
301 if (node == 'utwor') {
302 $('<hr>').appendTo($("#properties-form", self.$pane))
303 META_FIELDS.forEach(function(field) {
304 let $fg = $("<div class='form-group'>");
305 $("<label/>").text(field.name).appendTo($fg);
308 $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
310 let match = field.uri.match(/({(.*)})?(.*)/);
314 let cont = $('<div class="c">');
317 $fg.data('tag', tag);
318 $fg.data('field', field);
321 self.displayMetaProperty($fg);
323 $fg.appendTo( $("#properties-form", self.$pane));
327 // check node type, find relevant tags
328 if ($node[0].nodeName == 'DIV') {
329 $("#current-convert").attr("data-current-type", "div");
330 } else if ($node[0].nodeName == 'EM') {
331 $("#current-convert").attr("data-current-type", "span");
335 addMetaInput(cont, field, element) {
338 let ig = $('<div class="input-group">');
339 //ig.data('edited', element);
342 if (field.value_type.hasLanguage) {
343 let pp = $("<div class='input-group-prepend'>");
344 let lang_input = $("<input class='form-control' size='1' class='lang'>");
345 lang_input.data('edited', $(element));
346 lang_input.data('edited-attr', 'x-a-xml-lang');
348 $(element).attr('x-a-xml-lang')
350 lang_input.appendTo(pp);
355 if (field.value_type.widget == 'select') {
356 $aninput = $("<select class='form-control'>");
357 $.each(field.value_type.options, function() {
358 $("<option>").text(this).appendTo($aninput);
361 $aninput = $("<input class='form-control'>");
362 if (field.value_type.autocomplete) {
363 let autoOptions = field.value_type.autocomplete;
364 $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
366 if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
367 if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
369 .append( "<div>" + t + "</div>" )
374 $aninput.data('edited', $(element))
378 $aninput.appendTo(ig);
380 let ap = $("<div class='input-group-append'>");
383 if (field.value_type.chooser) {
384 ap.append($("<button type='button' class='btn btn-outline-secondary' data-toggle='modal' data-target='#meta-chooser'>…</button>"));
386 $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
391 applyFilter(filter, text) {
393 return !this.applyFilter(filter.not, text)
394 } else if (filter.startswith) {
395 for (prefix of filter.startswith) {
396 if (text.startsWith(prefix))
403 displayMetaProperty($fg) {
405 let ns = $fg.data('ns');
406 let tag = $fg.data('tag');
407 let field = $fg.data('field');
410 $('.c', $fg).empty();
412 let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
414 selector += "[x-ns='"+ns+"']";
416 $(selector, self.$edited).each(function() {
418 let text = $(this).text();
419 if (!self.applyFilter(field.filter, text))
428 let count = $('.c > .input-group', $fg).length;
429 if (field.required) {
431 $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
436 addEditField(defn, value, elem) {
438 let $form = $("#properties-form", self.$pane);
440 let $fg = $("<div class='form-group'>");
441 $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
442 let $input, $inputCnt;
445 $inputCnt =$input = $("<textarea>");
448 $inputCnt = $input = $("<select>");
449 $.each(defn.options, function(i, e) {
450 $("<option>").text(e).appendTo($input);
454 $inputCnt = $input = $("<input type='checkbox'>");
457 $inputCnt = $("<div class='media-input input-group'>");
458 $input = $("<input type='text'>");
459 $inputCnt.append($input);
460 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
463 $inputCnt = $input = $("<input>");
466 $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
467 if ($input.attr('type') == 'checkbox') {
468 $input.prop('checked', value == 'true');
474 $input.data("edited", elem);
476 $inputCnt.appendTo($fg);
482 this.$edited.attr('x-node', newtag);
483 // TODO: take care of attributes?
487 p = this.$edited.parent();
488 this.$edited.remove();
492 onEnter(success, failure) {
496 if ($.wiki.activePerspective() != 'VisualPerspective')
497 $.wiki.switchToTab('#VisualPerspective');
503 if (this.$edited === null) {
504 this.edit($('[x-node="utwor"]')[0]);
511 $.wiki.PropertiesPerspective = PropertiesPerspective;