From 2b74aab321d8f4a627fe05d18389d030bf7c6a8e Mon Sep 17 00:00:00 2001 From: Alan Knowles Date: Tue, 22 Mar 2011 09:37:25 +0000 Subject: [PATCH] Add new form element Roo.form.DayPicker, a row of 7 checkboxes for Mon-Sun. --- Roo/form/DayPicker.js | 187 +++++++++++++++++++++++++++++++++++++ css/daypicker.css | 19 ++++ css/form.css | 3 + examples/form/dynamic.html | 119 +++++++++++------------ examples/form/dynamic.js | 10 +- 5 files changed, 278 insertions(+), 60 deletions(-) create mode 100644 Roo/form/DayPicker.js create mode 100644 css/daypicker.css diff --git a/Roo/form/DayPicker.js b/Roo/form/DayPicker.js new file mode 100644 index 0000000000..b0025048e4 --- /dev/null +++ b/Roo/form/DayPicker.js @@ -0,0 +1,187 @@ +/* + * + * Licence- LGPL + * + */ + +/** + * @class Roo.form.DayPicker + * @extends Roo.form.Field + * A Day picker show [M] [T] [W] .... + * @constructor + * Creates a new Day Picker + * @param {Object} config Configuration options + */ +Roo.form.DayPicker= function(config){ + Roo.form.DayPicker.superclass.constructor.call(this, config); + +}; + +Roo.extend(Roo.form.DayPicker, Roo.form.Field, { + /** + * @cfg {String} focusClass The CSS class to use when the checkbox receives focus (defaults to undefined) + */ + focusClass : undefined, + /** + * @cfg {String} fieldClass The default CSS class for the checkbox (defaults to "x-form-field") + */ + fieldClass: "x-form-field", + + /** + * @cfg {String/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to + * {tag: "input", type: "checkbox", autocomplete: "off"}) + */ + defaultAutoCreate : { tag: "input", type: 'hidden', autocomplete: "off"}, + + + actionMode : 'viewEl', + // + // private + + inputType : 'hidden', + + + inputElement: false, // real input element? + basedOn: false, // ???? + + isFormField: true, // not sure where this is needed!!!! + + onResize : function(){ + Roo.form.Checkbox.superclass.onResize.apply(this, arguments); + if(!this.boxLabel){ + this.el.alignTo(this.wrap, 'c-c'); + } + }, + + initEvents : function(){ + Roo.form.Checkbox.superclass.initEvents.call(this); + this.el.on("click", this.onClick, this); + this.el.on("change", this.onClick, this); + }, + + + getResizeEl : function(){ + return this.wrap; + }, + + getPositionEl : function(){ + return this.wrap; + }, + + + // private + onRender : function(ct, position){ + Roo.form.Checkbox.superclass.onRender.call(this, ct, position); + + this.wrap = this.el.wrap({cls: 'x-form-daypick-item '}); + + var r1 = '' + var r2 = ''; + for (var i=0; i < 7; i++) { + r1+= ''; + r2+= ''; + } + + var viewEl = this.wrap.createChild( r1 + '' + r2 + '
' + Date.dayNames[i].substring(0,3) + '
'); + viewEl.select('img').on('click', this.onClick, this); + this.viewEl = viewEl; + + + // this will not work on Chrome!!! + this.el.on('DOMAttrModified', this.setFromHidden, this); //ff + this.el.on('propertychange', this.setFromHidden, this); //ie + + + + + }, + + // private + initValue : Roo.emptyFn, + + /** + * Returns the checked state of the checkbox. + * @return {Boolean} True if checked, else false + */ + getValue : function(){ + return this.el.dom.value; + + }, + + // private + onClick : function(e){ + //this.setChecked(!this.checked); + Roo.get(e.target).toggleClass('x-menu-item-checked'); + this.refreshValue(); + //if(this.el.dom.checked != this.checked){ + // this.setValue(this.el.dom.checked); + // } + }, + + // private + refreshValue : function() + { + var val = ''; + this.viewEl.select('img',true).each(function(e,i,n) { + val += e.is(".x-menu-item-checked") ? String(n) : ''; + }); + this.setValue(val, true); + }, + + /** + * Sets the checked state of the checkbox. + * On is always based on a string comparison between inputValue and the param. + * @param {Boolean/String} value - the value to set + * @param {Boolean/String} suppressEvent - whether to suppress the checkchange event. + */ + setValue : function(v,suppressEvent){ + if (!this.el.dom) { + return; + } + var old = this.el.dom.value ; + this.el.dom.value = v; + if (suppressEvent) { + return ; + } + var cur = {}; + for (var i =0; i < v.length;i++) { + cur[v.substring(i,i+1)] = 1 ; + } + // update display.. + this.viewEl.select('img',true).each(function(e,i,n) { + + var on = e.is(".x-menu-item-checked"); + var newv = typeof(cur[n]) != 'undefined'; + if (on != newv) { + e.toggleClass('x-menu-item-checked'); + } + + }); + + + this.fireEvent('change', this, v, old); + + + }, + + // handle setting of hidden value by some other method!!?!? + setFromHidden: function() + { + if(!this.el){ + return; + } + //console.log("SET FROM HIDDEN"); + //alert('setFrom hidden'); + this.setValue(this.el.dom.value); + }, + + onDestroy : function() + { + if(this.viewEl){ + Roo.get(this.viewEl).remove(); + } + + Roo.form.DayPicker.superclass.onDestroy.call(this); + } + +}); \ No newline at end of file diff --git a/css/daypicker.css b/css/daypicker.css new file mode 100644 index 0000000000..c3135726d3 --- /dev/null +++ b/css/daypicker.css @@ -0,0 +1,19 @@ + + + +.x-form-daypick-item td div { + width: 30px; + text-align: center; +} + +.x-form-daypick-icons td img { + background: url(../images/default/menu/chk-sprite.gif) no-repeat 0 0; + width: 16px; + height: 16px; + margin: 0; + margin-left: 5px; + +} +.x-form-daypick-icons td img.x-menu-item-checked { + background-position: 0 -16px; +} \ No newline at end of file diff --git a/css/form.css b/css/form.css index c3a7448dd3..e772038e5a 100644 --- a/css/form.css +++ b/css/form.css @@ -71,7 +71,10 @@ padding-top: 2px; padding-bottom: 2px; } +/* +on our demo's this is far to small +*/ .x-form-item .x-form-text { height: 14px; /* need to determine when this should be larger or smaller.. ? */ diff --git a/examples/form/dynamic.html b/examples/form/dynamic.html index e47af9677f..f1659c581f 100644 --- a/examples/form/dynamic.html +++ b/examples/form/dynamic.html @@ -1,24 +1,27 @@ - + - + Forms - + - + + - - - - - - + + +   + + + @@ -32,56 +35,56 @@ - -

Dynamic Forms built with JavaScript

-

- These forms do not do anything and have very little validation. They solely demonstrate - how you can use Ext Forms to build and layout forms on the fly. -

- -

The js is not minified so it is readable. See - and - -.

- - - - -
-

Display Form

-
-
- - -
-

Simple Form

-
-
-
- - -
-

Multi-column and labels top

-
+

Dynamic Forms built with JavaScript

+

+ These forms do not do anything and have very little validation. They solely demonstrate + how you can use Ext Forms to build and layout forms on the fly. +

-
- - - -
-
-

Fieldsets, labels right and complex fields

-
-
- -
-
-

Multi-column, nesting and fieldsets

-
-
- -
+

The js is not minified so it is readable. See + and + + .

+ + + + +
+

Display Form

+
+
+ + +
+

Simple Form

+
+
+
+ + +
+ +

Multi-column and labels top

+
+ +
+ + + +
+
+

Fieldsets, labels right and complex fields

+
+
+ +
+
+

Multi-column, nesting and fieldsets

+
+
+ +
diff --git a/examples/form/dynamic.js b/examples/form/dynamic.js index 8e66b5656e..d95d7552c4 100644 --- a/examples/form/dynamic.js +++ b/examples/form/dynamic.js @@ -9,7 +9,8 @@ *