3bd6ed5b9c757246e2f02acf0bce73bd2fe047f6
[fnpeditor.git] / src / editor / plugins / core / edumed / choice / choiceBase.js
1 define(function(require) {
2     
3 'use strict';
4
5 var $ = require('libs/jquery'),
6     _ = require('libs/underscore'),
7     elementBase = require('plugins/core/edumed/elementBase'),
8     template = require('libs/text!./element.html');
9
10
11 var choiceBase = Object.create(elementBase);
12 _.extend(choiceBase, {
13     init: function() {
14         elementBase.init.call(this);
15         this.view = $(_.template(template)({type: this.type}));
16         this._container().append(this.view);
17
18         this.createContainer(this.wlxmlNode.contents().filter(function(n) {return !n.is('list');}), {
19             manages: function(node, removedFrom) {
20                 if(node.is('list.orderable')) {
21                     return false;
22                 }
23                 return this.wlxmlNode.sameNode(node.parent() || removedFrom);
24             }.bind(this),
25             dom: this.view.find('.description')
26         });
27
28         this.wlxmlNode.contents()
29             .filter(function(node) { return node.is('list'); })
30             .some(function(node) {
31                 this.listView = this.createListView(node);
32                 this.view.find('.list').append(this.listView.dom);
33             }.bind(this));
34     },
35     getVerticallyFirstTextElement: function() {
36         var toret;
37         this.containers.some(function(container) {
38             toret = container.getVerticallyFirstTextElement();
39             return !!toret;
40         });
41         return toret;
42     },
43     onNodeAdded: function(event) {
44         var node = event.meta.node;
45         if(this.listView.listNode.sameNode(node.parent()) && node.is('item.answer')) {
46             this.listView.addItem(node);
47         }
48     },
49     onNodeDetached: function(event) {
50         var node = event.meta.node;
51         if(this.listView.listNode.sameNode(event.meta.parent) && node.is('item.answer')) {
52             this.listView.removeItem(node);
53         }
54     }
55 });
56
57 return choiceBase;
58
59 });
60
61