From b65b469b46b95356aed0efc7b527a929962d3ece Mon Sep 17 00:00:00 2001 From: Arjen van Bochoven Date: Wed, 25 May 2016 12:25:20 +0200 Subject: [PATCH 1/2] Fix reset button in demo And consolidate button checks in one handler --- bootstrap-formform.js | 13 +++++++++++-- formform_demo.html | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bootstrap-formform.js b/bootstrap-formform.js index b838144..6dad316 100755 --- a/bootstrap-formform.js +++ b/bootstrap-formform.js @@ -25,7 +25,7 @@ var FormForm = (function () { if ( type == 'select' ) return new ElementSelect( itemData ); else if ( type == 'textarea' ) return new ElementTextarea( itemData ); - else if ( type in { 'button':0, 'submit':0, 'reset':0 } ) return new ElementButton( itemData ); + else if ( this.isButton(type) ) return new ElementButton( itemData ); else if ( type == 'static' ) return new ElementStatic( itemData ); else if ( type == 'radiogroup' ) return new ElementRadiogroup( itemData ); else if ( type == 'file' ) return new ElementFile( itemData ); @@ -61,6 +61,7 @@ var FormForm = (function () { var self = this; // will ECMA 6 finally fix this crap? var buttons = []; var btnsAlignRight = ''; + var formform = this; Element.prototype.isHorizontal = this.isHorizontal ? [this.col1, this.col2] : null; @@ -70,7 +71,7 @@ var FormForm = (function () { var item = self.createItem( itemData ).render(); // filter out any buttons on the form root (with added whitespace for proper spacing) - if ( itemData.type == 'submit' || itemData.type == 'button' ) + if ( formform.isButton(itemData.type)) { if ( itemData.position == 'right' ) btnsAlignRight = ' text-right'; buttons.push( item, "\n" ); @@ -164,6 +165,14 @@ var FormForm = (function () { } } + // --------------------------------- + + FormForm.prototype.isButton = function(type) + { + return type in {button:0, reset:0, submit:0, menu:0}; + } + + // ----------------------------------------------------------------------------- // FormForm Elements // ----------------------------------------------------------------------------- diff --git a/formform_demo.html b/formform_demo.html index 457b52e..558e9e4 100644 --- a/formform_demo.html +++ b/formform_demo.html @@ -401,7 +401,7 @@ "classes": "btn-primary" }, { - "type": "button", + "type": "reset", "label": "Reset", "classes": "btn-danger" }, From cf057ccbc97102eb688809affcd198d0ccfd60a8 Mon Sep 17 00:00:00 2001 From: Arjen van Bochoven Date: Wed, 25 May 2016 14:54:28 +0200 Subject: [PATCH 2/2] Add isTextInput() method and move isButton() outside object --- bootstrap-formform.js | 66 +++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 37 deletions(-) diff --git a/bootstrap-formform.js b/bootstrap-formform.js index 6dad316..abfcba6 100755 --- a/bootstrap-formform.js +++ b/bootstrap-formform.js @@ -25,29 +25,12 @@ var FormForm = (function () { if ( type == 'select' ) return new ElementSelect( itemData ); else if ( type == 'textarea' ) return new ElementTextarea( itemData ); - else if ( this.isButton(type) ) return new ElementButton( itemData ); + else if ( isButton(type) ) return new ElementButton( itemData ); else if ( type == 'static' ) return new ElementStatic( itemData ); else if ( type == 'radiogroup' ) return new ElementRadiogroup( itemData ); else if ( type == 'file' ) return new ElementFile( itemData ); else if ( type == 'hidden' ) return new ElementHidden( itemData ); - else if ( type in { - text:0, - password:0, - datetime:0, - 'datetime-local':0, - date:0, - month:0, - time:0, - week:0, - number:0, - email:0, - url:0, - search:0, - tel:0, - color:0, - checkbox:0, - radio:0 - } ) return new Element( itemData ); + else if ( isTextInput(type) ) return new Element( itemData ); if ( ! type ) return new Element( itemData ); else throw new Error( 'unknown type: ' + itemData.type ); @@ -71,7 +54,7 @@ var FormForm = (function () { var item = self.createItem( itemData ).render(); // filter out any buttons on the form root (with added whitespace for proper spacing) - if ( formform.isButton(itemData.type)) + if ( isButton(itemData.type)) { if ( itemData.position == 'right' ) btnsAlignRight = ' text-right'; buttons.push( item, "\n" ); @@ -167,7 +150,31 @@ var FormForm = (function () { // --------------------------------- - FormForm.prototype.isButton = function(type) + var isTextInput = function(type) + { + return type in { + text:0, + password:0, + datetime:0, + 'datetime-local':0, + date:0, + month:0, + time:0, + week:0, + number:0, + email:0, + url:0, + search:0, + tel:0, + color:0, + checkbox:0, + radio:0 + }; + } + + // --------------------------------- + + var isButton = function(type) { return type in {button:0, reset:0, submit:0, menu:0}; } @@ -340,22 +347,7 @@ var FormForm = (function () { if ( typeof this.itemData.value != 'undefined' ) this.elem.val( this.itemData.value ); // create (optional) addons and enclosing group - if ( this.itemData.addons && this.elem.attr( 'type' ) in { - text:0, - password:0, - datetime:0, - 'datetime-local':0, - date:0, - month:0, - time:0, - week:0, - number:0, - email:0, - url:0, - search:0, - tel:0, - color:0 - } ) return this.createAddons(); + if ( this.itemData.addons && isTextInput( this.elem.attr( 'type' ) ) ) return this.createAddons(); else return this.createGroup(); }