f7e6694390c9b370af7a62d4e7e08772c9cd8faf
[fnpeditor.git] / src / editor / modules / nodePane / nodePane.js
1 define([
2 'libs/text!./template.html',
3 'libs/jquery',
4 'libs/underscore',
5 'modules/nodePane/metaWidget/metaWidget',
6 'utils/wlxml'
7 ], function(templateSrc, $, _, metaWidget, wlxmlUtils) {
8
9 'use strict';
10
11 return function(sandbox) {
12     
13     var view = $(_.template(templateSrc)({utils: wlxmlUtils})),
14         listens = false,
15         currentNode;
16     
17     view.on('change', 'select', function(e) {
18         var target = $(e.target);
19         var attr = target.attr('class').split('-')[3] === 'tagSelect' ? 'Tag' : 'Class',
20             value = target.val().replace(/-/g, '.');
21         currentNode['set' + attr](value);
22     });
23
24
25    
26     return {
27         start: function() {
28             sandbox.publish('ready');
29         },
30         getView: function() {
31             return view;
32         },
33         setNodeElement: function(wlxmlNodeElement) {
34             if(wlxmlNodeElement) {
35                 var module = this;
36                 if(!listens) {
37                     wlxmlNodeElement.document.on('change', function(event) {
38                         if(event.type === 'nodeAttrChange' && event.meta.node.sameNode(currentNode)) {
39                             module.setNodeElement(currentNode);
40                         }
41                     });
42                     listens = true;
43                 }
44
45                 view.find('.rng-module-nodePane-tagSelect').attr('disabled', false).val(wlxmlNodeElement.getTagName());
46
47                 var escapedClassName = (wlxmlNodeElement.getClass() || '').replace(/\./g, '-');
48                 view.find('.rng-module-nodePane-classSelect').attr('disabled', false).val(escapedClassName);
49
50                 var attrs = _.extend(wlxmlNodeElement.getMetaAttributes(), wlxmlNodeElement.getOtherAttributes());
51                 var widget = metaWidget.create({attrs:attrs});
52                 widget.on('valueChanged', function(key, value) {
53                     wlxmlNodeElement.setMetaAttribute(key, value);
54                     //wlxmlNodeElement.setMetaAttribute(key, value);
55                 });
56                 view.find('.metaFields').empty().append(widget.el);
57             } else {
58                 view.find('.rng-module-nodePane-tagSelect').attr('disabled', true).val('');
59                 view.find('.rng-module-nodePane-classSelect').attr('disabled', true).val('');
60                 view.find('.metaFields').empty();
61             }
62             currentNode = wlxmlNodeElement;
63         }
64     };
65     
66 };
67
68 });