This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
/
backpack.badgeCollection.controller.js
executable file
·113 lines (97 loc) · 2.78 KB
/
backpack.badgeCollection.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var BadgeCollection = function() {
this.setModel(new BadgeCollectionModel());
};
/**
* Group listing object representation - 'copy' constructor
*
* @param {HTMLElement} element that acts as construction template
*/
BadgeCollection.fromElement = function (element) {
var $el = $(element);
// set up a new collection
var collection = new BadgeCollection();
// find all badges that we need to boostrap, too
var badges = element.find(".badge");
// replace the collection with the backbone'd collection
var $newEl = collection.currentView.$el;
$el.replaceWith($newEl);
// if more than 0 badges, create badges inside
// this collection.
if(badges.length>0) {
badges.each(function() {
var badge = collection.createBadgeFromElement(this);
collection.addBadge(badge);
});
}
collection.currentView.selectSet(1);
// done
return collection;
};
/**
* Group listing object representation - prototype
*/
BadgeCollection.prototype = {
model: null,
views: null,
currentView: null,
/**
* Set the model for this group
*/
setModel: function(BadgeCollectionModel) {
this.model = BadgeCollectionModel;
BadgeCollectionModel.controller = this;
this.views = {
listing: new BadgeCollectionView({model: BadgeCollectionModel})
};
this.views.listing.controller = this;
this.currentView = this.views.listing;
this.render();
},
/**
* Render this listing (delegated to current view)
*/
render: function() {
return this.currentView.render(this.model);
},
/**
* Create a badge and add it to the list of known badges.
*/
createBadge: function(a,b,c,d) {
var badge;
if(a && b && c && d) { badge = new Badge(a,b,c,d); }
else { badge = new Badge(); }
badge.owner = this;
this.model.addBadge(badge);
return badge;
},
/**
* Bootstrap a badge and add it to the list of known badges.
*/
createBadgeFromElement: function(element) {
var badge = Badge.fromElement(element);
badge.owner = this;
this.model.addBadge(badge);
return badge;
},
/**
* Add a badge to this collection
*/
addBadge: function(badge) {
this.currentView.addBadgeElement(badge.currentView.$el);
},
/**
* Add badge 'add to group' buttons (delegated to view)
*/
addBadgePickers: function(group) {
var badgeArea = group.currentView.$el.find(".badgeArea");
this.currentView.addBadgePickers(group);
},
/**
* Remove badge 'add to group' buttons (delegated to view)
*/
removeBadgePickers: function() {
this.currentView.removeBadgePickers();
}
};
// ensure correct constructor identifier
BadgeCollection.prototype.constructor = BadgeCollection;