Skip to content

Commit f49e4de

Browse files
committed
A lot of things changed, 1) fix some stuff that didn't work(see last commit) + app.initApplication() works automatic in controller
1 parent 601dcb3 commit f49e4de

File tree

5 files changed

+74
-37
lines changed

5 files changed

+74
-37
lines changed

EXERCISE/Exercise 4/app/components/events.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ var Events = {
66
/***
77
* Create event
88
* @param {String} eventName
9+
* @param {Object} eventDetail
910
* returns {Event Object}
1011
*/
11-
create: function(eventName){
12+
create: function(eventName, eventDetail){
1213

13-
var event = new CustomEvent(eventName);
14+
var event = new CustomEvent(eventName, { detail: eventDetail });
1415

1516
return event;
1617
},
@@ -28,17 +29,27 @@ var Events = {
2829
el.addEventListener(eventName,handler,false);
2930
},
3031

32+
// elem.addEventListener(eventName, function(e) {
33+
// // in the event handler function here, you can directly refer
34+
// // to arg1 and arg2 from the parent function arguments
35+
// }, false);
36+
37+
// element.addEventListener('click', (function(passedInElement) {
38+
// return function(e) {func(e, passedInElement); };
39+
// }) (this.elements[i]), false);
40+
41+
3142
/***
3243
* Publishes an event to the listneres
3344
* @param {Dom Object} el
3445
* @param {String} eventName
3546
* @param {Object} params
3647
* returns void
3748
*/
38-
publish: function (el,eventName) {
49+
publish: function (el,eventName,params) {
3950

40-
var event = this.create(eventName);
41-
el.dispatchEvent(event);
51+
var evnt = this.create(eventName, params);
52+
el.dispatchEvent(evnt);
4253

4354
},
4455

EXERCISE/Exercise 4/app/controllers/controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var app = app || {};
5858

5959
notify: function (eventName,params) {
6060

61-
Events.publish(elements.el, eventName);
61+
Events.publish(elements.el, eventName,params);
6262
}
6363
};
6464

@@ -115,4 +115,6 @@ var app = app || {};
115115
elements.el = doc.querySelector(config.el);
116116
}
117117

118+
app.init();
119+
118120
})(window, document);

