Uwaga button.
[redakcja.git] / src / redakcja / static / js / wiki / view_properties.js
index 571dac0..204a4de 100644 (file)
                     "name": "alt",
                     "type": "text",
                 },
                     "name": "alt",
                     "type": "text",
                 },
+                {
+                    "name": "szer",
+                    "type": "percent",
+                },
+                {
+                    "name": "wyrownanie",
+                    "type": "select",
+                    "options": ["lewo", "srodek", "prawo"],
+                },
+                {
+                    "name": "oblew",
+                    "type": "bool",
+                },
             ],
         },
         "ref": {
             ],
         },
         "ref": {
 
             self.$pane = $("#side-properties");
             
 
             self.$pane = $("#side-properties");
             
-            $(document).on('click', '[x-node]', function(e) {
-                e.stopPropagation();
-                self.edit(this);
+            $("#simple-editor").on('click', '[x-node]', function(e) {
+                if (!e.redakcja_edited) {
+                    e.redakcja_edited = true;
+                    self.edit(this);
+                }
             });
 
             self.$pane.on('click', '#parents li', function(e) {
                 self.edit($(this).data('node'));
             });
 
             });
 
             self.$pane.on('click', '#parents li', function(e) {
                 self.edit($(this).data('node'));
             });
 
+            $(document).on('click', '#bubbles .badge', function(e) {
+                self.edit($(this).data('node'));
+            });
+
             self.$pane.on('change', '.form-control', function() {
                 let $input = $(this);
 
             self.$pane.on('change', '.form-control', function() {
                 let $input = $(this);
 
+                let inputval;
+                if ($input.attr('type') == 'checkbox') {
+                    inputval = $input.is(':checked');
+                } else {
+                    inputval = $input.val();
+                }
+                
                 if ($input.data("edited")) {
                 if ($input.data("edited")) {
-                    $input.data("edited").text($input.val());
+                    if ($input.data("edited-attr")) {
+                        $input.data("edited").attr($input.data("edited-attr"), inputval);
+                    } else {
+                        $input.data("edited").text(inputval);
+                    }
                     return;
                 }
                 
                     return;
                 }
                 
@@ -56,7 +86,8 @@
                         w(222)
                         let $xmlelem = $($.parseXML(xml));
                         w(333, $xmlelem)
                         w(222)
                         let $xmlelem = $($.parseXML(xml));
                         w(333, $xmlelem)
-                        $xmlelem.contents().attr($input.data('property'), $input.val());
+                        w($input.data('property'), $input.val());
+                        $xmlelem.contents().attr($input.data('property'), inputval);
                         w(444, $xmlelem)
                         let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
                         w(555, newxml)
                         w(444, $xmlelem)
                         let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
                         w(555, newxml)
                 });
                 self.$edited;
             });
                 });
                 self.$edited;
             });
