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');
20 this.listNode.contents()
21 .filter(function(node) {
22 return node.is('item.answer');
24 .forEach(function(node) {
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'); })
35 if(idx <= this.itemViews.length - 1) {
36 this.itemViews.splice(idx, 0, view);
38 this.itemViews.push(view);
41 this.itemViews[idx-1].dom.after(view.dom);
43 this.list.prepend(view.dom);
45 if(this.params.onItemViewAdded) {
46 this.params.onItemViewAdded(view);
49 removeItem: function(node) {
50 this.itemViews.some(function(view, idx) {
51 if(view.node.sameNode(node)) {
53 this.itemViews.splice(idx, 1);
58 getItemView: function(node) {
60 this.itemViews.some(function(view) {
61 if(view.node.sameNode(node)) {
70 var ItemView = function(node, exerciseView) {
72 this.exerciseView = exerciseView;
73 this.dom = $(_.template(viewItemTemplate)());
75 this.container = exerciseView.element.createContainer(node.contents(), {
76 resetBackground: true,
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;