Skip to content

Commit

Permalink
Fix options save.
Browse files Browse the repository at this point in the history
Add remove function.
  • Loading branch information
matrozov committed May 10, 2017
1 parent 479d069 commit 8b23480
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 71 deletions.
75 changes: 53 additions & 22 deletions dist/js/selectopus.full.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@
.addClass('selectopus-popup-hint')
.appendTo(self.$popup);

$(document).click(self.view.popup.onClose);
$(document).on('click', self.view.popup.onClose);
},

remove: function() {
$(document).off('click', self.view.popup.onClose);

self.$root.remove();

self.$element.removeData('selectopus');
},

optionsPredefined: function() {
Expand Down Expand Up @@ -178,6 +186,36 @@
return result;
},

save: function() {
self.$element.empty();

self.$element.attr('multiple', self._options.multiple);

$.each(self._items, function(value, data) {
var title = self._options.onGetLabel(self.public, data);

$('<option>')
.attr('selected', self.values.exists(value))
.attr('value', value)
.text(title)
.appendTo(self.$element);
});

$.each(self._values, function(value, data) {
if (self.items.exists(value)) {
return;
}

var title = self._options.onGetLabel(self.public, data);

$('<option>')
.attr('selected', true)
.attr('value', value)
.text(title)
.appendTo(self.$element);
});
},

language: {
exists: function(value) {
return typeof(self._languages[value]) !== 'undefined';
Expand Down Expand Up @@ -257,19 +295,6 @@
return typeof(self._values[value]) !== 'undefined';
},

save: function() {
self.$element.empty();

self.$element.attr('multiple', self._options.multiple);

$.each(self._values, function(value) {
$('<option>')
.attr('selected', true)
.text(value)
.appendTo(self.$element);
});
},

addList: function(values) {
if ($.isArray(values)) {
$.each(values, function(key, value) {
Expand Down Expand Up @@ -362,6 +387,7 @@
onClear: function() {
self.values.clear();
self.view.items.createList();
self.view.popup.close();

return false;
},
Expand All @@ -375,7 +401,7 @@

if (self.values.exists(value)) {
self.values.remove(value);
self.values.save();
self.save();

self.view.items.createList();
}
Expand Down Expand Up @@ -460,24 +486,24 @@
if (self._options.multiple) {
if (self.values.exists(value)) {
self.values.remove(value);
self.values.save();
self.save();
}
else {
self.values.add(value);
self.values.save();
self.save();
}
}
else {
if (self.values.exists(value)) {
if (!self._options.required) {
self.values.remove(value);
self.values.save();
self.save();
}
}
else {
self.values.clear();
self.values.add(value);
self.values.save();
self.save();
}
}

Expand Down Expand Up @@ -514,10 +540,11 @@
self.view.popup.items.createList();

var bound = self.$root[0].getBoundingClientRect();
var $body = $('body');

self.$popup.css('width', bound.width);
self.$popup.css('left', bound.left + $('body').scrollLeft());
self.$popup.css('top', bound.bottom + $('body').scrollTop());
self.$popup.css('left', bound.left + $body.scrollLeft());
self.$popup.css('top', bound.bottom + $body.scrollTop());

self.$popup.appendTo('body');

Expand Down Expand Up @@ -638,7 +665,7 @@

self.values.addList(values);

self.values.save();
self.save();
self.view.items.createList();

},
Expand All @@ -661,6 +688,10 @@
}
},

remove: function() {
self.remove();
},

utils: {
match: function(text, search) {
if ((typeof(text) !== 'string') || (typeof(search) !== 'string') || (search.trim().length === 0)) {
Expand Down
2 changes: 1 addition & 1 deletion dist/js/selectopus.full.min.js

Large diffs are not rendered by default.

75 changes: 53 additions & 22 deletions dist/js/selectopus.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@
.addClass('selectopus-popup-hint')
.appendTo(self.$popup);

$(document).click(self.view.popup.onClose);
$(document).on('click', self.view.popup.onClose);
},

remove: function() {
$(document).off('click', self.view.popup.onClose);

self.$root.remove();

self.$element.removeData('selectopus');
},

optionsPredefined: function() {
Expand Down Expand Up @@ -178,6 +186,36 @@
return result;
},

save: function() {
self.$element.empty();

self.$element.attr('multiple', self._options.multiple);

$.each(self._items, function(value, data) {
var title = self._options.onGetLabel(self.public, data);

$('<option>')
.attr('selected', self.values.exists(value))
.attr('value', value)
.text(title)
.appendTo(self.$element);
});

$.each(self._values, function(value, data) {
if (self.items.exists(value)) {
return;
}

var title = self._options.onGetLabel(self.public, data);

$('<option>')
.attr('selected', true)
.attr('value', value)
.text(title)
.appendTo(self.$element);
});
},

language: {
exists: function(value) {
return typeof(self._languages[value]) !== 'undefined';
Expand Down Expand Up @@ -257,19 +295,6 @@
return typeof(self._values[value]) !== 'undefined';
},

save: function() {
self.$element.empty();

self.$element.attr('multiple', self._options.multiple);

$.each(self._values, function(value) {
$('<option>')
.attr('selected', true)
.text(value)
.appendTo(self.$element);
});
},

addList: function(values) {
if ($.isArray(values)) {
$.each(values, function(key, value) {
Expand Down Expand Up @@ -362,6 +387,7 @@
onClear: function() {
self.values.clear();
self.view.items.createList();
self.view.popup.close();

return false;
},
Expand All @@ -375,7 +401,7 @@

if (self.values.exists(value)) {
self.values.remove(value);
self.values.save();
self.save();

self.view.items.createList();
}
Expand Down Expand Up @@ -460,24 +486,24 @@
if (self._options.multiple) {
if (self.values.exists(value)) {
self.values.remove(value);
self.values.save();
self.save();
}
else {
self.values.add(value);
self.values.save();
self.save();
}
}
else {
if (self.values.exists(value)) {
if (!self._options.required) {
self.values.remove(value);
self.values.save();
self.save();
}
}
else {
self.values.clear();
self.values.add(value);
self.values.save();
self.save();
}
}

Expand Down Expand Up @@ -514,10 +540,11 @@
self.view.popup.items.createList();

var bound = self.$root[0].getBoundingClientRect();
var $body = $('body');

self.$popup.css('width', bound.width);
self.$popup.css('left', bound.left + $('body').scrollLeft());
self.$popup.css('top', bound.bottom + $('body').scrollTop());
self.$popup.css('left', bound.left + $body.scrollLeft());
self.$popup.css('top', bound.bottom + $body.scrollTop());

self.$popup.appendTo('body');

Expand Down Expand Up @@ -638,7 +665,7 @@

self.values.addList(values);

self.values.save();
self.save();
self.view.items.createList();

},
Expand All @@ -661,6 +688,10 @@
}
},

remove: function() {
self.remove();
},

utils: {
match: function(text, search) {
if ((typeof(text) !== 'string') || (typeof(search) !== 'string') || (search.trim().length === 0)) {
Expand Down
Loading

0 comments on commit 8b23480

Please sign in to comment.