Skip to content
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

4.0 #84

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

4.0 #84

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

170 changes: 84 additions & 86 deletions autoform-select2.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,65 @@
/* global AutoForm, _, $, Template */
/* global AutoForm, $, Template */

AutoForm.addInputType('select2', {
template: 'afSelect2',
valueConverters: {
stringArray: function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
return $.trim(item);
});
if (Array.isArray(val)) {
return val.map(item => String(item).trim())
}
return val;
return val
},
number: AutoForm.Utility.stringToNumber,
numberArray: function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
item = $.trim(item);
return AutoForm.Utility.stringToNumber(item);
});
if (Array.isArray(val)) {
return val.map(item => AutoForm.Utility.stringToNumber(item.trim()))
}
return val;
return val
},
boolean: AutoForm.Utility.stringToBool,
booleanArray: function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
item = $.trim(item);
return AutoForm.Utility.stringToBool(item);
});
if (Array.isArray(val)) {
return val.map(item => AutoForm.Utility.stringToBool(item.trim()))
}
return val;
return val
},
date: AutoForm.Utility.stringToDate,
dateArray: function (val) {
if (_.isArray(val)) {
return _.map(val, function (item) {
item = $.trim(item);
return AutoForm.Utility.stringToDate(item);
});
if (Array.isArray(val)) {
return val.map(item => AutoForm.Utility.stringToDate(item.trim()))
}
return val;
return val
}
},
contextAdjust: function (context) {
var itemAtts = _.omit(context.atts, 'firstOption');
const { firstOption, ...itemAtts } = context.atts

// NOTE: We don't add firstOption to select2 input because
// it doesn't make sense with the way select2 works.

// build items list
context.items = [];
context.items = []

// When single-select and placeholder is passed,
// the first option should be an empty option.
var multiple = itemAtts.multiple;
var select2Options = itemAtts.select2Options || {};
const multiple = itemAtts.multiple
const select2Options = itemAtts.select2Options || {}

if (!multiple && select2Options.placeholder) {
context.items.push('');
context.items.push('')
}

// Check if option is selected
var isSelected = function(conVal, optVal) {
return _.isArray(conVal) ? _.contains(conVal, optVal) : optVal === conVal;
};
const isSelected = function (conVal, optVal) {
return Array.isArray(conVal)
? conVal.includes(optVal)
: optVal === conVal
}

// Add all defined options
_.each(context.selectOptions, function(opt) {
context.selectOptions.forEach(opt => {
if (opt.optgroup) {
var subItems = _.map(opt.options, function(subOpt) {
const subItems = opt.options.map(subOpt => {
return {
name: context.name,
label: subOpt.label,
Expand All @@ -79,12 +70,12 @@ AutoForm.addInputType('select2', {
_id: subOpt.value,
selected: isSelected(context.value, subOpt.value),
atts: itemAtts
};
});
}
})
context.items.push({
optgroup: opt.optgroup,
items: subItems
});
})
} else {
context.items.push({
name: context.name,
Expand All @@ -96,19 +87,20 @@ AutoForm.addInputType('select2', {
_id: opt.value,
selected: isSelected(context.value, opt.value),
atts: itemAtts
});
})
}
});
})

return context;
return context
}
});
})

Template.afSelect2.helpers({
atts: function addFormControlAtts() {
return _.omit(this.atts, 'select2Options');
atts: function addFormControlAtts () {
const { select2Options, ...rest } = this.atts
return rest
}
});
})

Template.afSelect2.events({
'select2:select select': function (event, template) {
Expand All @@ -117,88 +109,94 @@ Template.afSelect2.events({
// changes, allowing us to retain selection properly by using this
// in the template autorun.
// Fixes #18
var val = template.$('select').val();
if (!_.isArray(val)) { val = [val]; }
let val = template.$('select').val()

if (!Array.isArray(val)) {
val = [val]
}

template.$('select option').each(function () {
var $this = $(this);
var selected = val.indexOf($this.attr('value')) !== -1;
$this.prop('selected', selected).attr('selected', selected);
});
const $this = $(this)
const selected = val.indexOf($this.attr('value')) !== -1
$this.prop('selected', selected).attr('selected', selected)
})
}
});
})

