-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathscript.js
444 lines (340 loc) · 11 KB
/
script.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
BX.saleOrderAjax = { // bad solution, actually, a singleton at the page
BXCallAllowed: false,
options: {},
indexCache: {},
controls: {},
modes: {},
properties: {},
// called once, on component load
init: function(options)
{
var ctx = this;
this.options = options;
window.submitFormProxy = BX.proxy(function(){
ctx.submitFormProxy.apply(ctx, arguments);
}, this);
BX(function(){
ctx.initDeferredControl();
});
BX(function(){
ctx.BXCallAllowed = true; // unlock form refresher
});
this.controls.scope = BX('bx-soa-order');
// user presses "add location" when he cannot find location in popup mode
BX.bindDelegate(this.controls.scope, 'click', {className: '-bx-popup-set-mode-add-loc'}, function(){
var input = BX.create('input', {
attrs: {
type: 'hidden',
name: 'PERMANENT_MODE_STEPS',
value: '1'
}
});
BX.prepend(input, BX('bx-soa-order'));
ctx.BXCallAllowed = false;
BX.Sale.OrderAjaxComponent.sendRequest();
});
},
cleanUp: function(){
for(var k in this.properties)
{
if (this.properties.hasOwnProperty(k))
{
if(typeof this.properties[k].input != 'undefined')
{
BX.unbindAll(this.properties[k].input);
this.properties[k].input = null;
}
if(typeof this.properties[k].control != 'undefined')
BX.unbindAll(this.properties[k].control);
}
}
this.properties = {};
},
addPropertyDesc: function(desc){
this.properties[desc.id] = desc.attributes;
this.properties[desc.id].id = desc.id;
},
// called each time form refreshes
initDeferredControl: function()
{
var ctx = this,
k,
row,
input,
locPropId,
m,
control,
code,
townInputFlag,
adapter;
// first, init all controls
if(typeof window.BX.locationsDeferred != 'undefined'){
this.BXCallAllowed = false;
for(k in window.BX.locationsDeferred){
window.BX.locationsDeferred[k].call(this);
window.BX.locationsDeferred[k] = null;
delete(window.BX.locationsDeferred[k]);
this.properties[k].control = window.BX.locationSelectors[k];
delete(window.BX.locationSelectors[k]);
}
}
for(k in this.properties){
// zip input handling
if(this.properties[k].isZip){
row = this.controls.scope.querySelector('[data-property-id-row="'+k+'"]');
if(BX.type.isElementNode(row)){
input = row.querySelector('input[type="text"]');
if(BX.type.isElementNode(input)){
this.properties[k].input = input;
// set value for the first "location" property met
locPropId = false;
for(m in this.properties){
if(this.properties[m].type == 'LOCATION'){
locPropId = m;
break;
}
}
if(locPropId !== false){
BX.bindDebouncedChange(input, function(value){
var zipChangedNode = BX('ZIP_PROPERTY_CHANGED');
zipChangedNode && (zipChangedNode.value = 'Y');
input = null;
row = null;
if(BX.type.isNotEmptyString(value) && /^\s*\d+\s*$/.test(value) && value.length > 3){
ctx.getLocationsByZip(value, function(locationsData){
ctx.properties[locPropId].control.setValueByLocationIds(locationsData);
}, function(){
try{
// ctx.properties[locPropId].control.clearSelected();
}catch(e){}
});
}
});
}
}
}
}
// location handling, town property, etc...
if(this.properties[k].type == 'LOCATION')
{
if(typeof this.properties[k].control != 'undefined'){
control = this.properties[k].control; // reference to sale.location.selector.*
code = control.getSysCode();
// we have town property (alternative location)
if(typeof this.properties[k].altLocationPropId != 'undefined')
{
if(code == 'sls') // for sale.location.selector.search
{
// replace default boring "nothing found" label for popup with "-bx-popup-set-mode-add-loc" inside
control.replaceTemplate('nothing-found', this.options.messages.notFoundPrompt);
}
if(code == 'slst') // for sale.location.selector.steps
{
(function(k, control){
// control can have "select other location" option
control.setOption('pseudoValues', ['other']);
// insert "other location" option to popup
control.bindEvent('control-before-display-page', function(adapter){
control = null;
var parentValue = adapter.getParentValue();
// you can choose "other" location only if parentNode is not root and is selectable
if(parentValue == this.getOption('rootNodeValue') || !this.checkCanSelectItem(parentValue))
return;
var controlInApater = adapter.getControl();
if(typeof controlInApater.vars.cache.nodes['other'] == 'undefined')
{
controlInApater.fillCache([{
CODE: 'other',
DISPLAY: ctx.options.messages.otherLocation,
IS_PARENT: false,
VALUE: 'other'
}], {
modifyOrigin: true,
modifyOriginPosition: 'prepend'
});
}
});
townInputFlag = BX('LOCATION_ALT_PROP_DISPLAY_MANUAL['+parseInt(k)+']');
control.bindEvent('after-select-real-value', function(){
// some location chosen
if(BX.type.isDomNode(townInputFlag))
townInputFlag.value = '0';
});
control.bindEvent('after-select-pseudo-value', function(){
// option "other location" chosen
if(BX.type.isDomNode(townInputFlag))
townInputFlag.value = '1';
});
// when user click at default location or call .setValueByLocation*()
control.bindEvent('before-set-value', function(){
if(BX.type.isDomNode(townInputFlag))
townInputFlag.value = '0';
});
// restore "other location" label on the last control
if(BX.type.isDomNode(townInputFlag) && townInputFlag.value == '1'){
// a little hack: set "other location" text display
adapter = control.getAdapterAtPosition(control.getStackSize() - 1);
if(typeof adapter != 'undefined' && adapter !== null)
adapter.setValuePair('other', ctx.options.messages.otherLocation);
}
})(k, control);
}
}
}
}
}
this.BXCallAllowed = true;
//set location initialized flag and refresh region & property actual content
if (BX.Sale.OrderAjaxComponent)
BX.Sale.OrderAjaxComponent.locationsCompletion();
},
checkMode: function(propId, mode){
//if(typeof this.modes[propId] == 'undefined')
// this.modes[propId] = {};
//if(typeof this.modes[propId] != 'undefined' && this.modes[propId][mode])
// return true;
if(mode == 'altLocationChoosen'){
if(this.checkAbility(propId, 'canHaveAltLocation')){
var input = this.getInputByPropId(this.properties[propId].altLocationPropId);
var altPropId = this.properties[propId].altLocationPropId;
if(input !== false && input.value.length > 0 && !input.disabled && this.properties[altPropId].valueSource != 'default'){
//this.modes[propId][mode] = true;
return true;
}
}
}
return false;
},
checkAbility: function(propId, ability){
if(typeof this.properties[propId] == 'undefined')
this.properties[propId] = {};
if(typeof this.properties[propId].abilities == 'undefined')
this.properties[propId].abilities = {};
if(typeof this.properties[propId].abilities != 'undefined' && this.properties[propId].abilities[ability])
return true;
if(ability == 'canHaveAltLocation'){
if(this.properties[propId].type == 'LOCATION'){
// try to find corresponding alternate location prop
if(typeof this.properties[propId].altLocationPropId != 'undefined' && typeof this.properties[this.properties[propId].altLocationPropId]){
var altLocPropId = this.properties[propId].altLocationPropId;
if(typeof this.properties[propId].control != 'undefined' && this.properties[propId].control.getSysCode() == 'slst'){
if(this.getInputByPropId(altLocPropId) !== false){
this.properties[propId].abilities[ability] = true;
return true;
}
}
}
}
}
return false;
},
getInputByPropId: function(propId){
if(typeof this.properties[propId].input != 'undefined')
return this.properties[propId].input;
var row = this.getRowByPropId(propId);
if(BX.type.isElementNode(row)){
var input = row.querySelector('input[type="text"]');
if(BX.type.isElementNode(input)){
this.properties[propId].input = input;
return input;
}
}
return false;
},
getRowByPropId: function(propId){
if(typeof this.properties[propId].row != 'undefined')
return this.properties[propId].row;
var row = this.controls.scope.querySelector('[data-property-id-row="'+propId+'"]');
if(BX.type.isElementNode(row)){
this.properties[propId].row = row;
return row;
}
return false;
},
getAltLocPropByRealLocProp: function(propId){
if(typeof this.properties[propId].altLocationPropId != 'undefined')
return this.properties[this.properties[propId].altLocationPropId];
return false;
},
toggleProperty: function(propId, way, dontModifyRow){
var prop = this.properties[propId];
if(typeof prop.row == 'undefined')
prop.row = this.getRowByPropId(propId);
if(typeof prop.input == 'undefined')
prop.input = this.getInputByPropId(propId);
if(!way){
if(!dontModifyRow)
BX.hide(prop.row);
prop.input.disabled = true;
}else{
if(!dontModifyRow)
BX.show(prop.row);
prop.input.disabled = false;
}
},
submitFormProxy: function(item, control)
{
var propId = false;
for(var k in this.properties){
if(typeof this.properties[k].control != 'undefined' && this.properties[k].control == control){
propId = k;
break;
}
}
// turning LOCATION_ALT_PROP_DISPLAY_MANUAL on\off
if(item != 'other'){
if(this.BXCallAllowed){
this.BXCallAllowed = false;
setTimeout(function(){BX.Sale.OrderAjaxComponent.sendRequest()}, 20);
}
}
},
getPreviousAdapterSelectedNode: function(control, adapter){
var index = adapter.getIndex();
var prevAdapter = control.getAdapterAtPosition(index - 1);
if(typeof prevAdapter !== 'undefined' && prevAdapter != null){
var prevValue = prevAdapter.getControl().getValue();
if(typeof prevValue != 'undefined'){
var node = control.getNodeByValue(prevValue);
if(typeof node != 'undefined')
return node;
return false;
}
}
return false;
},
getLocationsByZip: function(value, successCallback, notFoundCallback)
{
if(typeof this.indexCache[value] != 'undefined')
{
successCallback.apply(this, [this.indexCache[value]]);
return;
}
var ctx = this;
BX.ajax({
url: this.options.source,
method: 'post',
dataType: 'json',
async: true,
processData: true,
emulateOnload: true,
start: true,
data: {'ACT': 'GET_LOCS_BY_ZIP', 'ZIP': value},
//cache: true,
onsuccess: function(result){
if(result.result)
{
ctx.indexCache[value] = result.data;
successCallback.apply(ctx, [result.data]);
}
else
{
notFoundCallback.call(ctx);
}
},
onfailure: function(type, e){
// on error do nothing
}
});
}
};