+
+            
+            self.$pane.on('click', '.meta-add', function() {
+                // create a metadata item
+                let $fg = $(this).parent();
+                let ns = $fg.data('ns');
+                let tag = $fg.data('tag');
+                let field = $fg.data('field');
+                let span = $('<span/>');
+                span.attr('x-node', tag);
+                span.attr('x-ns', ns)
+                if (field.value_type.hasLanguage) {
+                    span.attr('x-a-xml-lang', 'pl');
+                }
+
+                rdf = $("> [x-node='RDF']", self.$edited);
+                if (!rdf.length) {
+                    rdf = $("<span x-node='RDF' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>");
+                    self.$edited.prepend(rdf); 
+                    self.$edited.prepend('\n  ');
+                   
+                }
+                rdfdesc = $("> [x-node='Description']", rdf);
+                if (!rdfdesc.length) {
+                    rdfdesc = $("<span x-node='Description' x-ns='http://www.w3.org/1999/02/22-rdf-syntax-ns#' x-a-rdf-about='" + self.doc.fullUri + "'>");
+                    rdf.prepend(rdfdesc);
+                    rdf.prepend('\n    ');
+                    rdfdesc.append('\n    ');
+                    rdf.append('\n  ');
+                }
+                span.appendTo(rdfdesc);
+                rdfdesc.append('\n    ');
+
+                self.displayMetaProperty($fg);
+                
+                return false;
+            });
+            
+            self.$pane.on('click', '.meta-delete', function() {
+                let $fg = $(this).closest('.form-group'); 
+                $('input', $fg).data('edited').remove();
+                self.displayMetaProperty($fg);
+                return false;
+            });
+
+
+            
+            self.$pane.on('click', '.current-convert', function() {
+                self.convert($(this).attr('data-to'));
+            });
+            self.$pane.on('click', '#current-delete', function() {
+                self.delete();
+            });
+            
             
             oldCallback.call(this);
         };
             
             oldCallback.call(this);
         };
     PropertiesPerspective.prototype.edit = function(element) {
         let self = this;
 
     PropertiesPerspective.prototype.edit = function(element) {
         let self = this;
 
-        let $node = $(element);
         $("#parents", self.$pane).empty();
         $("#parents", self.$pane).empty();
+        $("#bubbles").empty();
+
+        $f = $("#properties-form", self.$pane);
+        $f.empty();
+
+        if (element === null) {
+            self.$edited = null;
+            return;
+        }
+
+        let $node = $(element);
+        let b = $("<div class='badge badge-primary'></div>").text($node.attr('x-node'));
+        b.data('node', element);
+        $("#bubbles").append(b);
+
         $node.parents('[x-node]').each(function() {
             let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
             a.data('node', this);
             $("#parents", self.$pane).prepend(a)
         $node.parents('[x-node]').each(function() {
             let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
             a.data('node', this);
             $("#parents", self.$pane).prepend(a)
+
+            let b = $("<div class='badge badge-info'></div>").text($(this).attr('x-node'));
+            b.data('node', this);
+            $("#bubbles").append(b);
         })
         })
+
         // It's a tag.
         node = $(element).attr('x-node');
         $("h1", self.$pane).text(node);
 
         // It's a tag.
         node = $(element).attr('x-node');
         $("h1", self.$pane).text(node);
 
-        $f = $("#properties-form", self.$pane);
-        $f.empty();
         self.$edited = $(element);
 
         let nodeDef = elementDefs[node];
         if (nodeDef && nodeDef.attributes) {
             $.each(nodeDef.attributes, function(i, a) {
         self.$edited = $(element);
 
         let nodeDef = elementDefs[node];
         if (nodeDef && nodeDef.attributes) {
             $.each(nodeDef.attributes, function(i, a) {
-                self.addEditField(a.name, a.type, $(element).attr('data-wlf-' + a.name)); // ...
+                self.addEditField(a, $(element).attr('x-a-wl-' + a.name)); // ...
             })
         }
 
             })
         }
 
-
         // Only utwor can has matadata now.
         if (node == 'utwor') {
         // Only utwor can has matadata now.
         if (node == 'utwor') {
-            // Let's find all the metadata.
-            $("> .RDF > .Description > [x-node]", $node).each(function() {
-                $meta = $(this);
-                self.addEditField(
-                    $meta.attr('x-node'),
-                    null,
-                    $meta.text(),
-                    $meta,
-                );
+            $('<hr>').appendTo($("#properties-form", self.$pane))
+            META_FIELDS.forEach(function(field) {
+                let $fg = $("<div class='form-group'>");
+                $("<label/>").text(field.name).appendTo($fg);
+
+                // if multiple?
+                $("<button class='meta-add float-right btn btn-primary'>+</button>").appendTo($fg);
+
+                let match = field.uri.match(/({(.*)})?(.*)/);
+                ns = match[2];
+                tag = match[3];
+
+                let cont = $('<div class="c">');
+
+                $fg.data('ns', ns);
+                $fg.data('tag', tag);
+                $fg.data('field', field);
+                cont.appendTo($fg);
+
+                self.displayMetaProperty($fg);
+
+                $fg.appendTo( $("#properties-form", self.$pane));
             });
         }
             });
         }
+
+        // check node type, find relevant tags
+        if ($node[0].nodeName == 'DIV') {
+            $("#current-convert").attr("data-current-type", "div");
+        } else if ($node[0].nodeName == 'EM') {
+            $("#current-convert").attr("data-current-type", "span");
+        }
     };
     };
