0
|
1 |
|
|
2 |
/*######################################################################
|
|
3 |
### D A S C Y L L U S N A M E S P A C E
|
|
4 |
######################################################################*/
|
|
5 |
var D = {
|
|
6 |
|
|
7 |
// Versions
|
|
8 |
platform: Ti.getPlatform(),
|
|
9 |
name: Ti.API.Application.getName(),
|
|
10 |
version: Ti.API.Application.getVersion(),
|
|
11 |
ti_version: Ti.getVersion(),
|
|
12 |
architecture: Ti.Platform.getArchitecture(),
|
1
|
13 |
copyright: Ti.App.getCopyright(),
|
0
|
14 |
|
|
15 |
// Declared windows, for easy referencing
|
|
16 |
window: {},
|
|
17 |
|
|
18 |
// Very first run of Dasycllus?
|
|
19 |
firstrun: false,
|
|
20 |
|
|
21 |
// Persistent storage DB handle.
|
|
22 |
db: null,
|
|
23 |
|
1
|
24 |
// The primary main tray menu.
|
|
25 |
menu: null,
|
|
26 |
|
|
27 |
// The current TF server we're connected to.
|
|
28 |
//
|
0
|
29 |
// uri: http://localhost:8080/
|
|
30 |
// version: Thingfish 0.5.0 (build ...)
|
|
31 |
//
|
1
|
32 |
tf: new can.Observe({
|
|
33 |
uri: null,
|
|
34 |
version: null
|
|
35 |
}),
|
|
36 |
|
|
37 |
|
|
38 |
// Actions to perform at startup.
|
|
39 |
//
|
|
40 |
init: function() {
|
|
41 |
var D = this;
|
|
42 |
|
|
43 |
D.initDB();
|
|
44 |
D.window.main = Ti.UI.getMainWindow();
|
|
45 |
D.initMenu();
|
|
46 |
|
|
47 |
// Thingfish server object event handlers.
|
|
48 |
//
|
|
49 |
D.tf.bind( 'uri', D.initTF );
|
|
50 |
var server_uri = D.getPref( 'server_uri' );
|
|
51 |
if ( server_uri ) D.tf.attr( 'uri', server_uri );
|
|
52 |
|
|
53 |
// Main window event handlers.
|
|
54 |
//
|
|
55 |
D.window.main.addEventListener( 'moved', function() { D.saveWindowState(); });
|
|
56 |
D.window.main.addEventListener( 'resized', function() {
|
|
57 |
D.delay( function() {
|
|
58 |
D.saveWindowState();
|
|
59 |
},
|
|
60 |
500, true );
|
|
61 |
});
|
|
62 |
$( document ).contextmenu( function() { D.window.main.setContextMenu( D.menu ); });
|
|
63 |
|
|
64 |
// Each Tide window is an environment unto itself. There is a global
|
|
65 |
// key/val namespace, though. Make the 'D' namespace accessible to other windows here.
|
|
66 |
Ti.API.set( 'D', D );
|
|
67 |
},
|
|
68 |
|
|
69 |
|
|
70 |
// Determine if we should be running in developer mode.
|
|
71 |
// This requires that the user has previously enabled it in preferences,
|
|
72 |
// and the --debug command line option was passed.
|
|
73 |
//
|
|
74 |
devmode: function() {
|
|
75 |
return Ti.App.getArguments()[0] == "--debug" && this.getBoolPref( 'devmode' );
|
|
76 |
},
|
|
77 |
|
|
78 |
|
|
79 |
// Event handler for changing the Thingfish server URI.
|
|
80 |
// Sanity check and instantiate appropriate Model(s).
|
|
81 |
//
|
|
82 |
initTF: function( event, uri, old_uri ) {
|
|
83 |
var serverOk = function( data, status, xhr ) {
|
|
84 |
var info = xhr.getResponseHeader( 'x-thingfish' );
|
|
85 |
if ( ! info ) {
|
|
86 |
D.tf.attr( 'version', null );
|
|
87 |
D.notify( 'Unable to connect.', 'Not a Thingfish server?' );
|
|
88 |
return;
|
|
89 |
}
|
|
90 |
|
|
91 |
D.tf.attr( 'version', data.version )
|
|
92 |
D.notify( 'Connected.', info );
|
|
93 |
|
|
94 |
// FIXME: models?
|
|
95 |
};
|
|
96 |
|
|
97 |
try {
|
|
98 |
$.ajax({
|
|
99 |
url: uri + '/serverinfo',
|
|
100 |
async: false,
|
|
101 |
success: serverOk,
|
|
102 |
error: function( xhr, status, err ) {
|
|
103 |
D.tf.attr( 'version', null );
|
|
104 |
D.notify( 'Unable to connect.', err ? err : 'Network error.' );
|
|
105 |
},
|
|
106 |
dataType: 'json'
|
|
107 |
});
|
|
108 |
}
|
|
109 |
catch( err ) {
|
|
110 |
console.log( err );
|
|
111 |
};
|
|
112 |
},
|
0
|
113 |
|
|
114 |
|
|
115 |
// Open a handle to the local database, initializing it
|
|
116 |
// if necessary and performing rudamentary upwards migrations.
|
|
117 |
//
|
|
118 |
initDB: function() {
|
|
119 |
this.db = Ti.Database.openFile(
|
|
120 |
Ti.Filesystem.getFile(
|
|
121 |
Ti.Filesystem.getApplicationDataDirectory(), 'dascyllus.db'
|
|
122 |
)
|
|
123 |
);
|
|
124 |
|
|
125 |
var rv = this.db.execute( 'PRAGMA user_version' );
|
|
126 |
var schema_version = parseInt( rv.fieldByName('user_version') );
|
|
127 |
|
|
128 |
var schemas_dir = Ti.Filesystem.getFile(
|
|
129 |
Ti.Filesystem.getApplicationDirectory(), 'sql' );
|
|
130 |
var current_version = schemas_dir.getDirectoryListing().length;
|
|
131 |
|
|
132 |
if ( schema_version == 0 ) {
|
|
133 |
this.notify( 'Welcome!', 'Dascyllus defaults installed.' );
|
|
134 |
this.firstrun = true;
|
|
135 |
}
|
|
136 |
|
|
137 |
if ( schema_version != current_version ) {
|
|
138 |
console.log( 'Dascyllus DB at version ' + schema_version +
|
|
139 |
', needs to be upgraded to ' + current_version + '.' );
|
|
140 |
|
|
141 |
while ( schema_version != current_version ) {
|
|
142 |
var update_version = schema_version + 1;
|
|
143 |
console.log( ' updating to version ' + update_version );
|
|
144 |
|
|
145 |
var line;
|
|
146 |
var schema_file = Ti.Filesystem.getFileStream(
|
|
147 |
Ti.Filesystem.getApplicationDirectory(), 'sql', update_version + '.sql' );
|
|
148 |
schema_file.open();
|
|
149 |
|
|
150 |
this.db.execute( 'BEGIN' );
|
|
151 |
while ( line = schema_file.readLine() ) this.db.execute( line.toString() );
|
|
152 |
|
|
153 |
this.db.execute( 'PRAGMA user_version = ' + update_version );
|
|
154 |
this.db.execute( 'COMMIT' );
|
|
155 |
|
|
156 |
schema_version = update_version;
|
|
157 |
schema_file.close();
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
return this.db;
|
|
162 |
},
|
|
163 |
|
|
164 |
|
|
165 |
// Return a preference's current value, or null if nonexistent.
|
|
166 |
//
|
|
167 |
getPref: function( pref ) {
|
|
168 |
var row = this.db.execute( "SELECT val FROM prefs WHERE key=?", pref );
|
|
169 |
if ( ! row.isValidRow() ) return null;
|
|
170 |
|
|
171 |
var value = row.fieldByName( 'val' );
|
|
172 |
row.close();
|
|
173 |
|
|
174 |
return value;
|
|
175 |
},
|
|
176 |
|
|
177 |
|
|
178 |
// Return a preference's current value as a boolean.
|
|
179 |
//
|
|
180 |
getBoolPref: function( pref ) {
|
|
181 |
return this.getPref( pref ) == '1'
|
|
182 |
},
|
|
183 |
|
|
184 |
|
|
185 |
// Insert or update a preference value.
|
|
186 |
//
|
|
187 |
setPref: function( pref, value ) {
|
|
188 |
this.db.execute( 'BEGIN' );
|
|
189 |
|
|
190 |
// Row already exists, remove it first.
|
|
191 |
if ( this.getPref( pref ) != null )
|
|
192 |
this.db.execute( 'DELETE FROM prefs WHERE key=?', pref );
|
|
193 |
|
|
194 |
this.db.execute( 'INSERT INTO prefs VALUES ( ?, ? )', pref, value );
|
|
195 |
this.db.execute( 'COMMIT' );
|
|
196 |
},
|
|
197 |
|
|
198 |
|
|
199 |
// Set a bool preference value.
|
|
200 |
//
|
|
201 |
setBoolPref: function( pref, value ) {
|
|
202 |
this.setPref( pref, (value ? '1' : '0') );
|
|
203 |
},
|
|
204 |
|
|
205 |
|
|
206 |
// Generic delayed callback that returns the timer handle.
|
|
207 |
//
|
|
208 |
delay: (function() {
|
|
209 |
var timer = null;
|
|
210 |
return function( callback, ms, clear_previous ) {
|
|
211 |
if ( clear_previous) clearTimeout( timer );
|
|
212 |
timer = setTimeout( callback, ms );
|
|
213 |
return timer;
|
|
214 |
};
|
|
215 |
})(),
|
|
216 |
|
|
217 |
|
|
218 |
// Create and show a new notification.
|
|
219 |
//
|
|
220 |
notify: function( title, message, callback ) {
|
|
221 |
if ( ! callback ) callback = function(){};
|
|
222 |
|
|
223 |
var notification = Ti.Notification.createNotification({
|
|
224 |
'title' : title,
|
|
225 |
'message' : message,
|
|
226 |
'timeout' : 10,
|
|
227 |
'callback' : callback,
|
|
228 |
});
|
|
229 |
|
|
230 |
notification.show();
|
|
231 |
return notification;
|
|
232 |
},
|
|
233 |
|
|
234 |
|
1
|
235 |
// Record the main window position and state.
|
0
|
236 |
//
|
1
|
237 |
saveWindowState: function() {
|
|
238 |
var w = this.window.main;
|
|
239 |
var x = w.getX();
|
|
240 |
var y = w.getY();
|
|
241 |
var width = w.getWidth();
|
|
242 |
var height = w.getHeight();
|
|
243 |
|
|
244 |
var hiddenMenu = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 0 );
|
|
245 |
var fullScreenMenu = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 1 );
|
|
246 |
|
|
247 |
var pos = this.getPref( 'window_pos' );
|
|
248 |
pos = Ti.JSON.parse( pos );
|
|
249 |
|
|
250 |
// first time
|
|
251 |
//
|
|
252 |
if ( ! pos ) {
|
|
253 |
pos = {
|
|
254 |
x: x,
|
|
255 |
y: y,
|
|
256 |
width: width,
|
|
257 |
height: height,
|
|
258 |
fullscreen: false,
|
|
259 |
hidden: false
|
|
260 |
};
|
|
261 |
}
|
|
262 |
|
|
263 |
if ( w.isVisible() ) {
|
|
264 |
pos.hidden = false;
|
|
265 |
fullScreenMenu.enable();
|
|
266 |
}
|
|
267 |
else {
|
|
268 |
pos.hidden = true;
|
|
269 |
fullScreenMenu.disable();
|
|
270 |
}
|
0
|
271 |
|
1
|
272 |
if ( w.isFullScreen() ) {
|
|
273 |
pos.fullscreen = true;
|
|
274 |
hiddenMenu.disable();
|
|
275 |
}
|
|
276 |
else {
|
|
277 |
pos.fullscreen = false;
|
|
278 |
hiddenMenu.enable();
|
|
279 |
}
|
|
280 |
|
|
281 |
if ( ! pos.fullscreen ) {
|
|
282 |
pos.x = x;
|
|
283 |
pos.y = y;
|
|
284 |
pos.width = width;
|
|
285 |
pos.height = height;
|
|
286 |
}
|
|
287 |
|
|
288 |
this.setPref( 'window_pos', Ti.JSON.stringify(pos) );
|
|
289 |
},
|
|
290 |
|
|
291 |
|
|
292 |
// Set the main window according to previously saved values.
|
|
293 |
//
|
|
294 |
setWindowState: function() {
|
|
295 |
var w = this.window.main;
|
|
296 |
var pos = this.getPref( 'window_pos' );
|
|
297 |
|
|
298 |
var hiddenMenu = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 0 );
|
|
299 |
var fullScreenMenu = this.menu.getItemAt( 2 ).getSubmenu().getItemAt( 1 );
|
|
300 |
|
|
301 |
pos = Ti.JSON.parse( pos );
|
|
302 |
if ( ! pos ) return;
|
|
303 |
|
|
304 |
if ( pos.fullscreen ) {
|
|
305 |
w.setFullscreen( true );
|
|
306 |
hiddenMenu.disable();
|
|
307 |
}
|
|
308 |
else {
|
|
309 |
|
|
310 |
console.log( "Moving to x:" + pos.x + ", y:" + pos.y );
|
|
311 |
console.log( "Sizing to w:" + pos.width + ", h:" + pos.height );
|
|
312 |
w.moveTo( pos.x, pos.y );
|
|
313 |
w.setSize( pos.width, pos.height );
|
|
314 |
|
|
315 |
hiddenMenu.enable();
|
|
316 |
|
|
317 |
if ( pos.hidden ) {
|
|
318 |
w.hide();
|
|
319 |
fullScreenMenu.disable();
|
0
|
320 |
}
|
|
321 |
else {
|
1
|
322 |
w.show();
|
|
323 |
fullScreenMenu.enable();
|
0
|
324 |
}
|
1
|
325 |
}
|
0
|
326 |
|
1
|
327 |
this.saveWindowState();
|
0
|
328 |
},
|
|
329 |
|
|
330 |
|
|
331 |
// Build the main menu.
|
|
332 |
//
|
1
|
333 |
initMenu: function() {
|
|
334 |
this.menu = Ti.UI.createMenu();
|
|
335 |
var tray = Ti.UI.addTray( 'app://img/tray_icon.png' );
|
0
|
336 |
|
1
|
337 |
// windows
|
|
338 |
// ---------------------------------------------------------------
|
|
339 |
this.menu.addItem( 'About', function() {
|
0
|
340 |
if ( D.window.about && D.window.about.isVisible() ) {
|
|
341 |
D.window.about.focus();
|
|
342 |
}
|
|
343 |
else {
|
|
344 |
D.window.about = D.window.main.createWindow( 'app://window/about.html' );
|
|
345 |
D.window.about.setTopMost( true );
|
|
346 |
D.window.about.open();
|
|
347 |
}
|
|
348 |
});
|
|
349 |
|
1
|
350 |
this.menu.addItem( 'Preferences', function() {
|
0
|
351 |
if ( D.window.prefs && D.window.prefs.isVisible() ) {
|
|
352 |
D.window.prefs.focus();
|
|
353 |
}
|
|
354 |
else {
|
|
355 |
D.window.prefs = D.window.main.createWindow( 'app://window/prefs.html' );
|
|
356 |
D.window.prefs.open();
|
|
357 |
}
|
|
358 |
});
|
|
359 |
|
1
|
360 |
// view menu
|
|
361 |
// ---------------------------------------------------------------
|
|
362 |
var viewMenu = Ti.UI.createMenuItem( 'View' );
|
|
363 |
|
|
364 |
viewMenu.addItem( 'Toggle Hidden', function() {
|
|
365 |
if ( D.window.main.isVisible() ) {
|
|
366 |
D.window.main.hide();
|
|
367 |
}
|
|
368 |
else {
|
|
369 |
D.window.main.show();
|
|
370 |
}
|
|
371 |
D.saveWindowState();
|
0
|
372 |
});
|
|
373 |
|
1
|
374 |
viewMenu.addItem( 'Toggle Full Screen', function() {
|
|
375 |
D.window.main.setFullscreen( ! D.window.main.isFullscreen() );
|
|
376 |
D.saveWindowState();
|
|
377 |
});
|
|
378 |
|
|
379 |
this.menu.appendItem( viewMenu );
|
|
380 |
|
|
381 |
// other
|
|
382 |
// --------------------------------------------------------------
|
|
383 |
if ( this.getBoolPref('devmode') ) {
|
|
384 |
this.menu.addItem( 'Restart', function() {
|
|
385 |
Ti.App.restart();
|
|
386 |
});
|
|
387 |
}
|
|
388 |
|
|
389 |
tray.setMenu( this.menu );
|
|
390 |
Ti.UI.setDockMenu( this.menu );
|
|
391 |
// D.window.main.setContextMenu( this.menu );
|
|
392 |
// Ti.UI.setMenu( menu );
|
0
|
393 |
},
|
|
394 |
|
|
395 |
|
|
396 |
/*######################################################################
|
|
397 |
### M O D E L S
|
|
398 |
######################################################################*/
|
|
399 |
|
1
|
400 |
// // A Thingfish asset.
|
|
401 |
// //
|
|
402 |
// initModel: function() {
|
|
403 |
// D.Asset = can.Model({
|
|
404 |
// id : 'oid',
|
|
405 |
// findAll: 'GET ' + D.tf.uri + '/',
|
|
406 |
// findOne: 'GET ' + D.tf.uri + '/{oid}',
|
|
407 |
// create : 'POST ' + D.tf.uri + '/',
|
|
408 |
// update : 'PUT ' + D.tf.uri + '/{oid}',
|
|
409 |
// destroy: 'DELETE ' + D.tf.uri + '/{oid}',
|
0
|
410 |
|
1
|
411 |
// // TODO: other metadata hooks? diff model?
|
|
412 |
// metadata: 'GET ' + D.tf.uri + '/{oid}/metadata',
|
0
|
413 |
|
1
|
414 |
// }, {}).
|
|
415 |
// bind( 'created', function( ev, asset ) {
|
|
416 |
// console.debug( "Created a new asset " + asset.id );
|
|
417 |
// }).
|
|
418 |
// bind( 'updated', function( ev, asset ) {
|
|
419 |
// console.debug( "Updated asset " + asset.id );
|
|
420 |
// }).
|
|
421 |
// bind( 'destroyed', function( ev, asset ) {
|
|
422 |
// console.debug( "Removed asset " + asset.id );
|
|
423 |
// }
|
|
424 |
// );
|
|
425 |
// },
|
0
|
426 |
};
|
|
427 |
|