This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
upload.js
218 lines (188 loc) · 6.26 KB
/
upload.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Copyright 2011 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*
*/
var utils = require("../../uki-core/utils"),
fun = require("../../uki-core/function"),
App = require("./app").App,
UploadDialog = require("../view/uploadDialog").UploadDialog,
CampaignUtils = require("../model/campaign/CampaignUtils").CampaignUtils,
UploadUtils = require("./upload/UploadUtils").UploadUtils,
CampaignUploader =
require("./upload/CampaignUploader").CampaignUploader,
AdUploader =
require("./upload/AdUploader").AdUploader,
ImageUploader =
require("./upload/ImageUploader").ImageUploader,
UploaderState =
require("./upload/PowerEditorUploaderState").UploaderState;
/**
* Upload camps and ads back to server
* Create new ones, update existing
* Resolve issues with images.
*/
var Upload = {
/**
* this function delegates its implementation to an instance of
* the "PowerEditorUploader" class.
*/
handleUpload: function() {
var power_editor_uploader = new PowerEditorUploader();
power_editor_uploader.start();
}
};
var PowerEditorUploader = fun.newClass({
_nAds: 0,
_nCampaigns: 0,
_dialog: null,
_nUploaded: 0,
init: function() {
this._dialog = null;
UploaderState.stopped = false;
this._nAds = 0;
this._nCampaigns = 0;
this._nUploaded = 0;
},
/**
* Displays the upload dialog, and hands off to "uploadAll"
*/
start: function() {
// try to see if we created this earlier.
this._dialog = UploaderState.dialog;
// if not, create it now. :)
if (!this._dialog) {
UploaderState.dialog =
this._dialog = new UploadDialog();
}
this._dialog.reset();
var changed_campaigns = CampaignUtils.getChanged();
this._dialog.on('continueWithErrors',
fun.bind(this._uploadAll, this, changed_campaigns))
.on('stop', UploaderState.signalStopped);
this._dialog.visible(true);
if (changed_campaigns.length === 0) {
this._dialog.notifyNoChanges();
return;
}
if (CampaignUtils.haveErrors(changed_campaigns)) {
this._dialog.confirmContinueWithErrors();
} else {
this._uploadAll(changed_campaigns);
}
},
/**
* The heavy lifting happens here. This is extracted out
* so it can be started either after the error dialog or not (see "start")
*
* (note - "changed_campaigns" is different than "changed_campaigns_directly")
*/
_uploadAll: function(changed_campaigns) {
UploadUtils.getChangedAdsFromCampaigns(
changed_campaigns, fun.bind(function(ads) {
// as opposed to "changed_campaigns" which might not have changed
// but have changes in their child ads.
var changed_campaigns_directly = CampaignUtils.getChangedDirectly();
this._nAds = ads.length;
this._nCampaigns = changed_campaigns_directly.length;
this._updateProgress();
// the "image_uploader" handles image uploading.
var image_uploader = new ImageUploader();
image_uploader
.setErrorHandler(fun.bindOnce(this._logErrorAndQuit, this))
.setProgressMessageHandler(
fun.bindOnce(this._setProgressMessage, this));
// the "campaign_uplader" handles campaign uploading
var campaign_uploader = new CampaignUploader();
campaign_uploader
.setErrorHandler(fun.bindOnce(this._logError, this))
.setProgressReportHandler(fun.bindOnce(this._incrCampaigns, this))
.setProgressMessageHandler(
fun.bindOnce(this._setProgressMessage, this));
// the "ad_uploader" handles ad uploading.
var ad_uploader = new AdUploader();
ad_uploader
.setErrorHandler(fun.bindOnce(this._logError, this))
.setProgressReportHandler(fun.bindOnce(this._incrAds, this))
.setProgressMessageHandler(fun.bindOnce(
this._setProgressMessage, this));
// upload all images...
image_uploader.uploadAllImages(fun.bind(function() {
// ... then upload all direct campaign changes.
campaign_uploader.uploadAllCampaigns(
changed_campaigns_directly,
// ... then upload all ads
fun.bind(ad_uploader.uploadAllAds,
ad_uploader, changed_campaigns,
// ... then, "stop"
fun.bindOnce(this._finish, this)));
}, this));
}, this));
},
/**
* called at the very end of the upload process,
* whether or not the upload finishes successfully
*/
_finish: function() {
this._dialog.notifyComplete();
App.reload();
},
/**
* Logs an error
*/
_logError: function(msg) {
this._dialog.logError(msg);
},
/**
* Logs an error, then quits
*/
_logErrorAndQuit: function(msg) {
this._logError(msg);
this._finish();
},
_setProgressMessage: function(msg) {
this._dialog.updateProgressMessage(msg);
return this;
},
/**
* delegates to the UploadDialog
*/
_updateProgress: function() {
this._dialog.updateProgress(
this._nUploaded, this._nAds, this._nCampaigns);
return this;
},
/**
* Increments the number of ads processed by "n"
*/
_incrAds: function(n) {
this._nUploaded += n;
this._updateProgress();
},
/**
* Increments the number of campaigns processed by "n"
*/
_incrCampaigns: function(n) {
this._nUploaded += n;
this._updateProgress();
}
});
exports.Upload = Upload;