21 "options": ["lewo", "srodek", "prawo"],
35 "tab": {"attributes": [{"name": "szer"}]},
36 "naglowek_parsza": {"attributes": [{"name": "nazwa"}]},
37 "naglowek_czytanie": {"attributes": [{"name": "nazwa"}]}
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) {
89 let $xmlelem = $($.parseXML(xml));
90 $xmlelem.contents().attr($input.data('property'), inputval);
91 let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
94 success: function(html) {
95 let htmlElem = $(html);
96 self.$edited.replaceWith(htmlElem);
98 $.wiki.activePerspective().flush();
102 error: function(e) {console.log(e);},
108 self.$pane.on('click', '.meta-add', function() {
109 // create a metadata item
110 let $fg = $(this).parent();
111 let ns = $fg.data('ns');
112 let tag = $fg.data('tag');
113 let field = $fg.data('field');
114 let span = $('<span/>');
115 span.attr('x-node', tag);
116 span.attr('x-ns', ns)
117 if (field.value_type.hasLanguage) {
118 span.attr('x-a-xml-lang', 'pl');
121 let rdf = $("> [x-node='RDF']", self.$edited);
123 rdf = $("<span x-node='RDF' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>");
124 self.$edited.prepend(rdf);
125 self.$edited.prepend('\n ');
128 let rdfdesc = $("> [x-node='Description']", rdf);
129 if (!rdfdesc.length) {
130 rdfdesc = $("<span x-node='Description' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#' x-a-rdf-about='" + self.doc.fullUri + "'>");
131 rdf.prepend(rdfdesc);
133 rdfdesc.append('\n ');
136 if (field.filter && field.filter.startswith) {
137 span.text(field.filter.startswith[0]);
139 span.appendTo(rdfdesc);
140 rdfdesc.append('\n ');
142 self.displayMetaProperty($fg);
147 self.$pane.on('click', '.meta-delete', function() {
148 let $fg = $(this).closest('.form-group');
149 let $ig = $(this).closest('.input-group');
150 $('input', $ig).data('edited').remove();
151 self.displayMetaProperty($fg);
152 $.wiki.perspectives.VisualPerspective.flush();
156 $('#media-chooser').on('show.bs.modal', function (event) {
157 var input = $("input", $(event.relatedTarget).parent());
159 modal.data('target-input', input);
160 var imglist = modal.find('.modal-body');
163 self.doc.refreshImageGallery({
164 success: function(galleryImages) {
165 $.each(galleryImages, (i, imgItem) => {
166 let 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');
175 $('#media-chooser .ctrl-ok').on('click', function (event) {
176 $('#media-chooser').data('target-input')
178 (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
180 $('#media-chooser').modal('hide');
184 $('#meta-chooser').on('show.bs.modal', function (event) {
185 let input = $("input", $(event.relatedTarget).closest('.input-group'));
186 let $fg = $(event.relatedTarget).closest('.form-group');
187 let field = $fg.data('field');
189 modal.data('target-input', input);
190 let body = modal.find('.modal-body');
193 let add_options = function(cnt, options, value) {
194 $.each(options, (i, item) => {
195 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>');
197 $('input', elem).remove();
200 $('input', elem).prop('disabled', 'disabled');
202 $('input', elem).val(item.value);
203 $('input', elem).val(item.value);
204 $('.value', elem).text(item.value);
205 $('.name', elem).append(item.name);
206 $('.description', elem).append(item.description);
207 let valueMatch = value && value.startsWith(item.value);
209 $('label', elem).addClass('text-primary')
210 if (value == item.value) {
211 $('input', elem).prop('checked', true);
215 let subT = $('<div class="meta-chooser_toggle">+</div>');
216 let sub = $('<div>');
219 subT.on('click', () => {
222 add_options(sub, item.sub, valueMatch ? value : null);
229 url: field.value_type.chooser.source,
230 data: field.filter ? {'filter': JSON.stringify(field.filter)} : '',
231 success: function(data) {
232 add_options(body, data, input.val());
236 $('#meta-chooser .ctrl-ok').on('click', function (event) {
237 $('#meta-chooser').data('target-input').val(
238 $('#meta-chooser :checked').val()
240 $('#meta-chooser').modal('hide');
243 self.$pane.on('click', '.current-convert', function() {
244 self.convert($(this).attr('data-to'));
246 self.$pane.on('click', '#current-delete', function() {
254 $("#parents", self.$pane).empty();
255 $("#bubbles").empty();
257 let $f = $("#properties-form", self.$pane);
260 if (element === null) {
262 $("h1", self.$pane).text('');
266 let $node = $(element);
267 let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
268 b.data('node', element);
269 $("#bubbles").append(b);
271 $node.parents('[x-node]').each(function() {
272 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
273 a.data('node', this);
274 $("#parents", self.$pane).prepend(a)
276 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
277 b.data('node', this);
278 $("#bubbles").append(b);
282 let node = $(element).attr('x-node');
283 $("h1", self.$pane).text(node);
285 self.$edited = $(element);
287 let nodeDef = elementDefs[node];
288 if (nodeDef && nodeDef.attributes) {
289 $.each(nodeDef.attributes, function(i, a) {
290 self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
294 // Only utwor can has matadata now.
295 if (node == 'utwor') {
296 $('<hr>').appendTo($("#properties-form", self.$pane))
297 META_FIELDS.forEach(function(field) {
298 let $fg = $("<div class='form-group'>");
299 $("<label/>").text(field.name).appendTo($fg);
302 $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
304 let match = field.uri.match(/({(.*)})?(.*)/);
308 let cont = $('<div class="c">');
311 $fg.data('tag', tag);
312 $fg.data('field', field);
315 self.displayMetaProperty($fg);
317 $fg.appendTo( $("#properties-form", self.$pane));
321 // check node type, find relevant tags
322 if ($node[0].nodeName == 'DIV') {
323 $("#current-convert").attr("data-current-type", "div");
324 } else if ($node[0].nodeName == 'EM') {
325 $("#current-convert").attr("data-current-type", "span");
329 addMetaInput(cont, field, element) {
332 let ig = $('<div class="input-group">');
333 //ig.data('edited', element);
336 if (field.value_type.hasLanguage) {
337 let pp = $("<div class='input-group-prepend'>");
338 let lang_input = $("<input class='form-control' size='1' class='lang'>");
339 lang_input.data('edited', $(element));
340 lang_input.data('edited-attr', 'x-a-xml-lang');
342 $(element).attr('x-a-xml-lang')
344 lang_input.appendTo(pp);
349 if (field.value_type.widget == 'select') {
350 $aninput = $("<select class='form-control'>");
351 $.each(field.value_type.options, function() {
352 $("<option>").text(this).appendTo($aninput);
355 $aninput = $("<input class='form-control'>");
356 if (field.value_type.autocomplete) {
357 let autoOptions = field.value_type.autocomplete;
358 $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
360 if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
361 if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
363 .append( "<div>" + t + "</div>" )
368 $aninput.data('edited', $(element))
372 $aninput.appendTo(ig);
374 let ap = $("<div class='input-group-append'>");
377 if (field.value_type.chooser) {
378 ap.append($("<button type='button' class='btn btn-outline-secondary' data-toggle='modal' data-target='#meta-chooser'>…</button>"));
380 $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
385 applyFilter(filter, text) {
387 return !this.applyFilter(filter.not, text)
388 } else if (filter.startswith) {
389 for (prefix of filter.startswith) {
390 if (text.startsWith(prefix))
397 displayMetaProperty($fg) {
399 let ns = $fg.data('ns');
400 let tag = $fg.data('tag');
401 let field = $fg.data('field');
404 $('.c', $fg).empty();
406 let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
408 selector += "[x-ns='"+ns+"']";
410 $(selector, self.$edited).each(function() {
412 let text = $(this).text();
413 if (!self.applyFilter(field.filter, text))
422 let count = $('.c > .input-group', $fg).length;
423 if (field.required) {
425 $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
430 addEditField(defn, value, elem) {
432 let $form = $("#properties-form", self.$pane);
434 let $fg = $("<div class='form-group'>");
435 $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
436 let $input, $inputCnt;
439 $inputCnt =$input = $("<textarea>");
442 $inputCnt = $input = $("<select>");
443 $.each(defn.options, function(i, e) {
444 $("<option>").text(e).appendTo($input);
448 $inputCnt = $input = $("<input type='checkbox'>");
451 $inputCnt = $("<div class='media-input input-group'>");
452 $input = $("<input type='text'>");
453 $inputCnt.append($input);
454 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
457 $inputCnt = $input = $("<input>");
460 $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
461 if ($input.attr('type') == 'checkbox') {
462 $input.prop('checked', value == 'true');
468 $input.data("edited", elem);
470 $inputCnt.appendTo($fg);
476 this.$edited.attr('x-node', newtag);
477 // TODO: take care of attributes?
481 p = this.$edited.parent();
482 this.$edited.remove();
486 onEnter(success, failure) {
490 if ($.wiki.activePerspective() != 'VisualPerspective')
491 $.wiki.switchToTab('#VisualPerspective');
497 if (this.$edited === null) {
498 this.edit($('[x-node="utwor"]')[0]);
505 $.wiki.PropertiesPerspective = PropertiesPerspective;