forked from weaverryan/php-js-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-05.html
218 lines (185 loc) · 7.95 KB
/
index-05.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>php|tek 2012 - Writing bad JavaScript with Mario!</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="ryan@knplabs.com">
<!-- Le styles -->
<link href="css/bootstrap.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="js/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">php|tek JavaScript Playground</a>
<div class="nav-collapse">
<ul class="nav">
<li><a href="index.html">Home</a></li>
<li><a href="http://knpuniversity.com">KnpUniversity.com (shameless plug)</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="span12">
<h1>5 - jQuery extends</h1>
<div class="mario-world row-fluid">
<div class="span8">
<div class="mario box">
.mario
<div class="luigi box">
.luigi
<div class="kick-butt">
<img src="images/luigi.gif" alt="Luigi, kicking ass" class="luigi-himself" />
<img src="images/fireball.gif" alt="fireball!" class="fireball" />
<img src="images/goomba.gif" alt="Enemy" class="enemy" />
<img src="images/bam.gif" alt="BAM!" class="bam-explode" />
</div>
<a href="#" class="peach box">
.peach
</a>
</div>
</div>
</div>
<div class="span4 log"></div>
</div>
</div>
</div>
<div class="alert alert-info">
<i class="icon-info-sign"></i>
Find the code! View source or go to:
<a href="https://github.com/weaverryan/php-js-playground/tree/05-extends">https://github.com/weaverryan/php-js-playground/tree/05-extends</a>
</div>
<div class="pagination">
<ul>
<li><a href="index-04.html">Prev</a></li>
<li><a href="index-06.html">Next</a></li>
</ul>
</div>
</div> <!-- /container -->
<a href="https://github.com/weaverryan/php-js-playground"><img style="position: absolute; top: 0; right: 0; border: 0; z-index: 1050;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png" alt="Fork me on GitHub"></a>
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery.js"></script>
<script type="text/javascript">
// a global object we can use to store and namespace things
window.MagicBoxes = function($container) {
this.$el = $container;
this.logCount = 0;
this.initialize();
};
/*
* jQuery.extend merges the {} object into MagicBoxes.prototype
* this effectively "extends" it by adding methods to the prototype
* this is the same thing we had before, but a little cleaner, and more common
*
* This functions almost like an array_merge of 2 objects. In this case
* we're adding one object with some method (the {} guy) into the "prototype",
* which makes those methods available when we create an instance of that object
*/
$.extend(MagicBoxes.prototype, {
initialize: function() {
/*
* In both cases, the $.proxy ensures that "this" in the resulting
* function refers to this (e.g. MagicBoxes) instances.
*
* Without the $.proxy, "this" is the element that is responding
* to the jQuery event (i.e. event.currentTarget). We want to keep
* the scope in "this" object, so that we can continue calling other
* methods on our object.
*
* In PHP, this would be as if we could call a function on an object,
* but force $this to be something different inside that method:
*
* $foo = new Foo();
* $bar = new Bar();
*
* // this is totally made-up, just for analogy
* // call $foo->doThings(), but make $this actually be $bar in that method
* call_user_function_array(array($foo, 'doThings'), array(), $bar));
*/
// register the luigi click
this.$el.find('.luigi').click($.proxy(this._handleLuigiClick, this));
// register the wrapper logging click
this.$el.click($.proxy(this._handleEventLogging, this));
},
makeLuigiFight: function($luigi) {
// alternatively, we could say
// var $luigi = $(event.currentTarget);
$luigi.addClass('active');
var $fireball = $luigi.find('.fireball');
var $enemy = $luigi.find('.enemy');
var $bam = $luigi.find('.bam-explode');
$fireball.animate({
left: '+=320'
}, 1000, function() {
$fireball.hide();
$enemy.hide();
$bam.show();
setTimeout(function() {
$luigi.removeClass('active');
$fireball.css({left: '70px'});
$fireball.show();
$enemy.show();
$bam.hide();
}, 1000);
});
},
_handleLuigiClick: function(event) {
// *almost* the same as returning false, but better, for one,
// because it does not stop event propagation
event.preventDefault();
// use currentTarget, because it's the element that was actually attached to this event
this.makeLuigiFight($(event.currentTarget));
this.logEvent(event, 'Luigi click');
},
_handleEventLogging: function(event) {
// handles the click on the whole wrapper to log events
event.preventDefault();
this.logEvent(event, 'Generic box click');
},
/*
* Re-usable function to log details about an event
*/
logEvent: function(event, label) {
// print out some details about the event
var $log = this.$el.find('.log');
// the logCount is temporarily broken! See my note below
var html = '<span class="badge badge-info">'+this.logCount+'</span>';
html += '<h4>'+label+'</h4>'
html += '<span class="label">target:</span> .' + $(event.target).attr('class') + '<br/>';
html += '<span class="label">currentTarget:</span> .' + $(event.currentTarget).attr('class') + '<br/>';
html += '<span class="label">delegateTarget:</span> .' + $(event.delegateTarget).attr('class');
$log.prepend('<div class="log-row">'+html+'</div>');
// increase the local logCount variable
this.logCount++;
}
});
jQuery(document).ready(function() {
// initialize our little application
var magicBoxApp = new MagicBoxes($('.mario-world'));
});
</script>
</body>
</html>