annotate source/examples/counters.js @ 194:2f10061fec29 javascript-exercises

$ python py/remove_answers.py
author jf3243
date Mon, 21 Nov 2011 17:27:12 +0000
parents 5bec62c76e8b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
1 (function()
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
2 {
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
3
34
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
4 var _create_fn = function(){};
40
5def381f824e counters-example.rst: Drafted 'Onload'.
Jonathan Fine <jfine@pytex.org>
parents: 37
diff changeset
5 var create = function(parent){
34
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
6
40
5def381f824e counters-example.rst: Drafted 'Onload'.
Jonathan Fine <jfine@pytex.org>
parents: 37
diff changeset
7 _create_fn.prototype = parent;
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
8 var instance = new _create_fn();
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
9 return instance;
34
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
10 };
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
11
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
12 var global = (function(){return this;})();
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
13
34
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
14 var counter = {}; // Prototype object for Counter.
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
15
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
16 var Counter = function(){
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
17
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
18 var instance = create(counter);
34
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
19 instance.__init__.apply(instance, arguments);
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
20 return instance;
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
21 };
0314a8ce97cf counters.js: Started recoding as documented example file.
Jonathan Fine <jfine@pytex.org>
parents: 33
diff changeset
22
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
23 counter.__init__ = function(name){
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
24
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
25 this.name = name;
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
26 this.count = 0;
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
27 };
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
28
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
29 counter.onclick = function(event){
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
31 this.count ++;
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
32 };
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
33
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
34 counter.html = function(parent_id){
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
35
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
36 return this.name + ' ' + this.count;
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
37 };
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
38
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
39 global.onload = function(){
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
40
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
41 var models = [
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
42 Counter('apple'),
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
43 Counter('banana'),
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
44 Counter('cherry'),
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
45 Counter('date')
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
46 ];
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
47
42
0fd501cf5ada counter.js: Fixed silly typo that stopped file working.
Jonathan Fine <jfine@pytex.org>
parents: 40
diff changeset
48 var element = document.getElementById('example');
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
49
40
5def381f824e counters-example.rst: Drafted 'Onload'.
Jonathan Fine <jfine@pytex.org>
parents: 37
diff changeset
50 element.innerHTML = (
43
5c0b4e00b027 counters.css: Revised styles to make counters.html look better.
Jonathan Fine <jfine@pytex.org>
parents: 42
diff changeset
51 '<span id="a0">apple 0</span>'
5c0b4e00b027 counters.css: Revised styles to make counters.html look better.
Jonathan Fine <jfine@pytex.org>
parents: 42
diff changeset
52 + '<span id="a1">banana 0</span>'
5c0b4e00b027 counters.css: Revised styles to make counters.html look better.
Jonathan Fine <jfine@pytex.org>
parents: 42
diff changeset
53 + '<span id="a2">cherry 0</span>'
5c0b4e00b027 counters.css: Revised styles to make counters.html look better.
Jonathan Fine <jfine@pytex.org>
parents: 42
diff changeset
54 + '<span id="a3">date 0</span>'
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
55 );
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
56
40
5def381f824e counters-example.rst: Drafted 'Onload'.
Jonathan Fine <jfine@pytex.org>
parents: 37
diff changeset
57 element.onclick = onclick_factory(models);
5def381f824e counters-example.rst: Drafted 'Onload'.
Jonathan Fine <jfine@pytex.org>
parents: 37
diff changeset
58 element = undefined; // Avoid IE memory leak.
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
59 };
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
60
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
61 var onclick_factory = function(models){
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
62
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
63 var onclick = function(event){
59
5bec62c76e8b counters-discuss.rst: Added editorial changes and some additional content.
Jonathan Fine <jfine@pytex.org>
parents: 46
diff changeset
64
46
2f44deda0e32 counters.js: Modified to work with IE.
jfine
parents: 43
diff changeset
65 event = event || global.event; // For IE event handling.
2f44deda0e32 counters.js: Modified to work with IE.
jfine
parents: 43
diff changeset
66 var target = event.target || event.srcElement;
2f44deda0e32 counters.js: Modified to work with IE.
jfine
parents: 43
diff changeset
67 var id = target.id;
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
68 if (id) {
59
5bec62c76e8b counters-discuss.rst: Added editorial changes and some additional content.
Jonathan Fine <jfine@pytex.org>
parents: 46
diff changeset
69 var id_num = +id.slice(1);
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
70 var model = models[id_num];
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
71 model.onclick();
59
5bec62c76e8b counters-discuss.rst: Added editorial changes and some additional content.
Jonathan Fine <jfine@pytex.org>
parents: 46
diff changeset
72 var html = model.html(id);
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
73 if (html){
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
74 document.getElementById(id).innerHTML = html;
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
75 }
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
76 }
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
77 };
35
71d8deb7928f counters.js: Major rewrite. Close to being finished. Next, rst.
Jonathan Fine <jfine@pytex.org>
parents: 34
diff changeset
78 return onclick;
30
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
79 };
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
80
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
81 })();
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
82
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
83
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
84
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
85
dd3c26d29d6d example.js: Added content to this file, omitted in recent commit.
Jonathan Fine <jfine@pytex.org>
parents: 28
diff changeset
86