-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmain.js
More file actions
174 lines (133 loc) · 4.3 KB
/
main.js
File metadata and controls
174 lines (133 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* require config */
requirejs.config({
shim: {
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'underscore': {
exports: '_'
},
'jquerymobile': {
deps: ['jquery']
}
}, //shim
paths: {
'backbone': 'lib/backbone',
'jquerymobile': 'lib/jquery.mobile-1.3.1',
'underscore': 'lib/underscore',
'jquery': 'lib/jquery-1.9.1'
} //path
});
requirejs(['jquery','backbone','conf','router','api','utils','models'],
function($, Backbone, conf, router, api, utils, models){
/************* utilities ***********/
function registerLoginPageActions(){
// register login button action
$('#login form').submit(function(e){
$.mobile.loading( 'show', { text: 'Authenticating...', textVisible: true} );
e.preventDefault();
// message to send
var data = {
op: "login",
user: $('#loginInput').val(),
password : $('#passwordInput').val()
};
jQuery.ajax(
{
url: conf.apiPath + 'api/',
contentType: "application/json",
dataType: 'json',
cache: 'false',
data: JSON.stringify(data),
type: 'post',
async: false
}
)
.done(function(data){
if (data.status == 0){
// we store the sessions id
models.settings.set("sid", data.content.session_id);
models.settings.save();
router.myRouter.setNextTransOptions({reverse: true, transition: "slideup"});
// try to get from query string if it exists
var fragment = location.hash;
var re = /\?from=#(.+)/;
var nextRoute = "cat";
var ex = re.exec(fragment)
if (ex != null){
nextRoute = ex[1];
}
router.myRouter.navigate(nextRoute, {trigger: true});
} else {
var msg = "Unknown answer from the API:" + data.content;
if (data.content.error == "API_DISABLED"){
msg = 'API is disabled for this user';
} else if (data.content.error == "LOGIN_ERROR"){
msg = "Specified username and password are incorrect";
}
alert(msg);
$.mobile.loading('hide');
}
});
}); // login button
}
/************** init bindings *************/
$(document).bind('mobileinit', function(event){
// desactivate jQueryMobile routing (we use Backbone.Router)
$.mobile.ajaxEnabled = false;
$.mobile.linkBindingEnabled = false;
$.mobile.hashListeningEnabled = false;
$.mobile.pushStateEnabled = false;
$.mobile.changePage.defaults.changeHash = false;
$.mobile.defaultPageTransition = "slide";
});
var g_init = false;
$(document).bind('pageinit', function(event){
if (! g_init){
g_init = true;
// alternative to localStorage using cookies
utils.localStorageSupport();
// events for login page
registerLoginPageActions();
// initialize all logout buttons
$('a.logoutButton').on('click',
function(e){
e.preventDefault();
$.mobile.loading( 'show', { text: 'Logging out...', textVisible: true} );
api.logout();
}
);
// initialize all back buttons
$('a.backButton').on('click',
function(e){
router.myRouter.setNextTransOptions({reverse: true});
}
);
// initialize all menu buttons
$("a[data-rel='popup']").on('click',
function(e){
e.preventDefault();
var popupId = $(e.currentTarget).attr('href');
var transition = $(popupId).attr('data-transition');
$(popupId).popup("open", {transition: transition,
positionTo: $(e.currentTarget) });
}
);
// prepare all pages now
$("div:jqmData(role='page')").page();
// first transition
router.myRouter.setNextTransOptions({transition: "fade"});
// start Backbone router
if (! Backbone.history.start({
pushState: false,
root: window.location.pathname,
silent: false
})){
alert("Could not start router!");
}
}
});
//loading jQuery Mobile after everything
require(['jquerymobile'], function(){});
}); //requirejs