917471046a6ed48d174115ef69f94b37c4592a16
[fnpeditor.git] / src / editor / plugins / core / edumed / choice / list.js
1 define(function(require) {
2     
3 'use strict';
4 var $ = require('libs/jquery');
5
6 var _ = require('libs/underscore'),
7     Backbone = require('libs/backbone'),
8     viewTemplate = require('libs/text!./list.html'),
9     viewItemTemplate = require('libs/text!./listItem.html');
10
11
12 var ListView = function(element, listNode, params) {
13     this.element = element;
14     this.listNode = listNode;
15     this.params = params;
16     this.dom = $(_.template(viewTemplate)());
17     this.list = this.dom.find('ul');
18     this.addButton = this.dom.find('button.add');
19     this.itemViews = [];
20
21     this.listNode.contents()
22         .filter(function(node) {
23             return node.is('item.answer');
24         })
25         .forEach(function(node) {
26             this.addItem(node);
27         }.bind(this));
28 };
29 _.extend(ListView.prototype, Backbone.Events, {
30     addItem: function(node) {
31         var view = new ItemView(node, this);
32         var idx = this.listNode.contents()
33             .filter(function(n) { return n.is('item'); })
34             .indexOf(node);
35
36         if(idx <= this.itemViews.length - 1) {
37                 this.itemViews.splice(idx, 0, view);
38         } else {
39             this.itemViews.push(view);
40         }
41         if(idx > 0) {
42             this.itemViews[idx-1].dom.after(view.dom);
43         } else {
44             this.list.prepend(view.dom);
45         }
46         if(this.params.onItemViewAdded) {
47             this.params.onItemViewAdded(view);
48         }
49     },
50     removeItem: function(node) {
51         this.itemViews.some(function(view, idx) {
52             if(view.node.sameNode(node)) {
53                 view.remove();
54                 this.itemViews.splice(idx, 1);
55                 return true;
56             }
57         }.bind(this));
58     },
59     getItemView: function(node) {
60         var toret;
61         this.itemViews.some(function(view) {
62             if(view.node.sameNode(node)) {
63                 toret = view;
64                 return true;
65             }
66         }.bind(this));
67         return toret;
68     }
69 });
70
71 var ItemView = function(node, exerciseView) {
72     this.node = node;
73     this.exerciseView = exerciseView;
74     this.dom = $(_.template(viewItemTemplate)());
75
76     this.container = exerciseView.element.createContainer(node.contents(), {
77         resetBackground: true,
78         manages: function(node, originaParent) {
79             return this.node.sameNode(node.parent() || originaParent);
80         }.bind(this),
81         dom: this.dom.find('.content')
82     });
83 };
84
85 _.extend(ItemView.prototype, Backbone.Events, {
86     remove: function() {
87         this.container.remove();
88         this.dom.remove();
89     },
90     addPrefixView: function(view) {
91         this.dom.find('.prefix').append(view.dom);
92         this.prefixView = view;
93     }
94 });
95
96 return ListView;
97
98 });