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 $('input', $fg).data('edited').remove();
153 self.displayMetaProperty($fg);
154 $.wiki.perspectives.VisualPerspective.flush();
158 $('#media-chooser').on('show.bs.modal', function (event) {
159 var input = $("input", $(event.relatedTarget).parent());
161 modal.data('target-input', input);
162 var imglist = modal.find('.modal-body');
164 $.each(self.doc.galleryImages, (i, imgItem) => {
165 img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
166 imglist.find('img').removeClass('active');
167 $(this).addClass('active');
172 $('#media-chooser .ctrl-ok').on('click', function (event) {
173 $('#media-chooser').data('target-input')
175 (new URL($('#media-chooser .active').data('url'), document.baseURI)).href
177 $('#media-chooser').modal('hide');
180 self.$pane.on('click', '.current-convert', function() {
181 self.convert($(this).attr('data-to'));
183 self.$pane.on('click', '#current-delete', function() {
191 $("#parents", self.$pane).empty();
192 $("#bubbles").empty();
194 let $f = $("#properties-form", self.$pane);
197 if (element === null) {
199 $("h1", self.$pane).text('');
203 let $node = $(element);
204 let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
205 b.data('node', element);
206 $("#bubbles").append(b);
208 $node.parents('[x-node]').each(function() {
209 let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
210 a.data('node', this);
211 $("#parents", self.$pane).prepend(a)
213 let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
214 b.data('node', this);
215 $("#bubbles").append(b);
219 let node = $(element).attr('x-node');
220 $("h1", self.$pane).text(node);
222 self.$edited = $(element);
224 let nodeDef = elementDefs[node];
225 if (nodeDef && nodeDef.attributes) {
226 $.each(nodeDef.attributes, function(i, a) {
227 self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
231 // Only utwor can has matadata now.
232 if (node == 'utwor') {
233 $('<hr>').appendTo($("#properties-form", self.$pane))
234 META_FIELDS.forEach(function(field) {
235 let $fg = $("<div class='form-group'>");
236 $("<label/>").text(field.name).appendTo($fg);
239 $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
241 let match = field.uri.match(/({(.*)})?(.*)/);
245 let cont = $('<div class="c">');
248 $fg.data('tag', tag);
249 $fg.data('field', field);
252 self.displayMetaProperty($fg);
254 $fg.appendTo( $("#properties-form", self.$pane));
258 // check node type, find relevant tags
259 if ($node[0].nodeName == 'DIV') {
260 $("#current-convert").attr("data-current-type", "div");
261 } else if ($node[0].nodeName == 'EM') {
262 $("#current-convert").attr("data-current-type", "span");
266 addMetaInput(cont, field, element) {
269 let ig = $('<div class="input-group">');
270 //ig.data('edited', element);
273 if (field.value_type.hasLanguage) {
274 let pp = $("<div class='input-group-prepend'>");
275 let lang_input = $("<input class='form-control' size='1' class='lang'>");
276 lang_input.data('edited', $(element));
277 lang_input.data('edited-attr', 'x-a-xml-lang');
279 $(element).attr('x-a-xml-lang')
281 lang_input.appendTo(pp);
286 if (field.value_type.widget == 'select') {
287 $aninput = $("<select class='form-control'>");
288 $.each(field.value_type.options, function() {
289 $("<option>").text(this).appendTo($aninput);
292 $aninput = $("<input class='form-control'>");
293 if (field.value_type.autocomplete) {
294 let autoOptions = field.value_type.autocomplete;
295 $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
297 if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
298 if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
300 .append( "<div>" + t + "</div>" )
305 $aninput.data('edited', $(element))
309 $aninput.appendTo(ig);
311 let ap = $("<div class='input-group-append'>");
313 $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
318 displayMetaProperty($fg) {
320 let ns = $fg.data('ns');
321 let tag = $fg.data('tag');
322 let field = $fg.data('field');
325 $('.c', $fg).empty();
327 let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
329 selector += "[x-ns='"+ns+"']";
331 $(selector, self.$edited).each(function() {
338 let count = $('.c > .input-group', $fg).length;
339 if (field.required) {
341 $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
346 addEditField(defn, value, elem) {
348 let $form = $("#properties-form", self.$pane);
350 let $fg = $("<div class='form-group'>");
351 $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
352 let $input, $inputCnt;
355 $inputCnt =$input = $("<textarea>");
358 $inputCnt = $input = $("<select>");
359 $.each(defn.options, function(i, e) {
360 $("<option>").text(e).appendTo($input);
364 $inputCnt = $input = $("<input type='checkbox'>");
367 $inputCnt = $("<div class='media-input input-group'>");
368 $input = $("<input type='text'>");
369 $inputCnt.append($input);
370 $inputCnt.append($("<button type='button' class='ctrl-media-choose btn btn-primary' data-toggle='modal' data-target='#media-chooser'>…</button>"));
373 $inputCnt = $input = $("<input>");
376 $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
377 if ($input.attr('type') == 'checkbox') {
378 $input.prop('checked', value == 'true');
384 $input.data("edited", elem);
386 $inputCnt.appendTo($fg);
392 this.$edited.attr('x-node', newtag);
393 // TODO: take care of attributes?
397 p = this.$edited.parent();
398 this.$edited.remove();
402 onEnter(success, failure) {
406 if ($.wiki.activePerspective() != 'VisualPerspective')
407 $.wiki.switchToTab('#VisualPerspective');
413 if (this.$edited === null) {
414 this.edit($('[x-node="utwor"]')[0]);
421 $.wiki.PropertiesPerspective = PropertiesPerspective;