|
| 1 | +/* |
| 2 | + * jQuery Mobile Framework : plugin to provide a date and time picker. |
| 3 | + * Copyright (c) JTSage |
| 4 | + * CC 3.0 Attribution. May be relicensed without permission/notification. |
| 5 | + * https://github.com/jtsage/jquery-mobile-datebox |
| 6 | + */ |
| 7 | + |
| 8 | +(function($) { |
| 9 | + $.extend( $.mobile.datebox.prototype.options, { |
| 10 | + themeButton: 'a', |
| 11 | + themeInput: 'e', |
| 12 | + useSetButton: true, |
| 13 | + customData: [ |
| 14 | + {'input': true, 'name':'Letter', 'data':['a','b','c','d','e']}, |
| 15 | + {'input': true, 'name':'Text', 'data':['some','bull','shtuff','here']}, |
| 16 | + {'input': false, 'name':'Image', 'data':['<img src="img/slot1.png" />','<img src="img/slot2.png" />','<img src="img/slot3.png" />','<img src="img/slot4.png" />']} |
| 17 | + ], |
| 18 | + customDefault: [0,0,0], |
| 19 | + customFormat: false, |
| 20 | + customboxlang: { |
| 21 | + // This structure interfaces with __() -> if it exists, strings are looked up here after i8n fails, |
| 22 | + // and before going to 'default' - the name syntax is <mode>lang |
| 23 | + 'customSet':'Looks Good' |
| 24 | + } |
| 25 | + |
| 26 | + }); |
| 27 | + $.extend( $.mobile.datebox.prototype, { |
| 28 | + _cbox_offset: function (fld,amount) { |
| 29 | + // This is *not* an automatic override, used below specificly. |
| 30 | + var w = this, x, |
| 31 | + o = this.options; |
| 32 | + |
| 33 | + tmp = (w.customCurrent[fld] + amount) % o.customData[fld]['data'].length; |
| 34 | + if ( tmp < 0 ) { tmp = o.customData[fld]['data'].length + tmp; } |
| 35 | + |
| 36 | + w.customCurrent[fld] = tmp; |
| 37 | + if ( o.useImmediate ) { w.d.input.trigger('datebox', {'method':'set', 'value':w._formatter(o.customFormat,w.customCurrent), 'date':w.customCurrent}); } |
| 38 | + w.refresh(); |
| 39 | + } |
| 40 | + }); |
| 41 | + $.extend( $.mobile.datebox.prototype._parser, { |
| 42 | + // If this stucture exists, it is called instead of the usual date input parser. |
| 43 | + // The name of the structure is the same as the mode name - it recieves a string |
| 44 | + // as the input, which is the current value of the input element, pre-sanitized |
| 45 | + 'custombox' : function ( str ) { |
| 46 | + return ( str.length < 1 || ! str.match(/,/) ) ? this.options.customDefault : str.split(","); |
| 47 | + } |
| 48 | + }); |
| 49 | + $.extend( $.mobile.datebox.prototype._customformat, { |
| 50 | + // If this stucture exists, the formatter will call it when it encounters a special string |
| 51 | + // %X<whatever> - it recieves the single letter operater, and the current "date" value |
| 52 | + 'custombox' : function ( oper, val ) { |
| 53 | + return val[oper-1]; |
| 54 | + } |
| 55 | + }); |
| 56 | + $.extend( $.mobile.datebox.prototype._build, { |
| 57 | + 'custombox': function () { |
| 58 | + var w = this, |
| 59 | + o = this.options, i, y, tmp, cnt = -2, |
| 60 | + uid = 'ui-datebox-', |
| 61 | + divBase = $("<div>"), |
| 62 | + divPlus = $('<fieldset>'), |
| 63 | + divIn = divBase.clone(), |
| 64 | + divMinus = divPlus.clone(), |
| 65 | + customCurrent = this._makeDate(this.d.input.val()), |
| 66 | + inBase = $("<input type='text' />").addClass('ui-input-text ui-corner-all ui-shadow-inset ui-body-'+o.themeInput), |
| 67 | + inDiv = $("<div>").addClass('ui-input-text ui-corner-all ui-shadow-inset ui-body-'+o.themeInput).css({'padding':'.4em','margin':'.5em 0','text-align':'center'}), |
| 68 | + butBase = $("<div>"), |
| 69 | + butPTheme = {theme: o.themeButton, icon: 'plus', iconpos: 'bottom', corners:true, shadow:true}, |
| 70 | + butMTheme = $.extend({}, butPTheme, {icon: 'minus', iconpos: 'top'}); |
| 71 | + |
| 72 | + if ( typeof w.customCurrent === "undefined" ) { w.customCurrent = customCurrent; } |
| 73 | + |
| 74 | + if ( o.customFormat === false ) { |
| 75 | + tmp = []; |
| 76 | + for ( i = 0; i<o.customData.length; i++ ) { |
| 77 | + tmp.push('%X'+(i+1)); |
| 78 | + } |
| 79 | + o.customFormat = tmp.join(','); |
| 80 | + } |
| 81 | + |
| 82 | + if ( typeof w.d.intHTML !== 'boolean' ) { |
| 83 | + w.d.intHTML.empty().remove(); |
| 84 | + } |
| 85 | + |
| 86 | + w.d.headerText = ((w._grabLabel() !== false)?w._grabLabel():((o.mode==='datebox')?w.__('titleDateDialogLabel'):w.__('titleTimeDialogLabel'))); |
| 87 | + w.d.intHTML = $('<span>'); |
| 88 | + |
| 89 | + |
| 90 | + for(i=0; i<o.customData.length; i++) { |
| 91 | + tmp = ['a','b','c','d','e','f'][i]; |
| 92 | + if ( o.customData[i]['input'] === true ) { |
| 93 | + $('<div>').append(inBase.clone().attr('value', o.customData[i]['data'][w.customCurrent[i]])).addClass('ui-block-'+tmp).appendTo(divIn); |
| 94 | + } else { |
| 95 | + $('<div>').append(inDiv.clone().html(o.customData[i]['data'][w.customCurrent[i]])).addClass('ui-block-'+tmp).appendTo(divIn); |
| 96 | + } |
| 97 | + w._makeEl(butBase, {'attr': {'field':i, 'amount':1}}).addClass('ui-block-'+tmp).buttonMarkup(butPTheme).appendTo(divPlus); |
| 98 | + w._makeEl(butBase, {'attr': {'field':i, 'amount':1}}).addClass('ui-block-'+tmp).buttonMarkup(butMTheme).appendTo(divMinus); |
| 99 | + cnt++; |
| 100 | + } |
| 101 | + |
| 102 | + divPlus.addClass('ui-grid-'+['a','b','c','d','e'][cnt]).appendTo(w.d.intHTML); |
| 103 | + divIn.addClass('ui-datebox-dboxin').addClass('ui-grid-'+['a','b','c','d','e'][cnt]).appendTo(w.d.intHTML); |
| 104 | + divMinus.addClass('ui-grid-'+['a','b','c','d','e'][cnt]).appendTo(w.d.intHTML); |
| 105 | + |
| 106 | + if ( o.useSetButton || o.useClearButton ) { |
| 107 | + y = $('<div>', {'class':uid+'controls'}); |
| 108 | + |
| 109 | + if ( o.useSetButton ) { |
| 110 | + $('<a href="#">'+w.__('customSet')+'</a>') |
| 111 | + .appendTo(y).buttonMarkup({theme: o.theme, icon: 'check', iconpos: 'left', corners:true, shadow:true}) |
| 112 | + .on(o.clickEventAlt, function(e) { |
| 113 | + e.preventDefault(); |
| 114 | + w.d.input.trigger('datebox', {'method':'set', 'value':w._formatter(o.customFormat,w.customCurrent), 'date':w.customCurrent}); |
| 115 | + w.d.input.trigger('datebox', {'method':'close'}); |
| 116 | + }); |
| 117 | + } |
| 118 | + if ( o.useClearButton ) { |
| 119 | + $('<a href="#">'+w.__('clearButton')+'</a>') |
| 120 | + .appendTo(y).buttonMarkup({theme: o.theme, icon: 'delete', iconpos: 'left', corners:true, shadow:true}) |
| 121 | + .on(o.clickEventAlt, function(e) { |
| 122 | + e.preventDefault(); |
| 123 | + w.d.input.val(''); |
| 124 | + w.d.input.trigger('datebox',{'method':'clear'}); |
| 125 | + w.d.input.trigger('datebox',{'method':'close'}); |
| 126 | + }); |
| 127 | + } |
| 128 | + if ( o.useCollapsedBut ) { |
| 129 | + y.addClass('ui-datebox-collapse'); |
| 130 | + } |
| 131 | + y.appendTo(w.d.intHTML); |
| 132 | + } |
| 133 | + |
| 134 | + divIn.on('change', 'input', function() { w.refresh(); }); |
| 135 | + |
| 136 | + divPlus.on(o.clickEvent, 'div', function(e) { |
| 137 | + e.preventDefault(); |
| 138 | + w._cbox_offset($(this).jqmData('field'), $(this).jqmData('amount')); |
| 139 | + }); |
| 140 | + divMinus.on(o.clickEvent, 'div', function(e) { |
| 141 | + e.preventDefault(); |
| 142 | + w._cbox_offset($(this).jqmData('field'), $(this).jqmData('amount')*-1); |
| 143 | + }); |
| 144 | + |
| 145 | + } |
| 146 | + }); |
| 147 | +})( jQuery ); |
0 commit comments