EXERCISE/Exercise 4/app/views/content-view.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var app = app || {};
1414
app.ContentView.events = {
1515
on: function () {
1616
app.events.listen('app:window:create', this.createNewWindow);
17+
app.events.listen('app:window:destroy', this.destroyWindow);
1718
},
1819

1920
off: function (argument) {
@@ -23,33 +24,29 @@ var app = app || {};
2324

2425
app.ContentView.createNewWindow = function () {
2526
console.log(2);
26-
app.windowInstances.push(global.WindowView.init({
27-
/***
28-
* TO DO Homework: create UNIQUE ID
29-
*/
30-
31-
/**
32-
* Creates a string that can be used for dynamic id attributes
33-
* Example: "id-wm68fu1uk63cjtt9"
34-
* @returns {string}
35-
*/
36-
// var uniqueId =
37-
38-
id: 'id-' + Math.random().toString(36).substr(2, 16)
39-
40-
// id: 'id' + (new Date()).getTime()
41-
// app.windowInstances.length
42-
}));
27+
var windowInstance = Object.create(global.WindowView);
28+
/**
29+
* Creates a string that can be used for dynamic id attributes
30+
* Example: "id-wm68fu1uk63cjtt9"
31+
* @returns {string}
32+
*/
33+
windowInstance.init({id: 'id-' + Math.random().toString(36).substr(2, 16)});
34+
app.windowInstances.push(windowInstance);
4335
};
4436

4537
// app.ContentView.selectors={
4638
// destroyWindow: '.icon-delete-circle'
4739
// }
4840

49-
app.ContentView.destroyWindow = function () {
41+
app.ContentView.destroyWindow = function (evnt) {
5042
console.log(0);
5143
// this.parentElement.removeChild(this[i]);
52-
app.windowInstances.pop();
44+
for (var i = app.windowInstances.length - 1; i >= 0; i--) {
45+
if(app.windowInstances[i].id === evnt.detail.id){
46+
app.windowInstances.splice(i,1);
47+
}
48+
}
49+
5350
};
5451

5552

EXERCISE/Exercise 4/app/views/footer-view.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ var app = app || {};
1010
*/
1111
app.FooterView = Object.create(global.BaseView);
1212

13+
app.FooterView.render= function () {
14+
this.placeholder.innerHTML = this.template;
15+
this.el = document.getElementById('template-' + this.name);
16+
17+
if(this.selectors) {
18+
// in contentView selectors is undefined; in footer view we have createNewWindow and destroyWindow as selectors
19+
for(var selector in this.selectors) {
20+
this.elements[selector] = document.querySelector(this.selectors[selector]);
21+
}
22+
}
23+
24+
this.iconList = this.el.firstChild;
25+
26+
this.events.on.call(this);
27+
28+
};
29+
1330
/***
1431
* Bind event listeners to view elements
1532
*/
@@ -18,6 +35,7 @@ var app = app || {};
1835

1936
Events.subscribe(this.elements.createNewWindow, 'click', this.createNewWindow.bind(this));
2037
app.events.listen('app:window:destroy', this.destroyWindow.bind(this));
38+
app.events.listen('app:window:created', this.createNewIcon.bind(this));
2139
},
2240

2341
off: function () {
@@ -29,19 +47,21 @@ var app = app || {};
2947
* Create new Windown object
3048
*/
3149
app.FooterView.createNewWindow = function () {
32-
/***
33-
* TO DO HOMEWORK: add icon for each new window
34-
*/
3550

36-
this.createNewIcon();
3751
// app.FooterView.el.firstChild.innerHTML += app.templates.footerTemplate;
3852
app.events.notify('app:window:create');
3953

4054
};
4155

42-
app.FooterView.createNewIcon = function(){
56+
/***
57+
* Creates a new icon in footer
58+
* @param {EventObject} evnt
59+
* @return void
60+
*/
61+
app.FooterView.createNewIcon = function(evnt){
4362
var wrapper=document.createElement('li');
4463
wrapper.className='window-tab';
64+
wrapper.id = evnt.detail.id;
4565
wrapper.innerHTML=app.templates.footerTemplate;
4666
var iconcolor = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
4767
// function GetRandomColor() {
@@ -54,14 +74,18 @@ var app = app || {};
5474
// }
5575
// wrapper.firstChild.style.color = GetRandomColor();
5676
wrapper.firstChild.style.color = iconcolor;
57-
this.el.firstChild.appendChild(wrapper);
77+
this.iconList.appendChild(wrapper);
5878
};
5979

60-
app.FooterView.destroyWindow = function () {
80+
app.FooterView.destroyWindow = function (evnt) {
81+
6182
/***
6283
* Need to add code to remove icon
6384
*/
64-
this.wrapper.parentNode.removeChild(this.wrapper);
85+
var iconToRemove = this.iconList.querySelector('#'+evnt.detail.id);
86+
87+
iconToRemove.parentNode.removeChild(iconToRemove);
88+
6589
};
6690

6791

EXERCISE/Exercise 4/app/views/window-view.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ var app = app || {};
2121

2222
this.render();
2323

24-
this.closeIcon=this.wrapper.querySelector('.icon-delete-circle');
25-
Events.subscribe(this.closeIcon, 'click', this.destroy.bind(this));
24+
app.events.notify('app:window:created',{id: this.id});
25+
2626
// app.events.listen('window:' + this.id + ':render', this.render.bind(this));
2727
},
2828

@@ -34,6 +34,9 @@ var app = app || {};
3434
this.wrapper.className='window';
3535
this.wrapper.innerHTML=this.template.join('');
3636
app.ContentView.el.appendChild(this.wrapper);
37+
this.closeIcon = this.wrapper.querySelector(this.selectors.closeIcon);
38+
39+
this.events.on.call(this);
3740
},
3841

3942
/***
@@ -49,7 +52,7 @@ var app = app || {};
4952
events: {
5053

5154
on: function () {
52-
55+
Events.subscribe(this.closeIcon, 'click', this.destroy.bind(this));
5356
},
5457

5558
off: function () {
@@ -58,7 +61,7 @@ var app = app || {};
5861
},
5962

6063
selectors: {
61-
64+
closeIcon:'.icon-delete-circle'
6265
}
6366
};
6467
})(window);

0 commit comments

Comments
 (0)