Browse Source

Start splitting JS up into smaller chunks

master
Chris Smith 12 years ago
parent
commit
6e5788abed
1 changed files with 139 additions and 120 deletions
  1. 139
    120
      res/js/viewer.js

+ 139
- 120
res/js/viewer.js View File

@@ -1,4 +1,143 @@
1 1
 $(function() {
2
+
3
+ // Instructions
4
+ (function() {
5
+  var cookieName = 'hideinstructions';
6
+
7
+  function showInstructions() {
8
+   $.cookie(cookieName, null);
9
+   $('#instructions').show();
10
+   $('#showinstructions').hide();
11
+  }
12
+
13
+  function hideInstructions() {
14
+   $.cookie(cookieName, 1, { expires: 365 });
15
+   $('#instructions').hide();
16
+   $('#showinstructions').show();
17
+  }
18
+
19
+  $('#hideinstructions').click(hideInstructions);
20
+  $('#showinstructions').click(showInstructions);
21
+
22
+  if ($.cookie(cookieName)) {
23
+   hideInstructions();
24
+  }
25
+ })();
26
+
27
+ // Saving
28
+ (function() {
29
+  function saveLayout() {
30
+   _gaq.push(['_trackEvent', 'General', 'Save']);
31
+
32
+   layout.notes = $('#notecontent').val();
33
+
34
+   $('#save_inprogress').show();
35
+   $('#save_done').hide();
36
+   $('#savecontainer').show();
37
+   $('#save_error').hide();
38
+
39
+   $.ajax({
40
+    type: 'POST',
41
+    url: 'res/data/layouts/new',
42
+    data: {layout: JSON.stringify(layout)},
43
+    success: function(res) {
44
+     window.location.hash = res;
45
+     var url = window.location.href;
46
+     $('#link').children().remove();
47
+     $('<a>').attr('href', url).text(url).appendTo($('#link'));
48
+     $('#save_inprogress').hide();
49
+     $('#save_done').show();
50
+    },
51
+    error: function(xhr, status, error) {
52
+     $('#save_error').text('Save failed! Server said: ' + error).show();
53
+    }
54
+   });
55
+  }
56
+
57
+  function closeSave() {
58
+   $('#savecontainer').hide();
59
+  }
60
+
61
+  $('#savelayout').click(saveLayout);
62
+  $('#savemask').click(closeSave);
63
+  $('#saveclose').click(closeSave);
64
+ })();
65
+
66
+ // Layout picker
67
+ (function() {
68
+  $.each(levels, function(key) {
69
+   var name = this.name;
70
+
71
+   $('<button>')
72
+    .append($('<img>').attr('src', this.image))
73
+    .append($('<p>').text(name))
74
+    .click(function() {
75
+     window.location.hash = '';
76
+     _gaq.push(['_trackEvent', 'Level picker', 'Picked', name]);
77
+     showBlankLayout(key + 1);
78
+     closePicker();
79
+    })
80
+    .appendTo($('#layoutpicker .container'));
81
+  });
82
+
83
+  function showPicker() {
84
+   _gaq.push(['_trackEvent', 'Level picker', 'Shown']);
85
+   $('#layoutcontainer').show();
86
+  }
87
+
88
+  function closePicker() {
89
+   $('#layoutcontainer').hide();
90
+  }
91
+
92
+  $('#createlayout').click(showPicker);
93
+  $('#layoutmask').click(closePicker);
94
+  $('#layoutclose').click(closePicker);
95
+ })();
96
+
97
+ // Address management
98
+ (function() {
99
+  function updateFromHash() {
100
+   var id = window.location.hash;
101
+   if (id === '') {
102
+    showBlankLayout(1);
103
+   } else if (id.substr(0,7) == '#blank:') {
104
+    showBlankLayout(parseInt(id.substr(7)));
105
+   } else {
106
+    getLayout(id.substr(1));
107
+   }
108
+  }
109
+
110
+  $(window).bind('hashchange', updateFromHash);
111
+  updateFromHash();
112
+ })();
113
+
114
+ // Palette
115
+ (function() {
116
+  $.each(towers, function(key) {
117
+   createBaseElForTower(key, this).appendTo($('#palette'));
118
+  });
119
+
120
+  $('.tower,.core').draggable({
121
+   helper: 'clone',
122
+   containment: 'document',
123
+   stop: function(evt, ui) {
124
+    if (!$(this).data('type')) {
125
+     return;
126
+    }
127
+
128
+    var tower = {
129
+     type: $(this).data('type'),
130
+     rotation: 0,
131
+     position: adjustMapOffset(ui.helper.offsetFrom('#mapcontainer'), thisLevel, 1)
132
+    };
133
+
134
+    layout.towers.push(tower);
135
+    createElForTower(tower).appendTo($('#mapcontainer'));
136
+    updateDefenseUnits();
137
+   }
138
+  });
139
+ })();
140
+
2 141
  var thisLevel;
3 142
  var layout;
4 143
 
@@ -124,30 +263,6 @@ $(function() {
124 263
   return res;
125 264
  }
126 265
 
127
- $.each(towers, function(key) {
128
-  createBaseElForTower(key, this).appendTo($('#palette'));
129
- });
130
-
131
- $('.tower,.core').draggable({
132
-  helper: 'clone',
133
-  containment: 'document',
134
-  stop: function(evt, ui) {
135
-   if (!$(this).data('type')) {
136
-    return;
137
-   }
138
-
139
-   var tower = {
140
-    type: $(this).data('type'),
141
-    rotation: 0,
142
-    position: adjustMapOffset(ui.helper.offsetFrom('#mapcontainer'), thisLevel, 1)
143
-   };
144
-
145
-   layout.towers.push(tower);
146
-   createElForTower(tower).appendTo($('#mapcontainer'));
147
-   updateDefenseUnits();
148
-  }
149
- });
150
-
151 266
  function clearLayout() {
152 267
   $('#mapcontainer .tower').remove();
153 268
   if (layout) {
@@ -159,69 +274,6 @@ $(function() {
159 274
   $('#mapcontainer .core').remove();
160 275
  }
161 276
 
162
- $.each(levels, function(key) {
163
-  var name = this.name;
164
-
165
-  $('<button>')
166
-   .append($('<img>').attr('src', this.image))
167
-   .append($('<p>').text(name))
168
-   .click(function() {
169
-    window.location.hash = '';
170
-    _gaq.push(['_trackEvent', 'Level picker', 'Picked', name]);
171
-    showBlankLayout(key + 1);
172
-    closePicker();
173
-   })
174
-   .appendTo($('#layoutpicker .container'));
175
- });
176
-
177
- function showPicker() {
178
-  _gaq.push(['_trackEvent', 'Level picker', 'Shown']);
179
-  $('#layoutcontainer').show();
180
- }
181
-
182
- function closePicker() {
183
-  $('#layoutcontainer').hide();
184
- }
185
-
186
- function saveLayout() {
187
-  _gaq.push(['_trackEvent', 'General', 'Save']);
188
-
189
-  layout.notes = $('#notecontent').val();
190
-
191
-  $('#save_inprogress').show();
192
-  $('#save_done').hide();
193
-  $('#savecontainer').show();
194
-  $('#save_error').hide();
195
-
196
-  $.ajax({
197
-   type: 'POST',
198
-   url: 'res/data/layouts/new',
199
-   data: {layout: JSON.stringify(layout)},
200
-   success: function(res) {
201
-    window.location.hash = res;
202
-    var url = window.location.href;
203
-    $('#link').children().remove();
204
-    $('<a>').attr('href', url).text(url).appendTo($('#link'));
205
-    $('#save_inprogress').hide();
206
-    $('#save_done').show();
207
-   },
208
-   error: function(xhr, status, error) {
209
-    $('#save_error').text('Save failed! Server said: ' + error).show();
210
-   }
211
-  });
212
- }
213
-
214
- function closeSave() {
215
-  $('#savecontainer').hide();
216
- }
217
-
218
- $('#createlayout').click(showPicker);
219
- $('#layoutmask').click(closePicker);
220
- $('#layoutclose').click(closePicker);
221
- $('#savelayout').click(saveLayout);
222
- $('#savemask').click(closeSave);
223
- $('#saveclose').click(closeSave);
224
-
225 277
  function updateLayout(data) {
226 278
   clearLayout();
227 279
   clearCores();
@@ -256,37 +308,4 @@ $(function() {
256 308
   _gaq.push(['_trackPageview', '/view/blank:' + id]);
257 309
   updateLayout({level: id, towers:[]});
258 310
  }
259
-
260
- function updateFromHash() {
261
-  var id = window.location.hash;
262
-  if (id === '') {
263
-   showBlankLayout(1);
264
-  } else if (id.substr(0,7) == '#blank:') {
265
-   showBlankLayout(parseInt(id.substr(7)));
266
-  } else {
267
-   getLayout(id.substr(1));
268
-  }
269
- }
270
-
271
- function showInstructions() {
272
-  $.cookie('hideinstructions', null);
273
-  $('#instructions').show();
274
-  $('#showinstructions').hide();
275
- }
276
-
277
- function hideInstructions() {
278
-  $.cookie('hideinstructions', 1, { expires: 365 });
279
-  $('#instructions').hide();
280
-  $('#showinstructions').show();
281
- }
282
-
283
- $('#hideinstructions').click(hideInstructions);
284
- $('#showinstructions').click(showInstructions);
285
-
286
- $(window).bind('hashchange', updateFromHash);
287
- updateFromHash();
288
-
289
- if ($.cookie('hideinstructions')) {
290
-  hideInstructions();
291
- }
292 311
 });

Loading…
Cancel
Save