-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_area.js
487 lines (360 loc) · 18.4 KB
/
client_area.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
(function (jQuery, app) {
jQuery(function($) {
//TODO don't really need to use delegated events in most cases... except for when i've created
//elements dynamically - and then you could bind when the element is created?
var criticalErrorMessage = $(".ca_menu_bar").data("critical-error-message") ;
var redirectToLoginScreen = function() {
//this repeats the last page action whether it was a post or get
location.reload(true);
}
var criticalError = function() {
$(".ca_message_area").prepend( $("<span></span>").addClass("ca-error").html(criticalErrorMessage) );
}
$("#ca_content_area").on("click", ".ca_confirm_switch_event", function() {
var call = $(this).data("call");
$("#ca_action_field").val(call);
$("#ca_action_form").get(0).submit();
})
$("#ca_content_area").on("click", ".ca_choose_activity_eventOLD", function() {
var val = $("#ca_activity_choice").val();
if (val !== "0") {
$("#ca_action_form #ca_action_field").val(val);
$("#ca_action_form").get(0).submit();
}
})
$("#ca_content_area").on("click", ".ca_login_button_eventOLD", function() {
var username = $("#ca_login_name").val();
var clientAreaStorage = new ClientAreaStorage(username);
if (clientAreaStorage.supported) {
var storedDataProofs = clientAreaStorage.getValueAsString("ca_proofs");
$("#restoredProofs").val(storedDataProofs);
var storedDataPrints = clientAreaStorage.getValueAsString("ca_prints");
$("#restoredPrints").val(storedDataPrints);
var storedDataProofsPagesVisited = clientAreaStorage.getValueAsString("ca_proofs_pages_visited");
$("#restoredProofsPagesVisited").val(storedDataProofsPagesVisited);
var storedDataPrintsPagesVisited = clientAreaStorage.getValueAsString("ca_prints_pages_visited");
$("#restoredPrintsPagesVisited").val(storedDataPrintsPagesVisited);
}
$("#ca_action_field").val("login");
$("#ca_action_form").get(0).submit();
})
$("#ca_content_area").on("click", ".ca_page_number_event", function() {
var idx = $(this).data("index");
var mode = $(this).parents('#ca_content_area').find('.ca_menu_bar').data('mode'); //example of the problem with jQuery apps. our data is dependent on our html structure
if (mode === 'proofs') {
var sendMode = 'showProofsScreen';
} else
{
var sendMode = 'showPrintsScreen';
}
$("#ca_action_field").val(sendMode);
$("#ca_index_field").val(idx);
$("#ca_action_form").get(0).submit();
})
$("#ca_content_areaXXX").on("click", ".ca_thumb_pic img", function() {
var mode = $(".ca_menu_bar").data("mode");
if (mode == "prints") { //TODO temporary hack pending port of proofs to app
var that = this;
app.checkSessionAndRun(function() {
thumbSelected.call(that, mode);
});
} else {
thumbSelected.call(this, mode);
}
});
$("body").on("click", ".ca_lightbox_close_eventXXX", function() {
//TODO this is all a bit hacky until we have a single app. (and everything is managed by views in regions by an app region manager)
var mode = $(".ca_menu_bar").data("mode");
if ((mode == "prints") && (typeof(app.basketCollectionView) !== "undefined")) {
app.closePrintPopUp();
}
$(this).closest(".ca_lightbox").remove();
$("#ca_lightbox_overlay").remove();
})
$("#ca_content_area").on("click", ".ca_logout_confirm_event", function(evt) {
var text = $(".ca_menu_bar").data("confirm-logout-message");
var okText = $(".ca_menu_bar").data("ok-text");
var cancelText = $(".ca_menu_bar").data("cancel-text");
var popupContainer = $('<div></div>').addClass("ca_message_pop_up");
var top = evt.pageY;
var left = evt.pageX;
left = left - 200;
popupContainer.css("top", top + "px");
popupContainer.css("left", left + "px");
popupContainer.append( $('<div><button class="ca_popup_close ca_popup_close_event">x</button></div>').
addClass("ca_popup_close_bar") );
popupContainer.append( $('<div class="ca_popup_text"></div>').html(text) );
popupContainer.append( $('<button class="ca_logout_event ca_button_left">' + okText + '</button>') );
popupContainer.append( $('<button class="ca_popup_close_event">' + cancelText + '</button>') );
$(".ca_message_pop_up").remove();
$("body").append(popupContainer);
})
$("body").on("click", ".ca_popup_close_event", function() {
$(this).closest(".ca_message_pop_up").remove();
})
$("body").on("click", ".ca_logout_eventXXX", function(evt) {
$(this).closest(".ca_message_pop_up").remove();
$("#ca_action_field").val("logout");
$("#ca_action_form").get(0).submit();
});
$("#ca_content_area").on("click", ".ca_proof_cancel_event", function(evt) {
var idx = $(".ca_menu_bar").data("last-page-visited-index");
if ( (typeof(idx) === "undefined") || (idx === "")) {
idx = 0;
}
$("#ca_index_field").val(idx);
$("#ca_action_field").val("showProofsScreen");
$("#ca_action_form").get(0).submit();
});
$("#ca_content_area").on("click", ".ca_prints_checkout_event", function(evt) {
var allPagesVisited = $(".ca_menu_bar").data("all-prints-pages-visited");
})
$("#ca_content_area").on("click", ".ca_proof_event", function(evt) {
return;
var allPagesVisited = $(".ca_menu_bar").data("all-proofs-pages-visited");
var okText = $(".ca_menu_bar").data("ok-text");
if (allPagesVisited === "yes") {
var idx = $(".ca_page_info button.ca_highlighted_pagination").data("index");
$("#ca_index_field").val(idx);
$("#ca_action_field").val("processProofsConfirm");
$("#ca_action_form").get(0).submit();
} else {
var text = $(".ca_menu_bar").data("check-all-message");
var popupContainer = $('<div></div>').addClass("ca_message_pop_up");
var top = evt.pageY;
var left = evt.pageX;
left = left - 200;
popupContainer.css("top", top + "px");
popupContainer.css("left", left + "px");
popupContainer.append( $('<div><button class="ca_popup_close ca_popup_close_event">x</button></div>').
addClass("ca_popup_close_bar") );
popupContainer.append( $('<div class="ca_popup_text"></div>').html(text) );
popupContainer.append( $('<button class="ca_popup_close_event">' + okText + '</button>') );
$(".ca_message_pop_up").remove();
$("body").append(popupContainer);
}
})
$("body").on("click", ".ca_popup_close_event", function() {
$(this).closest(".ca_message_pop_up").remove();
});
$("body").on("click", ".ca_proof_lightbox_checkbox_event", function() {
//proofSelected.call(this);
});
$("#ca_content_area").on("click", ".ca_proof_checkbox_event", function() {
//proofSelected.call(this);
});
var thumbSelected = function(mode) {
var ref = $(this).data("file-ref");
var picPath = $(".ca_menu_bar").data("url-for-mains") + '&file=' + ref;
var lightBox = $('<div></div>').addClass("ca_lightbox " + "ca_" + mode);
lightBox.append( $('<div><button class="ca_popup_close ca_lightbox_close_event">x</button></div>').
addClass("ca_popup_close_bar") );
lightBox.append( $('<div></div>').addClass("ca_lightbox_image").
append( $("<img>").attr("src", picPath) ));
if (mode === 'proofs') {
var cb = $(this).parent(".ca_thumb_pic").find('input[type="checkbox"]');
var ref = cb.val();
var checkedState = cb.prop("checked");
var popUpCheckbox = $("<input>").addClass("ca_proof_lightbox_checkbox_event").attr("type", "checkbox").val(ref);
if (checkedState) {
popUpCheckbox.prop("checked", true);
}
lightBox.append( $('<div></div>').addClass("ca_lightbox_controls").
append( popUpCheckbox ));
}
var overLay = $('<div></div>').attr("id", "ca_lightbox_overlay");
var actualImageWidth = $(this).data("image-width");
var actualImageHeight = $(this).data("image-height");
//TODO - what if same?
var ratio = (Math.max(actualImageWidth, actualImageHeight)) / (Math.min(actualImageWidth, actualImageHeight)).toFixed(2);
ratio = ratio.toFixed(2);
var viewportWidth = $(window).width();
var viewportHeight = $(window).height();
var safeImageWidth = viewportWidth - 100;
var safeImageHeight = viewportHeight - 100;
var heightAdjuster = 100;
var usedImageWidth;
if (mode=="proofs") {
if ((actualImageWidth > safeImageWidth) && (actualImageHeight <= safeImageHeight)) {
lightBox.find("img").css({"width": safeImageWidth + ".px", "height": "auto"});
usedImageWidth = safeImageWidth;
} else if ( (actualImageWidth <= safeImageWidth) && (actualImageHeight > safeImageHeight)) {
lightBox.find("img").css({"height": safeImageHeight + ".px", "width": "auto"});
var resizedProportion = safeImageHeight / actualImageHeight;
usedImageWidth = Math.round(actualImageWidth * resizedProportion);
} else if ((actualImageWidth > safeImageWidth) && (actualImageHeight > safeImageHeight)) {
var widthExceedsRatio = actualImageWidth / safeImageWidth;
var heightExceedsRatio = actualImageHeight / safeImageHeight;
if (widthExceedsRatio > heightExceedsRatio) {
lightBox.find("img").css({"width": safeImageWidth + ".px", "height": "auto"});
usedImageWidth = safeImageWidth;
} else {
lightBox.find("img").css({"height": safeImageHeight + ".px", "width": "auto"});
var resizedProportion = safeImageHeight / actualImageHeight;
usedImageWidth = Math.round(actualImageWidth * resizedProportion);
}
} else {
lightBox.find("img").css({"width": actualImageWidth + ".px", "height": "auto"});
usedImageWidth = actualImageWidth;
}
lightBox.css('min-width', usedImageWidth + 'px');
var offset = Math.floor(usedImageWidth / 2);
lightBox.css("margin-left", "-" + offset + "px");
}
else //TODO formatting is messed up need to fix to use 4 spaces per indent not 2 - or 6
{ //in this scheme we set an overall width for the popup which is centered - a max of 1600 or 95% on smaller screens; the image is sized to fit inside the pop-up and left-aligned but without interpolating; height is always auto which mean be vertical scrolling
var maxPopupWidth = 1600;
var lightBoxWidthFraction = 0.95;
var lightBoxWidthPercent = (100*lightBoxWidthFraction) + '%';
var lightBoxWidth = maxPopupWidth + 'px';
lightBox.css({'max-width': lightBoxWidth ,'width': lightBoxWidthPercent});
if (actualImageHeight > (safeImageHeight)) //height is too big (portrait images on landscape orientation phones)
{
//problem is that we can't set img width auto in case it > than width of containing div
//so we have to calculate a new width and set that
var heightExceedsRatio = actualImageHeight / safeImageHeight;
var modifiedWidth = actualImageWidth / heightExceedsRatio;
lightBox.find("img").css({"max-width": Math.floor(modifiedWidth) + "px", "width": "100%"});
}
else
{
lightBox.find("img").css({"max-width": Math.floor(actualImageWidth) + "px", "width": "100%", "height": "auto"});
}
}
if (mode === 'prints') {
lightBox.append('<div id="ca_pricing_area" class="container-fluid"></div>');
}
$(".ca_message_pop_up").remove();
$("body").append(overLay);
$("body").append(lightBox);
if (mode == 'prints') {
renderPricingArea(ref, ratio);
}
$("body").scrollTop(0);
var labelsOption = $(".ca_menu_bar").data("labels-option");
if (labelsOption === "on") {
$(".ca_lightbox_controls").append( $('<div></div>').addClass("ca_lightbox_label").html(ref) );
}
}
var proofSelected = function() {
var fileRef = $(this).val();
var status = $(this).is(":checked");
if (status) {
var fileAction = "add";
} else {
var fileAction = "remove";
}
var data = {
action: "ajaxAddRemoveProofImage",
fileRef : fileRef,
fileAction : fileAction
}
var postUrl = $("#ca_action_form").attr("action");
var that = this;
var jqxhr = $.post(postUrl, data, function(res) {
if ((typeof(res.redirect)) !== "undefined" && (res.redirect)) {
redirectToLoginScreen();
return;
}
var checkState = res.checkboxOn;
$(that).prop("checked", checkState);
if ($(that).hasClass("ca_proof_lightbox_checkbox_event")) {
$('.ca_proof_checkbox_event[value="' + fileRef + '"]').prop("checked", checkState);
}
$(".ca_counter").html(res.numberOfProofs);
var ca_proofs_pages_visited = $(".ca_menu_bar").data("username");
var clientAreaStorage = new ClientAreaStorage(username);
if (clientAreaStorage.supported) {
if (checkState) {
clientAreaStorage.addToStorage("ca_proofs", res.fileRef);
} else {
clientAreaStorage.removeFromStorage("ca_proofs", res.fileRef);
}
}
}, "json").done(function() {
}).fail(function() {
criticalError();
}).always(function() {
});
}
var renderPricingArea = function(ref, ratio) {
app.showPrintPopUp(ref, ratio);
}
var ClientAreaStorage = function(username) {
this.username = username;
if ( (JSON && typeof JSON.parse === 'function') && (typeof(Storage) !== "undefined") &&
(typeof(Array.prototype.indexOf) === "function")) {
this.supported = true;
} else {
this.supported = false;
}
}
ClientAreaStorage.prototype.getUserKey = function(key)
{
return this.username + "_" + key;
}
ClientAreaStorage.prototype.getValueAsArray = function(key) {
var storedString = localStorage.getItem(this.getUserKey(key));
if ((storedString === null) || (storedString === "")) {
return new Array();
}
var storedArray = $.parseJSON(storedString);
return storedArray;
}
ClientAreaStorage.prototype.getValueAsString = function(key) {
var storedString = localStorage.getItem(this.getUserKey(key));
return storedString;
}
ClientAreaStorage.prototype.setValueFromArray = function(key, data) {
var dataAsString = JSON.stringify(data);
localStorage.setItem(this.getUserKey(key), dataAsString);
}
ClientAreaStorage.prototype.addToStorage = function(key, value) {
if (!this.supported) {
return;
}
var storedData = this.getValueAsArray(key);
if (storedData.indexOf(value) >= 0) {
return;
} else {
storedData.push(value);
}
this.setValueFromArray(key, storedData);
}
ClientAreaStorage.prototype.removeFromStorage = function(key, value) {
if (!this.supported) {
return;
}
var storedData = this.getValueAsArray(key);
var pos = storedData.indexOf(value);
if (pos >= 0) {
storedData.splice(pos, 1);
}
this.setValueFromArray(key, storedData);
}
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
//ca_proofs, ca_proofs_pages_visited
var ClientAreaStorageProofs = function(username) {
this.username = username;
ClientAreaStorage.call(this);
}
//onpageload
var pageOn = $("button.ca_thumbs_page");
var mode = $('.ca_menu_bar').data('mode');
if (pageOn.length >= 1) {
var username = $(".ca_menu_bar").data("username");
var clientAreaStorage = new ClientAreaStorage(username);
if (clientAreaStorage.supported) {
var pageIndex = $(pageOn).filter(".ca_highlighted_pagination").data("index");
clientAreaStorage.addToStorage("ca_" + mode + "_pages_visited", pageIndex);
}
}
app.init();
});
}(jQuery, caApp));