-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve select view update #271
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ad0c02
Improve select view update
NekR c0a9ed7
Remove not needed checks
NekR 2881d79
Remove simple way of select view handling
NekR 52dc363
Temorary restore stickit custom value for all select options
NekR 95d3488
Fix tests failures
NekR b469508
Fix tests failures
NekR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,6 +207,18 @@ | |
// ------- | ||
|
||
var slice = [].slice; | ||
var STICKIT_BINDVAL_KEY = 'stickit-bind-val'; | ||
|
||
// Returns value of select option | ||
// First checks for containing custom values, e.g not string | ||
// If such exists, then return it. If not, fallback to simple node.value | ||
var getSelectOptionValue = function(el) { | ||
if (el.__stickitBindVal__) { | ||
return Backbone.$(el).data(STICKIT_BINDVAL_KEY); | ||
} else { | ||
return el.value; | ||
} | ||
}; | ||
|
||
// Evaluates the given `path` (in object/dot-notation) relative to the given | ||
// `obj`. If the path is null/undefined, then the given `obj` is returned. | ||
|
@@ -501,34 +513,54 @@ | |
// If there are no `selectOptions` then we assume that the `<select>` | ||
// is pre-rendered and that we need to generate the collection. | ||
if (!selectConfig) { | ||
selectConfig = {}; | ||
var getList = function($el) { | ||
return $el.map(function(index, option) { | ||
// Retrieve the text and value of the option, preferring "stickit-bind-val" | ||
// data attribute over value property. | ||
var dataVal = Backbone.$(option).data('stickit-bind-val'); | ||
return { | ||
value: dataVal !== undefined ? dataVal : option.value, | ||
label: option.text | ||
}; | ||
}).get(); | ||
// simple way | ||
$el.val(val); | ||
return; | ||
|
||
// I do not know if it is possible that this method might be invoked | ||
// each time with different params. I |selectConfig| might be present | ||
// in future calls after one call with it, | ||
// then next code should be enable | ||
|
||
// I left it here so you can check. If that case is not possible | ||
// then I will remove this code. If it's possible, then I will enable it | ||
|
||
var check = function(val) { | ||
return _.isArray(val) || _.isObject(val); | ||
}; | ||
if ($el.find('optgroup').length) { | ||
list = {opt_labels:[]}; | ||
// Search for options without optgroup | ||
if ($el.find('> option').length) { | ||
list.opt_labels.push(undefined); | ||
_.each($el.find('> option'), function(el) { | ||
list[undefined] = getList(Backbone.$(el)); | ||
|
||
if (isMultiple && _.isArray(val)) { | ||
var hasCustomValue = _.some(val, check); | ||
} else { | ||
var hasCustomValue = check(val); | ||
} | ||
|
||
if (hasCustomValue) { | ||
$el.prop('selectedIndex', -1); | ||
|
||
if (isMultiple) { | ||
$el.find('option').each(function() { | ||
var optionVal = getSelectOptionValue(this); | ||
var self = this; | ||
|
||
_.each(val, function(deepVal) { | ||
if (_.isEqual(optionVal, deepVal)) { | ||
$(self).prop('selected', true); | ||
} | ||
}); | ||
}); | ||
} else { | ||
$el.find('option').each(function() { | ||
var optionVal = getSelectOptionValue(this); | ||
|
||
if (_.isEqual(optionVal, val)) { | ||
$(this).prop('selected', true); | ||
return false; | ||
} | ||
}); | ||
} | ||
_.each($el.find('optgroup'), function(el) { | ||
var label = Backbone.$(el).attr('label'); | ||
list.opt_labels.push(label); | ||
list[label] = getList(Backbone.$(el).find('option')); | ||
}); | ||
} else { | ||
list = getList($el.find('option')); | ||
$el.val(val); | ||
} | ||
} | ||
|
||
|
@@ -538,15 +570,22 @@ | |
selectConfig.disabledPath = selectConfig.disabledPath || 'disabled'; | ||
|
||
var addSelectOptions = function(optList, $el, fieldVal) { | ||
var fragment = document.createDocumentFragment(); | ||
|
||
_.each(optList, function(obj) { | ||
var option = Backbone.$('<option/>'), optionVal = obj; | ||
|
||
var fillOption = function(text, val, disabled) { | ||
option.text(text); | ||
optionVal = val; | ||
// Save the option value as data so that we can reference it later. | ||
option.data('stickit-bind-val', optionVal); | ||
if (!_.isArray(optionVal) && !_.isObject(optionVal)) option.val(optionVal); | ||
|
||
if (!_.isArray(optionVal) && !_.isObject(optionVal)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here (even though it's leftover from before) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I will fix it. Thank you! |
||
option.val(optionVal); | ||
} else { | ||
// Save the option value as data so that we can reference it later. | ||
option[0].__stickitBindVal__ = true; | ||
option.data(STICKIT_BINDVAL_KEY, optionVal); | ||
} | ||
|
||
if (disabled === true) option.prop('disabled', 'disabled'); | ||
}; | ||
|
@@ -583,8 +622,10 @@ | |
}); | ||
} | ||
|
||
$el.append(option); | ||
option.appendTo(fragment); | ||
}); | ||
|
||
$el.append(fragment); | ||
}; | ||
|
||
$el.find('*').remove(); | ||
|
@@ -679,10 +720,10 @@ | |
|
||
if ($el.prop('multiple')) { | ||
return _.map(selected, function(el) { | ||
return Backbone.$(el).data('stickit-bind-val'); | ||
return getSelectOptionValue(el); | ||
}); | ||
} else { | ||
return selected.data('stickit-bind-val'); | ||
return getSelectOptionValue(selected[0]); | ||
} | ||
} | ||
}]); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
_.isArray(val)
passes_.isObject(val)
will pass as well. Check can just be_.isObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior of
_.isObject
is strange to me, but thanks for correction!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean I understand why, because it's JS and Array also is object, but I just thought what that helper is more advanced.