571dac00d2ff3281cb46d0618e0621f77bfad9c4
[redakcja.git] / src / redakcja / static / js / wiki / view_properties.js
1 (function($){
2
3     let w = function() {};
4     w = console.log;
5     
6     const elementDefs = {
7         "ilustr": {
8             "attributes": [
9                 {
10                     "name": "src",
11                 },
12                 {
13                     "name": "alt",
14                     "type": "text",
15                 },
16             ],
17         },
18         "ref": {
19             "attributes": [
20                 {
21                     "name": "href",
22                 },
23             ],
24         }
25     };
26
27     function PropertiesPerspective(options) {
28         let oldCallback = options.callback || function() {};
29         this.vsplitbar = 'WŁAŚCIWOŚCI';
30
31         options.callback = function() {
32             let self = this;
33
34             self.$pane = $("#side-properties");
35             
36             $(document).on('click', '[x-node]', function(e) {
37                 e.stopPropagation();
38                 self.edit(this);
39             });
40
41             self.$pane.on('click', '#parents li', function(e) {
42                 self.edit($(this).data('node'));
43             });
44
45             self.$pane.on('change', '.form-control', function() {
46                 let $input = $(this);
47
48                 if ($input.data("edited")) {
49                     $input.data("edited").text($input.val());
50                     return;
51                 }
52                 
53                 html2text({
54                     element: self.$edited[0],
55                     success: function(xml) {
56                         w(222)
57                         let $xmlelem = $($.parseXML(xml));
58                         w(333, $xmlelem)
59                         $xmlelem.contents().attr($input.data('property'), $input.val());
60                         w(444, $xmlelem)
61                         let newxml = (new XMLSerializer()).serializeToString($xmlelem[0]);
62                         w(555, newxml)
63                         xml2html({
64                             xml: newxml,
65                             base: self.doc.getBase(),
66                             success: function(html) {
67                                 let htmlElem = $(html);
68                                 self.$edited.replaceWith(htmlElem);
69                                 self.edit(htmlElem);
70                             }
71                         });
72                     },
73                     error: function(e) {console.log(e);},
74                 });
75                 self.$edited;
76             });
77             
78             oldCallback.call(this);
79         };
80
81         $.wiki.SidebarPerspective.call(this, options);
82     }
83
84     PropertiesPerspective.prototype = new $.wiki.SidebarPerspective();
85
86     PropertiesPerspective.prototype.edit = function(element) {
87         let self = this;
88
89         let $node = $(element);
90         $("#parents", self.$pane).empty();
91         $node.parents('[x-node]').each(function() {
92             let a = $("<li class='breadcrumb-item'>").text($(this).attr('x-node'));
93             a.data('node', this);
94             $("#parents", self.$pane).prepend(a)
95         })
96         // It's a tag.
97         node = $(element).attr('x-node');
98         $("h1", self.$pane).text(node);
99
100         $f = $("#properties-form", self.$pane);
101         $f.empty();
102         self.$edited = $(element);
103
104         let nodeDef = elementDefs[node];
105         if (nodeDef && nodeDef.attributes) {
106             $.each(nodeDef.attributes, function(i, a) {
107                 self.addEditField(a.name, a.type, $(element).attr('data-wlf-' + a.name)); // ...
108             })
109         }
110
111
112         // Only utwor can has matadata now.
113         if (node == 'utwor') {
114             // Let's find all the metadata.
115             $("> .RDF > .Description > [x-node]", $node).each(function() {
116                 $meta = $(this);
117                 self.addEditField(
118                     $meta.attr('x-node'),
119                     null,
120                     $meta.text(),
121                     $meta,
122                 );
123             });
124         }
125     };
126         
127     PropertiesPerspective.prototype.addEditField = function(name, type, value, elem) {
128         let self = this;
129         let $form = $("#properties-form", self.$pane);
130
131         let $fg = $("<div class='form-group'>");
132         $("<label/>").attr("for", "property-" + name).text(name).appendTo($fg);
133         let $input;
134         if (type == 'text') {
135             $input = $("<textarea>");
136         } else {
137             $input = $("<input>")
138         }
139         // val?
140         $input.addClass("form-control").attr("id", "property-" + name).data("property", name).val(value);
141         if (elem) {
142             $input.data("edited", elem);
143         }
144         $input.appendTo($fg);
145
146         $fg.appendTo($form);
147     }
148     
149     $.wiki.PropertiesPerspective = PropertiesPerspective;
150
151 })(jQuery);
152