forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabs.html
147 lines (138 loc) · 3.26 KB
/
tabs.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<style type="text/css">
body {font-family: verdana}
tabs {
margin-top: 20px;
}
button {clear: both;}
tabs ul {
padding: 0px; margin: 0px;
}
tabs li {
float: left;
padding: 10px;
background-color: #F6F6F6;
list-style: none;
margin-left: 10px;
}
tabs li {
color: #1C94C4;
font-weight: bold;
text-decoration: none;
}
tabs li.active {
color: #F6A828;
cursor: default;
}
panel {
clear: both;
display: block;
}
/* clearfix from jQueryUI */
tabs ul:after { content: "."; display: block; height: 1px; clear: both; visibility: hidden; }
tabs ul { display: inline-block; }
</style>
<div id="demo-html">
<div id='out'></div>
<script id="app" type="text/mustache">
<p><button can-click="addVegies">Add Vegetables</button></p>
<tabs>
{{#each foodTypes}}
<panel title='{{title}}'>{{content}}</panel>
{{/each}}
</tabs>
</script>
</div>
<script src="../../lib/steal/steal.js"></script>
<script id="demo-source">
steal("can/component",function(){
can.Component.extend({
tag: "tabs",
template:
"<ul>"+
// Create an LI for each item in the panel's scope object
"{{#panels}}"+
"<li {{#isActive}}class='active'{{/isActive}} "+
"can-click='makeActive'>"+
"{{title}}"+
"</li>"+
"{{/panels}}"+
"</ul>"+
"<content></content>",
scope: {
// Contains a list of all panel scopes within the
// tabs element.
panels: [],
// When a `<panel>` element is inserted into the document,
// it calls this method to add the panel's scope to the
// panels array.
addPanel: function(panel){
// If this is the first panel, activate it.
if( this.attr("panels").length === 0 ) {
this.makeActive(panel)
}
this.attr("panels").push(panel);
},
// When a `<panel>` element is removed from the document,
// it calls this method to remove the panel's scope from
// the panels array.
removePanel: function(panel){
var panels = this.attr("panels");
can.batch.start();
panels.splice(panels.indexOf(panel),1);
// if the panel was active, make the first item active
if(panel === this.attr("active")){
if(panels.length){
this.makeActive(panels[0]);
} else {
this.removeAttr("active")
}
}
can.batch.stop()
},
makeActive: function(panel){
this.attr("active",panel);
this.attr("panels").each(function(panel){
panel.attr("active", false)
})
panel.attr("active",true);
},
// this is scope, not mustache
// consider removing scope as arg
isActive: function( panel ) {
return this.attr('active') == panel
}
}
});
can.Component.extend({
template: "{{#if active}}<content></content>{{/if}}",
tag:"panel",
scope: {
active: false,
title: "@"
},
events: {
inserted: function(){
this.element.parent().scope().addPanel( this.scope );
},
removed: function(){
this.element.parent().scope().removePanel( this.scope );
}
}
})
var foodTypes= new can.List([
{title: "Fruits", content: "oranges, apples"},
{title: "Breads", content: "pasta, cereal"},
{title: "Sweets", content: "ice cream, candy"}
])
window.foodTypes = foodTypes;
$("#out").html( can.view("app",{
foodTypes: foodTypes,
addVegies: function(){
foodTypes.push({
title: "Vegetables",
content: "Carrots, peas, kale"
})
}
}))
})
</script>