[101] | 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: 'a',
|
---|
| 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.mobVer >= 140) {
|
---|
| 107 | divMinus.find('div').css({'min-height': '2.3em'});
|
---|
| 108 | divPlus.find('div').css({'min-height': '2.3em'});
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (o.mobVer >= 140) {
|
---|
| 112 | divMinus.find('div').css({'min-height': '2.3em'});
|
---|
| 113 | divPlus.find('div').css({'min-height': '2.3em'});
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | if ( o.useSetButton || o.useClearButton ) {
|
---|
| 117 | y = $('<div>', {'class':uid+'controls'});
|
---|
| 118 |
|
---|
| 119 | if ( o.useSetButton ) {
|
---|
| 120 | $('<a href="#">'+w.__('customSet')+'</a>')
|
---|
| 121 | .appendTo(y).buttonMarkup({theme: o.theme, icon: 'check', iconpos: 'left', corners:true, shadow:true})
|
---|
| 122 | .on(o.clickEventAlt, function(e) {
|
---|
| 123 | e.preventDefault();
|
---|
| 124 | w.d.input.trigger('datebox', {'method':'set', 'value':w._formatter(o.customFormat,w.customCurrent), 'date':w.customCurrent});
|
---|
| 125 | w.d.input.trigger('datebox', {'method':'close'});
|
---|
| 126 | });
|
---|
| 127 | }
|
---|
| 128 | if ( o.useClearButton ) {
|
---|
| 129 | $('<a href="#">'+w.__('clearButton')+'</a>')
|
---|
| 130 | .appendTo(y).buttonMarkup({theme: o.theme, icon: 'delete', iconpos: 'left', corners:true, shadow:true})
|
---|
| 131 | .on(o.clickEventAlt, function(e) {
|
---|
| 132 | e.preventDefault();
|
---|
| 133 | w.d.input.val('');
|
---|
| 134 | w.d.input.trigger('datebox',{'method':'clear'});
|
---|
| 135 | w.d.input.trigger('datebox',{'method':'close'});
|
---|
| 136 | });
|
---|
| 137 | }
|
---|
| 138 | if ( o.useCollapsedBut ) {
|
---|
| 139 | y.addClass('ui-datebox-collapse');
|
---|
| 140 | }
|
---|
| 141 | y.appendTo(w.d.intHTML);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | divIn.on('change', 'input', function() { w.refresh(); });
|
---|
| 145 |
|
---|
| 146 | divPlus.on(o.clickEvent, 'div', function(e) {
|
---|
| 147 | e.preventDefault();
|
---|
| 148 | w._cbox_offset($(this).jqmData('field'), $(this).jqmData('amount'));
|
---|
| 149 | });
|
---|
| 150 | divMinus.on(o.clickEvent, 'div', function(e) {
|
---|
| 151 | e.preventDefault();
|
---|
| 152 | w._cbox_offset($(this).jqmData('field'), $(this).jqmData('amount')*-1);
|
---|
| 153 | });
|
---|
| 154 |
|
---|
| 155 | }
|
---|
| 156 | });
|
---|
| 157 | })( jQuery );
|
---|