Skip to content

Commit d276f56

Browse files
committed
Merge pull request CodiCamp#1 from An2kataluna/master
08.11.2015 Tasks
2 parents 3fb4f88 + 59e60d7 commit d276f56

File tree

9 files changed

+347
-55
lines changed

9 files changed

+347
-55
lines changed

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

Lines changed: 19 additions & 6 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
},
@@ -20,23 +21,35 @@ var Events = {
2021
* @param {Dom Object} el
2122
* @param {String} eventName
2223
* @param {Function} handler
24+
* @param {Object} params
2325
* returns void
2426
*/
2527
subscribe: function (el,eventName,handler) {
2628

2729
el.addEventListener(eventName,handler,false);
2830
},
2931

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+
3042
/***
3143
* Publishes an event to the listneres
3244
* @param {Dom Object} el
3345
* @param {String} eventName
46+
* @param {Object} params
3447
* returns void
3548
*/
36-
publish: function (el,eventName) {
49+
publish: function (el,eventName,params) {
3750

38-
var event = this.create(eventName);
39-
el.dispatchEvent(event);
51+
var evnt = this.create(eventName, params);
52+
el.dispatchEvent(evnt);
4053

4154
},
4255

@@ -50,4 +63,4 @@ var Events = {
5063
unsubscribe: function (el,eventName,handler) {
5164
el.removeEventListener(eventName,handler,false);
5265
}
53-
};
66+
};

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var app = app || {};
4747

4848
/***
4949
* Internal application events
50+
* @param {Object} params
5051
*/
5152
app.events = {
5253

@@ -55,9 +56,9 @@ var app = app || {};
5556
Events.subscribe(elements.el, eventName, handler);
5657
},
5758

58-
notify: function (eventName) {
59+
notify: function (eventName,params) {
5960

60-
Events.publish(elements.el, eventName);
61+
Events.publish(elements.el, eventName,params);
6162
}
6263
};
6364

@@ -80,7 +81,7 @@ var app = app || {};
8081

8182
for (var i = instanceStorage.length - 1; i >= 0; i--) {
8283
instanceStorage[i].destroy();
83-
};
84+
}
8485
};
8586

8687
/**
@@ -95,7 +96,7 @@ var app = app || {};
9596
name: templateName
9697
});
9798
}
98-
};
99+
}
99100

100101
/**
101102
* Get user specific configuration
@@ -114,4 +115,6 @@ var app = app || {};
114115
elements.el = doc.querySelector(config.el);
115116
}
116117

117-
})(window, document);
118+
app.init();
119+
120+
})(window, document);

EXERCISE/Exercise 4/app/templates/templates.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ var app = app || {};
99

1010
layout: {
1111
Content: '<div id="template-Content"></div>',
12-
Footer: '<nav class="menu" id="template-Footer"><ul><li class="start"><a href="javascript:;" class="icon-smile"></a></li><li class="window-tab"><a href="#" class="icon-browser"></a></li></ul></nav>'
12+
Footer: '<nav class="menu" id="template-Footer"><ul><li class="start"><a href="javascript:;" class="icon-smile"></a></li></ul></nav>'
1313
},
1414

15-
windowTemplate: ['<section class="window" id="test">',
15+
windowTemplate: [
1616
'<header class="header">',
1717
'<div class="window-actions">',
1818
'<span class="action icon-dash"></span>',
@@ -31,7 +31,8 @@ var app = app || {};
3131
'</div>',
3232
'<footer class="footer">',
3333
'<strong> Status bar :) </strong>',
34-
'</footer>',
35-
'</section>']
34+
'</footer>'],
35+
36+
footerTemplate:'<a href="#" class="icon-browser"></a>'
3637
};
37-
})();
38+
})();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
this.el = document.getElementById('template-' + this.name);
2929

3030
if(this.selectors) {
31-
31+
// in contentView selectors is undefined; in footer view we have createNewWindow and destroyWindow as selectors
3232
for(var selector in this.selectors) {
3333
this.elements[selector] = document.querySelector(this.selectors[selector]);
3434
}
@@ -58,4 +58,4 @@
5858
}
5959
};
6060

61-
})(window);
61+
})(window);

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

Lines changed: 25 additions & 7 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,13 +24,30 @@ var app = app || {};
2324

2425
app.ContentView.createNewWindow = function () {
2526
console.log(2);
26-
app.windowInstances.push(new global.windowView({
27-
/***
28-
* TO DO Homework: create UNIQUE ID
29-
*/
30-
id: app.windowInstances.length
31-
}));
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);
35+
};
36+
37+
// app.ContentView.selectors={
38+
// destroyWindow: '.icon-delete-circle'
39+
// }
40+
41+
app.ContentView.destroyWindow = function (evnt) {
42+
console.log(0);
43+
// this.parentElement.removeChild(this[i]);
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+
3250
};
3351

