e3fbd8439ca503f5c506d760c1c4e6dd3264f951
[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         manages: function(node, originaParent) {
78             return this.node.sameNode(node.parent() || originaParent);
79         }.bind(this),
80         dom: this.dom.find('.content')
81     });
82 };
83
84 _.extend(ItemView.prototype, Backbone.Events, {
85     remove: function() {
86         this.container.remove();
87         this.dom.remove();
88     },
89     addPrefixView: function(view) {
90         this.dom.find('.prefix').append(view.dom);
91         this.prefixView = view;
92     }
93 });
94
95 return ListView;
96
97 });