Fix error with revision check during saving.
[redakcja.git] / src / redakcja / static / js / wiki / view_properties.js
index 9fa1c9d..dde914f 100644 (file)
                     "name": "href",
                 },
             ],
                     "name": "href",
                 },
             ],
-        }
+            "tab": {"attributes": [{"name": "szer"}]},
+            "naglowek_parsza": {"attributes": [{"name": "nazwa"}]},
+            "naglowek_czytanie": {"attributes": [{"name": "nazwa"}]}
+        },
     };
 
     class PropertiesPerspective extends $.wiki.SidebarPerspective {
     };
 
     class PropertiesPerspective extends $.wiki.SidebarPerspective {
                         rdfdesc.append('\n    ');
                         rdf.append('\n  ');
                     }
                         rdfdesc.append('\n    ');
                         rdf.append('\n  ');
                     }
+                    if (field.filter && field.filter.startswith) {
+                        span.text(field.filter.startswith[0]);
+                    }
                     span.appendTo(rdfdesc);
                     rdfdesc.append('\n    ');
 
                     span.appendTo(rdfdesc);
                     rdfdesc.append('\n    ');
 
                     modal.data('target-input', input);
                     var imglist = modal.find('.modal-body');
                     imglist.html('');
                     modal.data('target-input', input);
                     var imglist = modal.find('.modal-body');
                     imglist.html('');
-                    $.each(self.doc.galleryImages, (i, imgItem) => {
-                        img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
-                            imglist.find('img').removeClass('active');
-                            $(this).addClass('active');
-                        });
-                        imglist.append(img);
+
+                    self.doc.refreshImageGallery({
+                        success: function(galleryImages) {
+                            $.each(self.doc.galleryImages, (i, imgItem) => {
+                                let img = $("<img>").attr("src", imgItem.thumb).attr('title', imgItem.url).data('url', imgItem.url).on('click', function() {
+                                    imglist.find('img').removeClass('active');
+                                    $(this).addClass('active');
+                                });
+                                imglist.append(img);
+                            });
+                        }
                     });
                 })
                 $('#media-chooser .ctrl-ok').on('click', function (event) {
                     });
                 })
                 $('#media-chooser .ctrl-ok').on('click', function (event) {
                     $('#media-chooser').modal('hide');
                 });
 
                     $('#media-chooser').modal('hide');
                 });
 
+            /* Meta chooser */
+            $('#meta-chooser').on('show.bs.modal', function (event) {
+                let input = $("input", $(event.relatedTarget).closest('.input-group'));
+                let $fg = $(event.relatedTarget).closest('.form-group');
+                let field = $fg.data('field');
+                let modal = $(this);
+                modal.data('target-input', input);
+                let body = modal.find('.modal-body');
+                body.html('');
+
+                let add_options = function(cnt, options, value) {
+                    $.each(options, (i, item) => {
+                        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>');
+                        if (!item.usable) {
+                            $('input', elem).remove();
+                        }
+                        if (item.hidden) {
+                            $('input', elem).prop('disabled', 'disabled');
+                        }
+                        $('input', elem).val(item.value);
+                        $('input', elem).val(item.value);
+                        $('.value', elem).text(item.value);
+                        $('.name', elem).append(item.name);
+                        $('.description', elem).append(item.description);
+                        let valueMatch = value && value.startsWith(item.value);
+                        if (valueMatch) {
+                            $('label', elem).addClass('text-primary')
+                            if (value == item.value) {
+                                $('input', elem).prop('checked', true);
+                            }
+                        }
+                        if (item.sub) {
+                            let subT = $('<div class="meta-chooser_toggle">+</div>');
+                            let sub = $('<div>');
+                            elem.append(subT);
+                            elem.append(sub);
+                            subT.on('click', () => {
+                                sub.toggle()
+                            });
+                            add_options(sub, item.sub, valueMatch ? value : null);
+                        }
+                        elem.appendTo(cnt);
+                    });
+                };
+
+                $.ajax({
+                    url: field.value_type.chooser.source,
+                    data: field.filter ? {'filter': JSON.stringify(field.filter)} : '',
+                    success: function(data) {
+                        add_options(body, data, input.val());
+                    }
+                });
+            })
+            $('#meta-chooser .ctrl-ok').on('click', function (event) {
+                $('#meta-chooser').data('target-input').val(
+                    $('#meta-chooser :checked').val()
+                ).trigger('change');
+                $('#meta-chooser').modal('hide');
+            });
+
                 self.$pane.on('click', '.current-convert', function() {
                     self.convert($(this).attr('data-to'));
                 });
                 self.$pane.on('click', '.current-convert', function() {
                     self.convert($(this).attr('data-to'));
                 });
 
             let ap = $("<div class='input-group-append'>");
             ap.appendTo(ig);
 
             let ap = $("<div class='input-group-append'>");
             ap.appendTo(ig);
+
+            if (field.value_type.chooser) {
+                ap.append($("<button type='button' class='btn btn-outline-secondary' data-toggle='modal' data-target='#meta-chooser'>…</button>"));
+            }
             $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
 
             // lang
         }
 
             $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
 
             // lang
         }
 
+        applyFilter(filter, text) {
+            if (filter.not) {
+                return !this.applyFilter(filter.not, text)
+            } else if (filter.startswith) {
+                for (prefix of filter.startswith) {
+                    if (text.startsWith(prefix))
+                        return true;
+                }
+                return false;
+            }
+        }
+        
         displayMetaProperty($fg) {
             let self = this;
             let ns = $fg.data('ns');
         displayMetaProperty($fg) {
             let self = this;
             let ns = $fg.data('ns');
                 selector += "[x-ns='"+ns+"']";
             }
             $(selector, self.$edited).each(function() {
                 selector += "[x-ns='"+ns+"']";
             }
             $(selector, self.$edited).each(function() {
+                if (field.filter) {
+                    let text = $(this).text();
+                    if (!self.applyFilter(field.filter, text))
+                        return;
+                }
                 self.addMetaInput(
                     $('.c', $fg),
                     field,
                 self.addMetaInput(
                     $('.c', $fg),
                     field,