Skip to content

Commit 47f760f

Browse files
committed
refactoring
1 parent 75f96c7 commit 47f760f

File tree

7 files changed

+115
-8
lines changed

7 files changed

+115
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ var app = app || {};
4343
app.events.listen('app:layout:cached', createLayout.bind(this));
4444
};
4545

46+
app.windowInstances = [];
47+
4648
/***
4749
* Internal application events
4850
*/

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var app = app || {};
88
app.templates = {
99

1010
layout: {
11-
footer: '<nav class="menu"><ul><li class="start"><a href="#" class="icon-smile"></a></li><li class="window-tab"><a href="#" class="icon-browser"></a></li></ul></nav>'
11+
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>'
1213
},
1314

1415
windowTemplate: ['<section class="window" id="test">',
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/***
2+
* Content view
3+
*/
4+
var app = app || {};
5+
6+
(function (global){
7+
8+
/***
9+
* @param {Object} options
10+
* returns void
11+
*/
12+
function contentView (options) {
13+
14+
this.template = options.template;
15+
this.name = options.name;
16+
this.placeholder = document.getElementById('placeholder-' + options.name);
17+
18+
19+
20+
/***
21+
* Bind event listeners to view elements
22+
*/
23+
this.bindEvents = function () {
24+
app.events.listen('app:window:create', this.createNewWindow);
25+
};
26+
27+
this.unbindEvents = function () {
28+
29+
};
30+
31+
/***
32+
* Create new Windown object
33+
*/
34+
this.createNewWindow = function () {
35+
console.log(2);
36+
app.windowInstances.push(new global.windowView({
37+
/***
38+
* TO DO Homework: create UNIQUE ID
39+
*/
40+
id: app.windowInstances.length
41+
}));
42+
};
43+
44+
45+
this.render = function () {
46+
47+
this.placeholder.innerHTML = this.template;
48+
this.el = document.getElementById('template-' + options.name);
49+
50+
this.bindEvents();
51+
};
52+
53+
this.destroy = function () {
54+
console.log('destroy', this.template);
55+
};
56+
57+
app.events.listen('view:' + this.name + ':render', this.render.bind(this));
58+
};
59+
60+
app.contentView = contentView;
61+
})(window);

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
var app = app || {};
55

6-
(function (){
6+
(function (global){
77

88
/***
99
* @param {Object} options
@@ -15,13 +15,51 @@ var app = app || {};
1515
this.name = options.name;
1616
this.placeholder = document.getElementById('placeholder-' + options.name);
1717

18+
/***
19+
* Elements selectors
20+
*/
21+
this.selectors = {
22+
createNewWindow: '.icon-smile'
23+
};
24+
25+
/***
26+
* Store cached elements
27+
*/
28+
this.elements = {
1829

30+
};
31+
32+
/***
33+
* Bind event listeners to view elements
34+
*/
35+
this.bindEvents = function () {
36+
37+
Events.subscribe(this.elements.createNewWindow, 'click', this.createNewWindow);
38+
};
39+
40+
this.unbindEvents = function () {
41+
42+
};
43+
44+
/***
45+
* Create new Windown object
46+
*/
47+
this.createNewWindow = function () {
48+
/***
49+
* TO DO HOMEWORK: add icon for each new window
50+
*/
51+
app.events.notify('app:window:create');
52+
};
1953

2054

2155
this.render = function () {
2256

2357
this.placeholder.innerHTML = this.template;
24-
this.el = document.getElementById('template-' + options.name)
58+
this.el = document.getElementById('template-' + options.name);
59+
60+
this.elements.createNewWindow = this.el.querySelector(this.selectors.createNewWindow);
61+
62+
this.bindEvents();
2563
};
2664

2765
this.destroy = function () {
@@ -32,4 +70,4 @@ var app = app || {};
3270
};
3371

3472
app.footerView = footerView;
35-
})();
73+
})(window);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ var app = app || {};
1414
//Execpional template or default template
1515
this.template = options.template || app.templates.windowTemplate;
1616
this.id = options.id;
17-
this.type = options.type; //minimized or maximized
17+
this.type = options.type || 'maximized'; //minimized or maximized
1818

1919
this.render = function () {
2020

21-
app.layout.content.el.innerHTML += this.template;
21+
app.layout.content.el.innerHTML += this.template.join('');
2222
};
2323

2424
this.destroy = function () {
2525
console.log('destroy', this.template);
2626
};
2727

28-
app.events.listen('window:' + this.id + ':render', this.render.bind(this));
28+
this.render();
29+
// app.events.listen('window:' + this.id + ':render', this.render.bind(this));
2930
};
3031

3132
window.windowView = windowView;

EXERCISE/Exercise 4/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<script type="text/javascript" src="app/components/events.js"></script>
2323
<script type="text/javascript" src="app/templates/templates.js"></script>
2424
<script type="text/javascript" src="app/views/footer-view.js"></script>
25+
<script type="text/javascript" src="app/views/content-view.js"></script>
2526
<script type="text/javascript" src="app/views/window-view.js"></script>
2627
<script type="text/javascript" src="app/controllers/controller.js"></script>
2728

EXERCISE/Exercise 4/styles/style.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ body {
3838
width: 40px;
3939
height: 100%;
4040
text-align: center;
41-
padding: 14px 10px 0 10px;
41+
/*padding: 14px 10px 0 10px;*/
4242
background-color: #ecf0f1;
4343
border-right: 1px solid #ccc;
4444
}
@@ -56,6 +56,9 @@ body {
5656
.wrapper .menu li a {
5757
font-size: 1.3em;
5858
color: #31535c;
59+
display: inline-block;
60+
width: 100%;
61+
line-height: 40px;
5962
}
6063

6164
.window {

0 commit comments

Comments
 (0)