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 span.appendTo(rdfdesc);
143 rdfdesc.append('\n ');
145 self.displayMetaProperty($fg);
150 self.$pane.on('click', '.meta-delete', function() {
151 let $fg = $(this).closest('.form-group');
152 let $ig = $(this).closest('.input-group');
153 $('input', $ig).data('edited').remove();
154 self.displayMetaProperty($fg);
155 $.wiki.perspectives.VisualPerspective.flush();
159 $('#media-chooser').on('show.bs.modal', function (event) {
160 var input = $("input", $(event.relatedTarget).parent());
162 modal.data('target-input', input);
163 var imglist = modal.find('.modal-body');
165 $.each(self.doc.galleryImages, (i, imgItem) => {
166 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');
173 $('#media-chooser .ctrl-ok').on('click', function (event) {
174 $('#media-chooser').data('target-input')
176 (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
178 $('#media-chooser').modal('hide');
182 $('#meta-chooser').on('show.bs.modal', function (event) {
183 let input = $("input", $(event.relatedTarget).closest('.input-group'));
184 let $fg = $(event.relatedTarget).closest('.form-group');
185 let field = $fg.data('field');
187 modal.data('target-input', input);
188 let body = modal.find('.modal-body');
191 let add_options = function(cnt, options, value) {
192 $.each(options, (i, item) => {
193 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>');
195 $('input', elem).remove();
198 $('input', elem).prop('disabled', 'disabled');
200 $('input', elem).val(item.value);
201 $('input', elem).val(item.value);
202 $('.value', elem).text(item.value);
203 $('.name', elem).append(item.name);
204 $('.description', elem).append(item.description);
205 let valueMatch = value && value.startsWith(item.value);
207 $('label', elem).addClass('text-primary')
208 if (value == item.value) {
209 $('input', elem).prop('checked', true);
213 let subT = $('<div class="meta-chooser_toggle">+</div>');
214 let sub = $('<div>');
217 subT.on('click', () => {
220 add_options(sub, item.sub, valueMatch ? value : null);
227 url: field.value_type.chooser.source,
228 success: function(data) {
229 add_options(body, data, input.val());
233 $('#meta-chooser .ctrl-ok').on('click', function (event) {
234 $('#meta-chooser').data('target-input').val(
235 $('#meta-chooser :checked').val()
237 $('#meta-chooser').modal('hide');
240 self.$pane.on('click', '.current-convert', function() {
241 self.convert($(this).attr('data-to'));
243 self.$pane.on('click', '#current-delete', function() {
251 $("#parents", self.$pane).empty();
252 $("#bubbles").empty();
254 let $f = $("#properties-form", self.$pane);
257 if (element === null) {
259 $("h1", self.$pane).text('');
263 let $node = $(element);
264 let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
265 b.data('node', element);
266 $("#bubbles").append(b);
268 $node.parents('[x-node]').each(function() {
269 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
270 a.data('node', this);
271 $("#parents", self.$pane).prepend(a)
273 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
274 b.data('node', this);
275 $("#bubbles").append(b);
279 let node = $(element).attr('x-node');
280 $("h1", self.$pane).text(node);
282 self.$edited = $(element);
284 let nodeDef = elementDefs[node];
285 if (nodeDef && nodeDef.attributes) {
286 $.each(nodeDef.attributes, function(i, a) {
287 self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
291 // Only utwor can has matadata now.
292 if (node == 'utwor') {
293 $('<hr>').appendTo($("#properties-form", self.$pane))
294 META_FIELDS.forEach(function(field) {
295 let $fg = $("<div class='form-group'>");
296 $("<label/>").text(field.name).appendTo($fg);
299 $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
301 let match = field.uri.match(/({(.*)})?(.*)/);
305 let cont = $('<div class="c">');
308 $fg.data('tag', tag);
309 $fg.data('field', field);
312 self.displayMetaProperty($fg);
314 $fg.appendTo( $("#properties-form", self.$pane));
318 // check node type, find relevant tags
319 if ($node[0].nodeName == 'DIV') {
320 $("#current-convert").attr("data-current-type", "div");
321 } else if ($node[0].nodeName == 'EM') {
322 $("#current-convert").attr("data-current-type", "span");
326 addMetaInput(cont, field, element) {
329 let ig = $('<div class="input-group">');
330 //ig.data('edited', element);
333 if (field.value_type.hasLanguage) {
334 let pp = $("<div class='input-group-prepend'>");
335 let lang_input = $("<input class='form-control' size='1' class='lang'>");
336 lang_input.data('edited', $(element));
337 lang_input.data('edited-attr', 'x-a-xml-lang');
339 $(element).attr('x-a-xml-lang')
341 lang_input.appendTo(pp);
346 if (field.value_type.widget == 'select') {
347 $aninput = $("<select class='form-control'>");
348 $.each(field.value_type.options, function() {
349 $("<option>").text(this).appendTo($aninput);
352 $aninput = $("<input class='form-control'>");
353 if (field.value_type.autocomplete) {
354 let autoOptions = field.value_type.autocomplete;
355 $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
357 if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
358 if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
360 .append( "<div>" + t + "</div>" )
365 $aninput.data('edited', $(element))
369 $aninput.appendTo(ig);
371 let ap = $("<div class='input-group-append'>");
374 if (field.value_type.chooser) {
375 ap.append($("<button type='button' class='btn btn-outline-secondary' data-toggle='modal' data-target='#meta-chooser'>…</button>"));
377 $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
382 displayMetaProperty($fg) {
384 let ns = $fg.data('ns');
385 let tag = $fg.data('tag');
386 let field = $fg.data('field');
389 $('.c', $fg).empty();
391 let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
393 selector += "[x-ns='"+ns+"']";
395 $(selector, self.$edited).each(function() {
402 let count = $('.c > .input-group', $fg).length;
403 if (field.required) {
405 $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
410 addEditField(defn, value, elem) {
412 let $form = $("#properties-form", self.$pane);
414 let $fg = $("<div class='form-group'>");
415 $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
416 let $input, $inputCnt;
419 $inputCnt =$input = $("<textarea>");
422 $inputCnt = $input = $("<select>");
423 $.each(defn.options, function(i, e) {
424 $("<option>").text(e).appendTo($input);
428 $inputCnt = $input = $("<input type='checkbox'>");
431 $inputCnt = $("<div class='media-input input-group'>");
432 $input = $("<input type='text'>");
433 $inputCnt.append($input);
434 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
437 $inputCnt = $input = $("<input>");
440 $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
441 if ($input.attr('type') == 'checkbox') {
442 $input.prop('checked', value == 'true');
448 $input.data("edited", elem);
450 $inputCnt.appendTo($fg);
456 this.$edited.attr('x-node', newtag);
457 // TODO: take care of attributes?
461 p = this.$edited.parent();
462 this.$edited.remove();
466 onEnter(success, failure) {
470 if ($.wiki.activePerspective() != 'VisualPerspective')
471 $.wiki.switchToTab('#VisualPerspective');
477 if (this.$edited === null) {
478 this.edit($('[x-node="utwor"]')[0]);
485 $.wiki.PropertiesPerspective = PropertiesPerspective;