3452

35-
})(window, app);
53+
})(window, app);

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

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,107 @@ 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 windowIcon 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.iconToHighlight = this.iconList.querySelector(this.selectors.windowIcon);
27+
this.elements.iconToHighlight =this.selectors.windowIcon;
28+
29+
this.events.on.call(this);
30+
31+
};
32+
1333
/***
1434
* Bind event listeners to view elements
1535
*/
1636
app.FooterView.events = {
1737
on: function () {
1838

19-
Events.subscribe(this.elements.createNewWindow, 'click', this.createNewWindow);
39+
Events.subscribe(this.elements.createNewWindow, 'click', this.createNewWindow.bind(this));
40+
// Events.subscribe(this.elements.iconToHighlight, 'click', this.iconHighlight.bind(this));
41+
app.events.listen('app:window:destroy', this.destroyWindow.bind(this));
42+
app.events.listen('app:window:created', this.createNewIcon.bind(this));
43+
44+
// Events.subscribe(this.iconToHighlight,'click', this.iconHighlight.bind(this));
45+
},
46+
47+
off: function () {
48+
2049
}
2150
};
2251

2352
/***
2453
* Create new Windown object
2554
*/
2655
app.FooterView.createNewWindow = function () {
56+
// app.FooterView.el.firstChild.innerHTML += app.templates.footerTemplate;
57+
app.events.notify('app:window:create');
58+
59+
};
60+
61+
/***
62+
* Creates a new icon in footer
63+
* @param {EventObject} evnt
64+
* @return void
65+
*/
66+
app.FooterView.createNewIcon = function(evnt){
67+
var wrapper=document.createElement('li');
68+
wrapper.className='window-tab';
69+
wrapper.id = evnt.detail.id;
70+
wrapper.innerHTML=app.templates.footerTemplate;
71+
var iconcolor = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
72+
// function GetRandomColor() {
73+
// var letters = '0123456789ABCDEF'.split('');
74+
// var color = '#';
75+
// for (var i = 0; i < 6; i++ ) {
76+
// color += letters[Math.floor(Math.random() * 16)];
77+
// }
78+
// return color;
79+
// }
80+
// wrapper.firstChild.style.color = GetRandomColor();
81+
wrapper.firstChild.style.color = iconcolor;
82+
this.iconList.appendChild(wrapper);
83+
};
84+
85+
app.FooterView.destroyWindow = function (evnt) {
86+
2787
/***
28-
* TO DO HOMEWORK: add icon for each new window
88+
* Need to add code to remove icon
2989
*/
30-
app.events.notify('app:window:create');
90+
var iconToRemove = this.iconList.querySelector('#'+evnt.detail.id);
91+
92+
iconToRemove.parentNode.removeChild(iconToRemove);
93+
94+
};
95+
96+
app.FooterView.iconHighlight = function () {
97+
this.iconToHighlight.classList.add('iconhighlight');
98+
99+
app.events.notify('app:footericon:highlighted',{ id: this.iconList.id });
31100
};
32101

102+
33103
/***
34104
* Store cached elements
35105
*/
36106
app.FooterView.elements = {
37-
38107
};
39108
/***
40109
* Elements selectors
41110
*/
42111
app.FooterView.selectors = {
43-
createNewWindow: '.icon-smile'
112+
createNewWindow: '.icon-smile',
113+
windowIcon: '.window-tab'
44114
};
45-
})(window,app);
115+
116+
})(window,app);

0 commit comments

Comments
 (0)