Template.afSelect2.onRendered(function () {
var template = this;
var $s = template.$('select');

// instanciate select2
$s.select2(template.data.atts.select2Options || {});
const template = this
const $s = template.$('select')
$s.select2(template.data.atts.select2Options || {})

template.autorun(function () {
var data = Template.currentData();
const data = Template.currentData()
const values = []
const currentValues = $s.val()

var values = [];
_.each(data.items, function (item) {
if (_.has(item, 'items')) {
_.each(item.items, function (subItem) {
data.items.forEach(item => {
if (Object.hasOwnProperty.call(item, 'items')) {
item.items.forEach(subItem => {
if (subItem.selected) {
values.push(subItem.value);
values.push(subItem.value)
}
});
})
} else {
if (item.selected) {
values.push(item.value);
values.push(item.value)
}
}
});
})

var $selects;
let $selects
if (values.length === 0) {
$selects = template.$('select option');
$selects = template.$('select option')
} else {
// Include any that were previously added as new tags
$selects = template.$('select option[data-select2-tag]');
$selects = template.$('select option[data-select2-tag]')
}

$selects.each(function () {
var $this = $(this);
const $this = $(this)
if ($this.attr('selected')) {
values.push($this.attr('value'));
values.push($this.attr('value'))
}
});
})

var currentValues = $s.val();
if ((!currentValues && values.length > 0) ||
(currentValues && currentValues.toString() !== values.toString())) {
if (values.length > 0 &&
(!currentValues || (currentValues && currentValues.toString() !== values.toString()))
) {
// select2 requires that we trigger change event
// for it to realize it needs to update the select2 list.
// We do it only if values have actually changed,
// which should help prevent autosave infinite looping.
$s.val(values).trigger('change');
$s.val(values)
//$s.trigger('change')
// sometimes the change event is not captured immediately, so we use
// a short timeout to ensure it, otherwise selected values may not show up
setTimeout(() => $s.trigger('change'), 10)
}
});
});
})
})

Template.afSelect2.onDestroyed(function () {
try {
if (this.view && this.view._domrange && this.$('select').data('select2')) {
this.$('select').select2('destroy');
this.$('select').select2('destroy')
}
} catch (error) {}
});
})

/*
* BOOTSTRAP THEME
*/

Template.afSelect2.copyAs('afSelect2_bootstrap3');
Template.afSelect2.copyAs('afSelect2_bootstrap4')

// The only difference is that we need to add "form-control" class
Template.afSelect2_bootstrap3.helpers({
atts: function addFormControlAtts() {
var atts = _.omit(this.atts, 'select2Options');
Template.afSelect2_bootstrap4.helpers({
atts: function addFormControlAtts () {
const { select2Options, ...rest } = this.atts
// Add bootstrap class
atts = AutoForm.Utility.addClass(atts, 'form-control');
return atts;
return AutoForm.Utility.addClass(rest, 'form-control')
}
});
})
21 changes: 11 additions & 10 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/* eslint-env meteor */
Package.describe({
name: 'aldeed:autoform-select2',
summary: 'Custom select2 input type for AutoForm',
version: '3.0.1',
version: '4.0.0',
git: 'https://github.com/aldeed/meteor-autoform-select2.git'
});
})

Package.onUse(function(api) {
api.use('underscore@1.0.0');
api.use('templating@1.0.0');
api.use('blaze@2.0.0');
api.use('aldeed:template-extension@4.0.0');
api.use('aldeed:autoform@6.0.0');
Package.onUse(function (api) {
api.use('templating@1.0.0')
api.use('blaze@2.0.0')
api.use('aldeed:template-extension@4.0.0')
api.use('aldeed:autoform@6.0.0 || 7.0.0')
api.use('jquery@3.0.0')
api.addFiles([
'autoform-select2.html',
'autoform-select2.js'
], 'client');
});
], 'client')
})