37eefb836d491dd333d1aaf66a6877056485644a
[fnpeditor.git] / src / editor / plugins / core / canvasElements.js
1 define(function(require) {
2     
3 'use strict';
4
5
6 var widgets = {
7     footnoteHandler: function(clickHandler) {
8         var mydom = $('<span>')
9             .addClass('canvas-widget canvas-widget-footnote-handle')
10             .css('display', 'inline')
11             .show();
12
13         mydom.click(function(e) {
14             e.stopPropagation();
15             clickHandler();
16         });
17
18         return mydom;
19     },
20     commentAdnotation: function(node) {
21         var widget = {
22             DOM: $('<div>').addClass('canvas-widget canvas-widget-comment-adnotation'),
23             update: function(node) {
24                 var parts = [],
25                     metadata = node.getMetadata(),
26                     dt;
27                 metadata.forEach(function(row) {
28                     parts.push(row.getValue());
29                 }, 'creator');
30                 metadata.some(function(row) {
31                     dt = row.getValue();
32                     return true; // break
33                 }, 'date');
34                 if(dt) {
35                     parts.push(dt);
36                 }
37                 this.DOM.text(parts.join(', '));
38             }
39         };
40         widget.update(node);
41         return widget;
42     },
43     hideButton: function(clickHandler) {
44         var mydom = $('<span>x</span>')
45             .addClass('canvas-widget canvas-widget-hide-button');
46         mydom.click(function(e) {
47             e.stopPropagation();
48             clickHandler();
49         });
50         return mydom;
51     }
52 }
53
54
55 var comment = {
56     init: function() {
57         this.super.init.call(this);
58         this.commentAdnotation = widgets.commentAdnotation(this.wlxmlNode);
59         this.addWidget(this.commentAdnotation, 'show');
60         this.commentAdnotation.DOM.show();
61     },
62
63     onMetadataChanged: function(event) {
64         this.commentAdnotation.update(event.meta.node);
65     },
66     onMetadataAdded: function(event) {
67         return this.onMetadataChanged(event);
68     },
69     onMetadataRemoved: function(event) {
70         return this.onMetadataChanged(event);
71     }
72 };
73
74 var footnote = {
75     init: function() {
76         this.super.init.call(this);
77         var clickHandler = function() {
78             this.toggle(true);
79         }.bind(this);
80         this.footnoteHandler = widgets.footnoteHandler(clickHandler);
81         this.addWidget(this.footnoteHandler);
82
83         var closeHandler = function() {
84             this.toggle(false);
85         }.bind(this);
86         this.hideButton = widgets.hideButton(closeHandler);
87         this.addWidget(this.hideButton);
88         this.toggle(false, {silent: true});
89     },
90     toggle: function(toggle, options) {
91         options = options || {};
92         this.hideButton.toggle(toggle);
93         this.footnoteHandler.toggle(!toggle);
94         
95         if(toggle) {
96             this.displayAsBlock();
97         } else {
98             this.displayInline();
99         }
100         this._container().toggle(toggle);
101         if(!options.silent) {
102             this.trigger('elementToggled', toggle, this.documentElement);
103         }
104     }
105 }
106
107
108 return [
109     {klass: 'comment', element: comment},
110     {klass: 'footnote', element: footnote}
111 ];
112
113
114 });