Skip to content

Commit

Permalink
improved es6 code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pieter Beckers committed Mar 22, 2018
1 parent 23b4888 commit 50dd349
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ module.exports = function( grunt ) {
babel: {
options: {
sourceMap: true,
presets: ['env']
presets: ["env"]
},
dist: {
files: {
'dist/jquery.selectpicker-tree.js': 'dist/jquery.selectpicker-tree.js'
"dist/jquery.selectpicker-tree.js": "dist/jquery.selectpicker-tree.js"
}
}
},
Expand Down
38 changes: 20 additions & 18 deletions src/jquery.selectpicker-tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;( function( $, window, document ) {
( ( $, window, document ) => {
const pluginName = "selectpickerTree";
const defaults = {
data: [],
Expand All @@ -12,38 +12,41 @@
}

$.extend( Plugin.prototype, {
init: function() {
console.log( this.element );
init() {
$( document ).ready( () => {
this.addSelectpicker( this.settings.data, this.settings.metadata );
} );
},
addSelectpicker: function( data, metadata ) {
addSelectpicker( data, metadata ) {
const formGroup = this.createFormGroupWithLabel( metadata.label );
const selectpicker = $( `<select class="selectpicker form-control"></select>` );
data.map( child => this.createOption( child, metadata ) )
.forEach( option => selectpicker.append( option ) );
const selectpicker = $( "<select class=\"selectpicker form-control\"></select>" );
data.map( ( child ) => this.createOption( child, metadata ) )
.forEach( ( option ) => selectpicker.append( option ) );
formGroup.append( selectpicker );
$( this.element ).append( formGroup );

this.initSelectpicker( selectpicker, metadata.options );
},
createFormGroupWithLabel: function( label ) {
const formGroup = $( `<div class="form-group"></div>` );
if ( label !== undefined ) {
createFormGroupWithLabel( label ) {
const formGroup = $( "<div class=\"form-group\"></div>" );
if ( typeof label !== "undefined" ) {
formGroup.append( $( `<label>${label}</label>` ) );
}
return formGroup;
},
createOption: function( child, metadata = {} ) {
const optionName = typeof child === "string" ?
child : child[ metadata.name ? metadata.name : "name" ];
createOption( child, metadata = {} ) {
let optionName;
if ( typeof child === "string" ) {
optionName = child;
} else {
optionName = child[ metadata.name ? metadata.name : "name" ];
}
const option = $( `<option>${optionName}</option>` );
option.data( "data", child[ metadata.children ? metadata.children : "items" ] );
option.data( "metadata", metadata.child ? metadata.child : {} );
return option;
},
initSelectpicker: function( selectpicker, options ) {
initSelectpicker( selectpicker, options ) {
selectpicker.selectpicker( this.getSelectpickerOptions( options ) );
const plugin = this;
selectpicker.on( "changed.bs.select", function( event ) {
Expand All @@ -54,12 +57,12 @@
}
} );
},
getSelectpickerOptions: function( options ) {
getSelectpickerOptions( options ) {
const defaultOptions = {
liveSearch: true
};
options = options ? options : defaultOptions;
if ( options.title === undefined ) {
if ( typeof options.title === "undefined" ) {
options.title = "Choose one...";
}
return options;
Expand All @@ -68,8 +71,7 @@
$.fn[ pluginName ] = function( options ) {
return this.each( function() {
if ( !$.data( this, "plugin_" + pluginName ) ) {
$.data( this, "plugin_" +
pluginName, new Plugin( this, options ) );
$.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
}
} );
};
Expand Down

0 comments on commit 50dd349

Please sign in to comment.