/
splash_controller.js
187 lines (187 loc) · 7.48 KB
/
splash_controller.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(function() {
var SplashController;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
Ti.include("/app/views/splash/splash_window.js");
SplashController = function() {
function SplashController(codeData, accountStore) {
this.takeActionFromRow = __bind(this.takeActionFromRow, this);; this.store = accountStore;
this.codeData = codeData;
this.window = new Citrus.SplashWindow(this);
root.tabGroup.activeTab.open(this.window.win, {
animated: true
});
}
__extends(SplashController, Citrus.Controller);
SplashController.prototype.tryToShow = function() {
d("Trying to show " + this.codeData);
this.window.showLoading();
return Citrus.Splash.newFromDecodedData(this.codeData, __bind(function(splash) {
d("Found a splash in the decoded data, with shortcode " + splash.shortcode);
this.splash = splash;
this.splash.actions = this._prepareActions(this.splash.actions);
this.fireEvent("splash:found", {
splash: this.splash
});
return this.window.displaySplash(this.splash);
}, this), __bind(function(xhr, status, error) {
var msg, retry;
e("Error finding a Citrus splash from the decoded data. Status: " + status);
e(xhr.responseText);
if (xhr) {
if (!Titanium.Network.online) {
msg = "You need to be connected to the internet to scan this code. Please connect and then retry.";
retry = true;
} else if (xhr.status === 404) {
msg = "This code couldn't be found in our database, probably because it's been deleted!";
retry = false;
} else {
msg = "There was an error fetching this code from the server! Please try again.";
retry = true;
}
} else {
if (status === "not_citrus_code") {
this.window.displayDecodedData(this.codeData);
return true;
}
}
this.window.displayError(msg, retry, __bind(function() {
return this.tryToShow();
}, this));
return false;
}, this));
};
SplashController.prototype.takeActionFromRow = function(row, e) {
var action, alertDialog, type;
action = row.action;
if (this.isActionTakeable(action)) {
if (action.requiresAccount()) {
return this.takeAccountBasedActionFromRow(row, e);
} else {
return this.takeAccountlessActionFromRow(row, e);
}
} else {
d("Cant run action!");
if (action.requiresAccount() && this.possibleAccountsForAction(action).length === 0) {
type = action.accountType.replace('Account', '');
alertDialog = Titanium.UI.createAlertDialog({
title: 'Whoops!',
message: "You can't run this yet because you haven't added a " + type + " account yet. Would you like to add one now?",
buttonNames: ["Add " + type, 'Cancel']
});
alertDialog.addEventListener('click', __bind(function(e) {
d(e);
if ((e.index != null) && e.index === 0) {
d("trying to add new account " + action.accountType);
return root.AccountsController.addNewAccountOfType(action.accountType, __bind(function(account) {
return account.addEventListener("state:ready", __bind(function() {
return this.takeAccountBasedActionFromRow(row, e);
}, this));
}, this));
}
}, this));
return alertDialog.show();
}
}
};
SplashController.prototype.takeAccountlessActionFromRow = function(row, e) {
var action;
action = row.action;
return action.run(_.bind(this._actionSuccess, this, row), _.bind(this._actionFailure, this, row));
};
SplashController.prototype.takeAccountBasedActionFromRow = function(row, e) {
var accounts, action, all_index, cancel_index, opts, runAction;
action = row.action;
accounts = this.possibleAccountsForAction(action);
runAction = __bind(function(account) {
row.displayInProgress();
return action.run(account, _.bind(this._actionSuccess, this, row), _.bind(this._actionFailure, this, row));
}, this);
if (accounts.length > 1) {
opts = _.map(accounts, function(account) {
return account.screenName;
});
if (_.isArray(opts)) {
opts.push("All");
all_index = opts.length - 1;
opts.push("Cancel");
cancel_index = opts.length - 1;
}
this.dialog = Titanium.UI.createOptionDialog({
options: opts,
title: "Select which accounts to " + row.text() + ".",
destructive: all_index,
cancel: cancel_index
});
this.dialog.addEventListener("click", __bind(function(e) {
var account, _i, _len, _results;
if (e.index === cancel_index) {
return Titanium.API.debug("Account select dialog was canceled.");
} else if (e.index === all_index) {
_results = [];
for (_i = 0, _len = accounts.length; _i < _len; _i++) {
account = accounts[_i];
_results.push(runAction(account));
}
return _results;
} else {
account = accounts[e.index];
if (account != null) {
return runAction(account);
}
}
}, this));
return this.dialog.show();
} else {
runAction(accounts[0]);
return true;
}
};
SplashController.prototype.isActionTakeable = function(action) {
if (action.requiresAccount()) {
return _.any(this.store.accounts, __bind(function(account) {
return this._canAccountRunAction(account, action);
}, this));
} else {
return action.readyToRun();
}
};
SplashController.prototype.possibleAccountsForAction = function(action) {
return _.select(this.store.accounts, __bind(function(account) {
return this._canAccountRunAction(account, action);
}, this));
};
SplashController.prototype._canAccountRunAction = function(account, action) {
return action.accountType === account.type;
};
SplashController.prototype._actionSuccess = function(row, e) {
Titanium.API.debug("Action complete!");
return row.displaySuccess();
};
SplashController.prototype._actionFailure = function(row, xhr, status, error) {
er("Action Failed! xhr:", xhr, "status:", status, "error", error);
if ((error != null) && (error.alertText != null) && _.isString(error.alertText)) {
alert(error.alertText);
}
return row.displayError();
};
SplashController.prototype._prepareActions = function(actions) {
var action, _i, _len;
for (_i = 0, _len = actions.length; _i < _len; _i++) {
action = actions[_i];
if (action.requiresResponders()) {
action.respondWith(this._actionSuccess, this._actionFailure);
}
}
return actions;
};
return SplashController;
}();
Citrus.SplashController = SplashController;
}).call(this);