1 | $(function() { |
---|
2 | |
---|
3 | jQuery.extend({ |
---|
4 | getScript: function(url, callback) { |
---|
5 | var d = $.Deferred(); |
---|
6 | if (callback) |
---|
7 | d.done(callback); |
---|
8 | var head = document.getElementsByTagName("head")[0]; |
---|
9 | var script = document.createElement("script"); |
---|
10 | script.src = url; |
---|
11 | |
---|
12 | // Handle Script loading |
---|
13 | { |
---|
14 | var done = false; |
---|
15 | |
---|
16 | // Attach handlers for all browsers |
---|
17 | script.onload = script.onreadystatechange = function(){ |
---|
18 | if ( !done && (!this.readyState || |
---|
19 | this.readyState == "loaded" || this.readyState == "complete") ) { |
---|
20 | done = true; |
---|
21 | d.resolve(); |
---|
22 | |
---|
23 | // Handle memory leak in IE |
---|
24 | script.onload = script.onreadystatechange = null; |
---|
25 | } |
---|
26 | }; |
---|
27 | } |
---|
28 | |
---|
29 | head.appendChild(script); |
---|
30 | |
---|
31 | // We handle everything using the script element injection |
---|
32 | return d; |
---|
33 | } |
---|
34 | }); |
---|
35 | |
---|
36 | rft = { |
---|
37 | api: { |
---|
38 | post: function(action,args) { |
---|
39 | return $.ajax({ |
---|
40 | type: "POST", |
---|
41 | url: "api.php", |
---|
42 | contentType: "application/json", |
---|
43 | data: JSON.stringify({ |
---|
44 | uid:rft.auth.getUid(), |
---|
45 | action:action, |
---|
46 | args:args |
---|
47 | }) |
---|
48 | }); |
---|
49 | }, |
---|
50 | createObject: function(type, args) { |
---|
51 | var d = $.Deferred(); |
---|
52 | rft.api.post("createObject", |
---|
53 | { |
---|
54 | type: type, |
---|
55 | values: args |
---|
56 | }) |
---|
57 | .done(function(data, textStatus, jqXHR) { |
---|
58 | d.resolve(data.uid); |
---|
59 | }) |
---|
60 | .fail(function(jqXHR,textStatus,errorThrown){ |
---|
61 | d.reject(); |
---|
62 | }); |
---|
63 | return d.promise(); |
---|
64 | }, |
---|
65 | getObject: function(type, uid, success) { |
---|
66 | var d = $.Deferred(); |
---|
67 | rft.api.post("getObject", |
---|
68 | { |
---|
69 | type: type, |
---|
70 | uid: uid |
---|
71 | }) |
---|
72 | .done(function(data, textStatus, jqXHR) { |
---|
73 | d.resolve(data.object); |
---|
74 | }) |
---|
75 | .fail(function(jqXHR,textStatus,errorThrown){ |
---|
76 | d.reject(); |
---|
77 | }); |
---|
78 | return d.promise(); |
---|
79 | }, |
---|
80 | getObjects: function(type, predicates, success) { |
---|
81 | var d = $.Deferred(); |
---|
82 | rft.api.post("getObjects", |
---|
83 | { |
---|
84 | type: type, |
---|
85 | predicates: predicates |
---|
86 | }) |
---|
87 | .done(function(data, textStatus, jqXHR) { |
---|
88 | d.resolve(data.objects); |
---|
89 | }) |
---|
90 | .fail(function(jqXHR,textStatus,errorThrown){ |
---|
91 | d.reject(); |
---|
92 | }); |
---|
93 | return d.promise(); |
---|
94 | }, |
---|
95 | deleteObject: function(type, uid, success) { |
---|
96 | var d = $.Deferred(); |
---|
97 | d.reject(); |
---|
98 | return d.promise(); |
---|
99 | |
---|
100 | } |
---|
101 | }, |
---|
102 | auth: new (function() { |
---|
103 | var self = this; |
---|
104 | var currentUid = null; |
---|
105 | |
---|
106 | self.login = function(username,password,success,error) { |
---|
107 | var d = $.Deferred(); |
---|
108 | rft.api.post("login", |
---|
109 | { |
---|
110 | username:username, |
---|
111 | password:password |
---|
112 | }) |
---|
113 | .done(function(data, textStatus, jqXHR) { |
---|
114 | currentUid = data.uid; |
---|
115 | d.resolve(data.uid); |
---|
116 | }) |
---|
117 | .fail(function(jqXHR,textStatus,errorThrown){ |
---|
118 | d.reject(); |
---|
119 | }); |
---|
120 | return d.promise(); |
---|
121 | }; |
---|
122 | |
---|
123 | self.register = function(username,password,success,error) { |
---|
124 | var d = $.Deferred(); |
---|
125 | rft.api.post("register", |
---|
126 | { |
---|
127 | username:username, |
---|
128 | password:password |
---|
129 | }) |
---|
130 | .done(function(data, textStatus, jqXHR) { |
---|
131 | currentUid = data.uid; |
---|
132 | d.resolve(data.uid); |
---|
133 | }) |
---|
134 | .fail(function(jqXHR,textStatus,errorThrown){ |
---|
135 | d.reject(); |
---|
136 | }); |
---|
137 | return d.promise(); |
---|
138 | }; |
---|
139 | |
---|
140 | self.getUid = function(){ |
---|
141 | return currentUid; |
---|
142 | }; |
---|
143 | })(), |
---|
144 | content: new (function() { |
---|
145 | var self = this; |
---|
146 | var current = ""; |
---|
147 | |
---|
148 | function init() { |
---|
149 | $(window).bind( 'hashchange', function(e){ |
---|
150 | var url = $.bbq.getState("content"); |
---|
151 | self.goTo(url); |
---|
152 | }); |
---|
153 | } |
---|
154 | |
---|
155 | self.initial = function(location) { |
---|
156 | if ( current ) { |
---|
157 | self.goTo(location); |
---|
158 | } |
---|
159 | var url = $.bbq.getState("content"); |
---|
160 | self.goTo(url || location); |
---|
161 | } |
---|
162 | |
---|
163 | self.goTo = function(location) { |
---|
164 | if ( current == location ) { |
---|
165 | return $.when(); |
---|
166 | } |
---|
167 | current = location; |
---|
168 | var d = $.Deferred(); |
---|
169 | $.get("pages/"+location+"/"+location+".html") |
---|
170 | .done(function(html){ |
---|
171 | $("#content").html(html); |
---|
172 | $.getScript("pages/"+location+"/"+location+".js") |
---|
173 | .done(function(){ |
---|
174 | d.resolve(); |
---|
175 | }) |
---|
176 | .fail(function(){ |
---|
177 | d.reject(); |
---|
178 | }) |
---|
179 | }) |
---|
180 | .fail(function(){ |
---|
181 | $("#content").html("Page "+location+" not found."); |
---|
182 | d.reject(); |
---|
183 | }).then(function(){ |
---|
184 | $.bbq.pushState({ |
---|
185 | "content": location |
---|
186 | }); |
---|
187 | }) |
---|
188 | return d; |
---|
189 | } |
---|
190 | |
---|
191 | init(); |
---|
192 | })() |
---|
193 | }; |
---|
194 | |
---|
195 | }); |
---|