+
+    PropertiesPerspective.prototype.addMetaInput = function(cont, field, element) {
+        let self = this;
+
+        let ig = $('<div class="input-group">');
+        //ig.data('edited', element);
+        ig.appendTo(cont);
+
+        if (field.value_type.hasLanguage) {
+            let pp = $("<div class='input-group-prepend'>");
+            let lang_input = $("<input class='form-control' size='1' class='lang'>");
+            lang_input.data('edited', $(element));
+            lang_input.data('edited-attr', 'x-a-xml-lang');
+            lang_input.val(
+                $(element).attr('x-a-xml-lang')
+            );
+            lang_input.appendTo(pp);
+            pp.appendTo(ig);
+        }
+
+        let $aninput;
+        if (field.value_type.widget == 'select') {
+            $aninput = $("<select class='form-control'>");
+            $.each(field.value_type.options, function() {
+                $("<option>").text(this).appendTo($aninput);
+            })
+        } else {
+            $aninput = $("<input class='form-control'>");
+            if (field.value_type.autocomplete) {
+                let autoOptions = field.value_type.autocomplete;
+                $aninput.autocomplete(autoOptions).autocomplete('instance')._renderItem = function(ul, item) {
+                    let t = item.label;
+                    if (item.name) t += '<br><small><strong>' + item.name + '</strong></small>';
+                    if (item.description) t += '<br><small><em>' + item.description + '</em></small>';
+                    return $( "<li>" )
+                        .append( "<div>" + t + "</div>" )
+                        .appendTo( ul );
+                };
+            }
+        }
+        $aninput.data('edited', $(element))
+        $aninput.val(
+            $(element).text()
+        );
+        $aninput.appendTo(ig);
+
+        let ap = $("<div class='input-group-append'>");
+        ap.appendTo(ig);
+        $("<button class='meta-delete btn btn-outline-secondary'>x</button>").appendTo(ap);
         
         
-    PropertiesPerspective.prototype.addEditField = function(name, type, value, elem) {
+        // lang
+    };
+    
+
+    PropertiesPerspective.prototype.displayMetaProperty = function($fg) {
+        let self = this;
+        let ns = $fg.data('ns');
+        let tag = $fg.data('tag');
+        let field = $fg.data('field');
+
+        //  clear container
+        $('.c', $fg).empty();
+
+        let selector = "> [x-node='RDF'] > [x-node='Description'] > [x-node='"+tag+"']";
+        if (ns) {
+            selector += "[x-ns='"+ns+"']";
+        }
+        $(selector, self.$edited).each(function() {
+            self.addMetaInput(
+                $('.c', $fg),
+                field,
+                this);
+        });
+
+        count = $('.c > .input-group', $fg).length;
+        if (field.required) {
+            if (!count) {
+                $('<div class="text-warning">WYMAGANE</div>').appendTo($('.c', $fg));
+            }
+        }
+    };
+    
+
+
+    PropertiesPerspective.prototype.addEditField = function(defn, value, elem) {
         let self = this;
         let $form = $("#properties-form", self.$pane);
 
         let $fg = $("<div class='form-group'>");
         let self = this;
         let $form = $("#properties-form", self.$pane);
 
         let $fg = $("<div class='form-group'>");
-        $("<label/>").attr("for", "property-" + name).text(name).appendTo($fg);
+        $("<label/>").attr("for", "property-" + defn.name).text(defn.name).appendTo($fg);
         let $input;
         let $input;
-        if (type == 'text') {
+        switch (defn.type) {
+        case 'text':
             $input = $("<textarea>");
             $input = $("<textarea>");
+            break;
+        case 'select':
+            $input = $("<select>");
+            $.each(defn.options, function(i, e) {
+                $("<option>").text(e).appendTo($input);
+            });
+            break;
+        case 'bool':
+            $input = $("<input type='checkbox'>");
+            break;
+        default:
+            $input = $("<input>");
+        }
+
+        $input.addClass("form-control").attr("id", "property-" + defn.name).data("property", defn.name);
+        if ($input.attr('type') == 'checkbox') {
+            $input.prop('checked', value == 'true');
         } else {
         } else {
-            $input = $("<input>")
+            $input.val(value);
         }
         }
-        // val?
-        $input.addClass("form-control").attr("id", "property-" + name).data("property", name).val(value);
+        
         if (elem) {
             $input.data("edited", elem);
         }
         if (elem) {
             $input.data("edited", elem);
         }
 
         $fg.appendTo($form);
     }
 
         $fg.appendTo($form);
     }
-    
+
+    PropertiesPerspective.prototype.convert = function(newtag) {
+        this.$edited.attr('x-node', newtag);
+        // TODO: take care of attributes?
+    }
+
+    PropertiesPerspective.prototype.delete = function(newtag) {
+        p = this.$edited.parent();
+        this.$edited.remove();
+        this.edit(p);
+    }
+
+    PropertiesPerspective.prototype.onEnter = function(success, failure){
+        var self = this;
+        $.wiki.SidebarPerspective.prototype.onEnter.call(this);
+
+        if ($.wiki.activePerspective() != 'VisualPerspective')
+            $.wiki.switchToTab('#VisualPerspective');
+
+        self.edit($('[x-node="utwor"]')[0]);
+    };
+
     $.wiki.PropertiesPerspective = PropertiesPerspective;
 
 })(jQuery);
     $.wiki.PropertiesPerspective = PropertiesPerspective;
 
 })(jQuery);