Skip to content

Commit 226256d

Browse files
authored
Merge pull request #639 from kabalin/fixregionselector
Fix identical names selection issue.
2 parents d4aa1e6 + d967c5b commit 226256d

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

public/js/module/region/select.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ define([
4343
if (this.selectedInit && this.selectedInit.length) {
4444
this.selectedInit.forEach(function (region) {
4545
this.selectedInitHash[region.cid] = region;
46-
this.selectedInitTkns.push({ value: region.title_local, label: region.title_local });
46+
this.selectedInitTkns.push({ cid: region.cid, value: region.title_local });
4747
}, this);
4848
}
4949

5050
this.regionsTree = ko.observableArray();
5151
this.regionsTypehead = [];
5252
this.regionsHashByCid = {};
53-
this.regionsHashByTitle = {};
5453

5554
this.sortBy = ko.observable(Utils.getLocalStorage('regionSelect.sortBy') || 'alphabet'); // alphabet, sub, photo, pic, comment
5655
this.sortOrder = ko.observable(Utils.getLocalStorage('regionSelect.sortOrder') || 1); // 1, -1
@@ -193,7 +192,7 @@ define([
193192
const result = [];
194193

195194
tokens.forEach(function (item) {
196-
const region = this.regionsHashByTitle[item.value];
195+
const region = this.regionsHashByCid[item.cid];
197196

198197
if (region && region.exists) {
199198
result.push(fields ? _.pick(region, fields) : region);
@@ -209,7 +208,7 @@ define([
209208
const results = [];
210209

211210
tokens.forEach(function (item) {
212-
const region = this.regionsHashByTitle[item.value];
211+
const region = this.regionsHashByCid[item.cid];
213212

214213
if (region && region.exists) {
215214
const result = [];
@@ -239,7 +238,7 @@ define([
239238
const result = [];
240239

241240
tokens.forEach(function (item) {
242-
const region = this.regionsHashByTitle[item.value];
241+
const region = this.regionsHashByCid[item.cid];
243242

244243
if (region && region.exists) {
245244
result.push(region.cid);
@@ -266,7 +265,7 @@ define([
266265
},
267266
createTokenfield: function () {
268267
const engine = new Bloodhound({
269-
local: this.regionsTypehead, /*[{cid: 2, title: 'США', tokens: ['2', 'USA', 'США']}]*/
268+
local: this.regionsTypehead, /*[{cid: 2, value: 'США', tokens: ['2', 'USA', 'США']}]*/
270269
datumTokenizer: function (d) {
271270
// Join all tokens using space and tokenise using whitespace.
272271
return Bloodhound.tokenizers.whitespace(d.tokens.join(' '));
@@ -290,13 +289,13 @@ define([
290289
highlight: true,
291290
}, {
292291
name: 'regions',
293-
displayKey: 'title',
292+
displayKey: 'value',
294293
limit: 10,
295294
templates: {
296295
'suggestion': function (context) {
297-
const title = '<p>' + context.title + '</p>';
296+
const title = `<p>${context.value}</p>`;
298297

299-
return title + (context.parentTitle ? "<p style='color: #aaa; font-size 0.9em'>" + context.parentTitle + '</p>' : '');
298+
return title + (context.parentTitle ? `<p style="color: #aaa; font-size 0.9em">${context.parentTitle}</p>` : '');
300299
},
301300
},
302301
source: engine.ttAdapter(),
@@ -309,7 +308,7 @@ define([
309308
const existingTokens = $(this).tokenfield('getTokens');
310309

311310
$.each(existingTokens, function (index, token) {
312-
if (token.value === e.attrs.value) {
311+
if (token.cid === e.attrs.cid) {
313312
e.preventDefault();
314313
}
315314
});
@@ -319,8 +318,7 @@ define([
319318
// Событие создания токена. Вызовется как при создании в поле,
320319
// так и при удалении из дерева (потому что при этом пересоздаются неудаляемые токены).
321320
onCreateToken: function (e) {
322-
const title = e.attrs.value;
323-
const region = this.regionsHashByTitle[title];
321+
const region = this.regionsHashByCid[e.attrs.cid];
324322

325323
if (region && region.exists) {
326324
//Если регион уже выбран, значит, мы создаем токен вручную после клика по узлу дерева
@@ -338,8 +336,7 @@ define([
338336
},
339337
//Событие удаления токена непосредственно из поля
340338
onRemoveToken: function (e) {
341-
const title = e.attrs.value;
342-
const region = this.regionsHashByTitle[title];
339+
const region = this.regionsHashByCid[e.attrs.cid];
343340

344341
if (region && region.exists) {
345342
region.selected(false);
@@ -349,12 +346,12 @@ define([
349346
//Ручное удаление токена, работает полной заменой токенов, кроме удаляемого.
350347
//Поэтому для удаляемого токена событие onRemoveToken не сработает, но сработает onCreateToken для каждого неудаляемого
351348
removeToken: function (region) {
352-
const title = region.title_local;
349+
const cid = region.cid;
353350
const tkn = this.$dom.find('.regionstkn');
354351
const tokensExists = tkn.tokenfield('getTokens');
355352

356353
_.remove(tokensExists, function (item) {
357-
return item.value === title;
354+
return item.cid === cid;
358355
});
359356
tkn.tokenfield('setTokens', tokensExists);
360357
},
@@ -386,13 +383,12 @@ define([
386383
return;
387384
}
388385

389-
const title = region.title_local;
390386
const add = !region.selected();
391387
const tkn = this.$dom.find('.regionstkn');
392388

393389
if (add) {
394390
if (this.selectRegion(region)) {
395-
tkn.tokenfield('createToken', { value: title, label: title });
391+
tkn.tokenfield('createToken', { cid: region.cid, value: region.title_local });
396392
}
397393
} else {
398394
region.selected(false);
@@ -495,11 +491,10 @@ define([
495491
cid = region.cid;
496492
this.regionsTypehead.push({
497493
cid: cid,
498-
title: region.title_local,
494+
value: region.title_local,
499495
parentTitle: region.parent && region.parent.title_local,
500496
tokens: [String(cid), region.title_local, region.title_en],
501497
});
502-
this.regionsHashByTitle[region.title_local] = region;
503498

504499
const proceed = !filterByCids || parentsCidsFilterHash[cid] === true ||
505500
region.level > 0 && parentsCidsFilterHash[region.parents[region.level - 1]] === true;

0 commit comments

Comments
 (0)