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');
166 self.doc.refreshImageGallery({
167 success: function(galleryImages) {
168 $.each(self.doc.galleryImages, (i, imgItem) => {
169 let img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
170 imglist.find('img').removeClass('active');
171 $(this).addClass('active');
178 $('#media-chooser .ctrl-ok').on('click', function (event) {
179 $('#media-chooser').data('target-input')
181 (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
183 $('#media-chooser').modal('hide');
187 $('#meta-chooser').on('show.bs.modal', function (event) {
188 let input = $("input", $(event.relatedTarget).closest('.input-group'));
189 let $fg = $(event.relatedTarget).closest('.form-group');
190 let field = $fg.data('field');
192 modal.data('target-input', input);
193 let body = modal.find('.modal-body');
196 let add_options = function(cnt, options, value) {
197 $.each(options, (i, item) => {
198 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>');
200 $('input', elem).remove();
203 $('input', elem).prop('disabled', 'disabled');
205 $('input', elem).val(item.value);
206 $('input', elem).val(item.value);
207 $('.value', elem).text(item.value);
208 $('.name', elem).append(item.name);
209 $('.description', elem).append(item.description);
210 let valueMatch = value && value.startsWith(item.value);
212 $('label', elem).addClass('text-primary')
213 if (value == item.value) {
214 $('input', elem).prop('checked', true);
218 let subT = $('<div class="meta-chooser_toggle">+</div>');
219 let sub = $('<div>');
222 subT.on('click', () => {
225 add_options(sub, item.sub, valueMatch ? value : null);
232 url: field.value_type.chooser.source,
233 success: function(data) {
234 add_options(body, data, input.val());
238 $('#meta-chooser .ctrl-ok').on('click', function (event) {
239 $('#meta-chooser').data('target-input').val(
240 $('#meta-chooser :checked').val()
242 $('#meta-chooser').modal('hide');
245 self.$pane.on('click', '.current-convert', function() {
246 self.convert($(this).attr('data-to'));
248 self.$pane.on('click', '#current-delete', function() {
256 $("#parents", self.$pane).empty();
257 $("#bubbles").empty();
259 let $f = $("#properties-form", self.$pane);
262 if (element === null) {
264 $("h1", self.$pane).text('');
268 let $node = $(element);
269 let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
270 b.data('node', element);
271 $("#bubbles").append(b);
273 $node.parents('[x-node]').each(function() {
274 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
275 a.data('node', this);
276 $("#parents", self.$pane).prepend(a)
278 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
279 b.data('node', this);
280 $("#bubbles").append(b);
284 let node = $(element).attr('x-node');
285 $("h1", self.$pane).text(node);
287 self.$edited = $(element);
289 let nodeDef = elementDefs[node];
290 if (nodeDef && nodeDef.attributes) {
291 $.each(nodeDef.attributes, function(i, a) {
292 self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
296 // Only utwor can has matadata now.
297 if (node == 'utwor') {
298 $('<hr>').appendTo($("#properties-form", self.$pane))
299 META_FIELDS.forEach(function(field) {
300 let $fg = $("<div class='form-group'>");
301 $("<label/>").text(field.name).appendTo($fg);
304 $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
306 let match = field.uri.match(/({(.*)})?(.*)/);
310 let cont = $('<div class="c">');
313 $fg.data('tag', tag);
314 $fg.data('field', field);
317 self.displayMetaProperty($fg);
319 $fg.appendTo( $("#properties-form", self.$pane));
323 // check node type, find relevant tags
324 if ($node[0].nodeName == 'DIV') {
325 $("#current-convert").attr("data-current-type", "div");
326 } else if ($node[0].nodeName == 'EM') {
327 $("#current-convert").attr("data-current-type", "span");
331 addMetaInput(cont, field, element) {
334 let ig = $('<div class="input-group">');
335 //ig.data('edited', element);
338 if (field.value_type.hasLanguage) {
339 let pp = $("<div class='input-group-prepend'>");
340 let lang_input = $("<input class='form-control' size='1' class='lang'>");
341 lang_input.data('edited', $(element));
342 lang_input.data('edited-attr', 'x-a-xml-lang');
344 $(element).attr('x-a-xml-lang')
346 lang_input.appendTo(pp);
351 if (field.value_type.widget == 'select') {
352 $aninput = $("<select class='form-control'>");
353 $.each(field.value_type.options, function() {
354 $("<option>").text(this).appendTo($aninput);
357 $aninput = $("<input class='form-control'>");
358 if (field.value_type.autocomplete) {
359 let autoOptions = field.value_type.autocomplete;
360 $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
362 if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
363 if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
365 .append( "<div>" + t + "</div>" )
370 $aninput.data('edited', $(element))
374 $aninput.appendTo(ig);
376 let ap = $("<div class='input-group-append'>");
379 if (field.value_type.chooser) {
380 ap.append($("<button type='button' class='btn btn-outline-secondary' data-toggle='modal' data-target='#meta-chooser'>…</button>"));
382 $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
387 displayMetaProperty($fg) {
389 let ns = $fg.data('ns');
390 let tag = $fg.data('tag');
391 let field = $fg.data('field');
394 $('.c', $fg).empty();
396 let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
398 selector += "[x-ns='"+ns+"']";
400 $(selector, self.$edited).each(function() {
407 let count = $('.c > .input-group', $fg).length;
408 if (field.required) {
410 $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
415 addEditField(defn, value, elem) {
417 let $form = $("#properties-form", self.$pane);
419 let $fg = $("<div class='form-group'>");
420 $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
421 let $input, $inputCnt;
424 $inputCnt =$input = $("<textarea>");
427 $inputCnt = $input = $("<select>");
428 $.each(defn.options, function(i, e) {
429 $("<option>").text(e).appendTo($input);
433 $inputCnt = $input = $("<input type='checkbox'>");
436 $inputCnt = $("<div class='media-input input-group'>");
437 $input = $("<input type='text'>");
438 $inputCnt.append($input);
439 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
442 $inputCnt = $input = $("<input>");
445 $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
446 if ($input.attr('type') == 'checkbox') {
447 $input.prop('checked', value == 'true');
453 $input.data("edited", elem);
455 $inputCnt.appendTo($fg);
461 this.$edited.attr('x-node', newtag);
462 // TODO: take care of attributes?
466 p = this.$edited.parent();
467 this.$edited.remove();
471 onEnter(success, failure) {
475 if ($.wiki.activePerspective() != 'VisualPerspective')
476 $.wiki.switchToTab('#VisualPerspective');
482 if (this.$edited === null) {
483 this.edit($('[x-node="utwor"]')[0]);
490 $.wiki.PropertiesPerspective = PropertiesPerspective;