85a6fdd9549c23ffd2c436f467039f63e38926cd
[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(currentNode && !currentNode.isInDocument()) {
39                             module.setNodeElement(null);
40                         }
41                         if(event.type === 'nodeAttrChange' && event.meta.node.sameNode(currentNode)) {
42                             module.setNodeElement(currentNode);
43                         }
44                     });
45                     listens = true;
46                 }
47
48                 view.find('.rng-module-nodePane-tagSelect').attr('disabled', false).val(wlxmlNodeElement.getTagName());
49
50                 var escapedClassName = (wlxmlNodeElement.getClass() || '').replace(/\./g, '-');
51                 view.find('.rng-module-nodePane-classSelect').attr('disabled', false).val(escapedClassName);
52
53                 var attrs = _.extend(wlxmlNodeElement.getMetaAttributes(), wlxmlNodeElement.getOtherAttributes());
54                 var widget = metaWidget.create({attrs:attrs});
55                 widget.on('valueChanged', function(key, value) {
56                     wlxmlNodeElement.setMetaAttribute(key, value);
57                     //wlxmlNodeElement.setMetaAttribute(key, value);
58                 });
59                 view.find('.metaFields').empty().append(widget.el);
60             } else {
61                 view.find('.rng-module-nodePane-tagSelect').attr('disabled', true).val('');
62                 view.find('.rng-module-nodePane-classSelect').attr('disabled', true).val('');
63                 view.find('.metaFields').empty();
64             }
65             currentNode = wlxmlNodeElement;
66         }
67     };
68     
69 };
70
71 });