1 define(function(require) {
4 var $ = require('libs/jquery');
6 var _ = require('libs/underscore'),
7 Backbone = require('libs/backbone'),
8 viewTemplate = require('libs/text!./list.html'),
9 viewItemTemplate = require('libs/text!./listItem.html');
12 var ListView = function(element, listNode, params) {
13 this.element = element;
14 this.listNode = listNode;
16 this.dom = $(_.template(viewTemplate)());
17 this.list = this.dom.find('ul');
18 this.addButton = this.dom.find('button.add');
21 this.listNode.contents()
22 .filter(function(node) {
23 return node.is('item.answer');
25 .forEach(function(node) {
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'); })
36 if(idx <= this.itemViews.length - 1) {
37 this.itemViews.splice(idx, 0, view);
39 this.itemViews.push(view);
42 this.itemViews[idx-1].dom.after(view.dom);
44 this.list.prepend(view.dom);
46 if(this.params.onItemViewAdded) {
47 this.params.onItemViewAdded(view);
50 removeItem: function(node) {
51 this.itemViews.some(function(view, idx) {
52 if(view.node.sameNode(node)) {
54 this.itemViews.splice(idx, 1);
59 getItemView: function(node) {
61 this.itemViews.some(function(view) {
62 if(view.node.sameNode(node)) {
71 var ItemView = function(node, exerciseView) {
73 this.exerciseView = exerciseView;
74 this.dom = $(_.template(viewItemTemplate)());
76 this.container = exerciseView.element.createContainer(node.contents(), {
77 manages: function(node, originaParent) {
78 return this.node.sameNode(node.parent() || originaParent);
80 dom: this.dom.find('.content')
84 _.extend(ItemView.prototype, Backbone.Events, {
86 this.container.remove();
89 addPrefixView: function(view) {
90 this.dom.find('.prefix').append(view.dom);
91 this.prefixView = view;