summaryrefslogtreecommitdiff
path: root/docroot/classes/utility.js
diff options
context:
space:
mode:
Diffstat (limited to 'docroot/classes/utility.js')
-rwxr-xr-xdocroot/classes/utility.js199
1 files changed, 199 insertions, 0 deletions
diff --git a/docroot/classes/utility.js b/docroot/classes/utility.js
new file mode 100755
index 0000000..218a059
--- /dev/null
+++ b/docroot/classes/utility.js
@@ -0,0 +1,199 @@
1/*
2 * Material Experience - Utility Functions
3 *
4 * EYEMG - Interactive Media Group
5 * Created by Mike Crute (mcrute@eyemg.com)
6 * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07
7 *
8 * Various utility functions that do not natively exist in any of
9 * the libraries we use.
10 *
11 * This is also the zoo for monkey patches. Be careful.
12 */
13
14Object.extend(window,
15{
16 /*
17 * Calculates the top and left offsets to center an element within the
18 * page. Every browser seems to approach this in a different fashion but
19 * this function works well on FX, OP, WK, and IE.
20 */
21 calcCordsToCenter: function(height, width)
22 {
23 var windowSize = window.getDimensions();
24
25 return {
26 top: Math.round(((windowSize.height - height) / 2)),
27 left: Math.round(((windowSize.width - width) / 2))
28 };
29 },
30
31 /*
32 * Gets the dimensions of the current window accounting for variations
33 * in browsers. Supports IE in either quirks or strict mode.
34 */
35 getDimensions: function()
36 {
37 var windowSize = {
38 height: document.documentElement.clientHeight,
39 width: document.documentElement.clientWidth
40 };
41
42 if (Prototype.Browser.Opera)
43 {
44 windowSize = {
45 height: document.body.clientHeight,
46 width: document.body.clientWidth
47 };
48 }
49
50 if (Prototype.Browser.WebKit)
51 {
52 windowSize = {
53 height: window.innerHeight,
54 width: window.innerWidth
55 };
56 }
57
58 return windowSize;
59 },
60
61 /*
62 * Verify the screen size is great than a set minimum or warn the user.
63 * Probably superfluous but why not.
64 */
65 checkResolution: function()
66 {
67 var size = window.getDimensions();
68
69 if (
70 size.height < SME.sizes.windMin.height ||
71 size.width < SME.sizes.windMin.width
72 )
73 {
74 new Bezel({ displayTime: 2 }).show(Strings.windowSize);
75 Event.stopObserving(window, 'resize', window.checkResolution);
76 }
77 }
78});
79
80Object.extend(String.prototype,
81{
82 /*
83 * Cleans up sloppy Compose JSON output by removing comments, newlines,
84 * and tabs.
85 */
86 cleanJSON: function()
87 {
88 var data = this;
89 data = data.replace(/^\/\/.*/gm,'');
90 data = data.replace(/(\n|\t|\r)/g,'');
91
92 return data;
93 }
94});
95
96Object.extend(Ajax.Request.prototype,
97{
98 /*
99 * Forces IE to enqueue XHR requests otherwise it gets bogged down and
100 * freezes the interface.
101 */
102 initialize: function(url, options)
103 {
104 this.transport = Ajax.getTransport();
105 this.setOptions(options);
106 this.url = url;
107
108 if (Prototype.Browser.IE)
109 {
110 Ajax.requestQueue.push(this);
111 Ajax._runQueue();
112 }
113 else
114 {
115 this.request(url);
116 }
117 },
118
119 /*
120 * Monkey patch prototype so it stops auto-evaluating the returned JSON
121 * data. Our JSON data is simply not clean enough for that and it throws
122 * errors all over the place.
123 *
124 * I've made as few modifications from the original as possible to ease
125 * in porting forward changes made in Prototype.
126 */
127 respondToReadyState: function(readyState)
128 {
129 var state = Ajax.Request.Events[readyState];
130 var transport = this.transport;
131
132 if (state == 'Complete')
133 {
134 try
135 {
136 this._complete = true;
137 (this.options['on' + this.transport.status]
138 || this.options['on' + (this.success() ? 'Success' : 'Failure')]
139 || Prototype.emptyFunction)(transport);
140 }
141 catch (e)
142 {
143 this.dispatchException(e);
144 }
145 }
146
147 try
148 {
149 (this.options['on' + state] || Prototype.emptyFunction)(transport);
150 Ajax.Responders.dispatch('on' + state, this, transport);
151 }
152 catch (e)
153 {
154 this.dispatchException(e);
155 }
156
157 if (state == 'Complete')
158 {
159 // avoid memory leak in MSIE: clean up
160 this.transport.onreadystatechange = Prototype.emptyFunction;
161 }
162 }
163});
164
165/*
166 * Implement AJAX request queueing. IE does not handle AJAX request concurrency
167 * very well and will freeze the entire browser interface if more than 2 calls
168 * are queued at a single time. We implement a global request queue and a queue
169 * runner that handles concurrent AJAX requests. The code is pretty straight
170 * forward but it also depends on modifications made to Ajax.Request.prototype.
171 */
172Object.extend(Ajax,
173{
174 // Global AJAX Request Queue
175 requestQueue: [],
176
177
178 /*
179 * Process events in the request queue.
180 */
181 _runQueue: function()
182 {
183 if (this.activeRequestCount > 1)
184 {
185 // Too many requests so we will try again later
186 new PeriodicalExecuter(function(executer)
187 {
188 this._runQueue();
189 executer.stop();
190 }.bind(this), 0.1);
191
192 return;
193 }
194
195 // Run the first request in the queue
196 var currentRequest = this.requestQueue.shift();
197 currentRequest.request(currentRequest.url);
198 }
199}); \ No newline at end of file