ed897c0c97de5f338421d05726c2472317abc1bd
[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.itemViews = [];
19
20     this.listNode.contents()
21         .filter(function(node) {
22             return node.is('item.answer');
23         })
24         .forEach(function(node) {
25             this.addItem(node);
26         }.bind(this));
27 };
28 _.extend(ListView.prototype, Backbone.Events, {
29     addItem: function(node) {
30         var view = new ItemView(node, this);
31         var idx = this.listNode.contents()
32             .filter(function(n) { return n.is('item'); })
33             .indexOf(node);
34
35         if(idx <= this.itemViews.length - 1) {
36                 this.itemViews.splice(idx, 0, view);
37         } else {
38             this.itemViews.push(view);
39         }
40         if(idx > 0) {
41             this.itemViews[idx-1].dom.after(view.dom);
42         } else {
43             this.list.prepend(view.dom);
44         }
45         if(this.params.onItemViewAdded) {
46             this.params.onItemViewAdded(view);
47         }
48     },
49     removeItem: function(node) {
50         this.itemViews.some(function(view, idx) {
51             if(view.node.sameNode(node)) {
52                 view.remove();
53                 this.itemViews.splice(idx, 1);
54                 return true;
55             }
56         }.bind(this));
57     },
58     getItemView: function(node) {
59         var toret;
60         this.itemViews.some(function(view) {
61             if(view.node.sameNode(node)) {
62                 toret = view;
63                 return true;
64             }
65         }.bind(this));
66         return toret;
67     }
68 });
69
70 var ItemView = function(node, exerciseView) {
71     this.node = node;
72     this.exerciseView = exerciseView;
73     this.dom = $(_.template(viewItemTemplate)());
74
75     this.container = exerciseView.element.createContainer(node.contents(), {
76         resetBackground: true,
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 });