← Back to the web2py-plugin list
Anytime Widget
This plugin provides time, date and datetime picker widgets using anytime.js.
NOTE: Confirm the anytime.js licence. The anytime.js is under the Creative Commons BY-NC-SA 3.0 License.
Demo
Usage
-
controllers/plugin_anytime_widget.py
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.from plugin_anytime_widget import anytime_widget, anydate_widget, anydatetime_widget
db = DAL('sqlite:memory:')
db.define_table('product',
Field('event_time', 'time'), Field('publish_date', 'date'),
Field('created_at', 'datetime'),
Field('updated_at', 'datetime'),
)
################################ The core ######################################
# Inject the corresponding anytime widgets for time, date, and datetime fields
db.product.event_time.widget = anytime_widget
db.product.publish_date.widget = anydate_widget
db.product.created_at.widget = anydatetime_widget
db.product.updated_at.widget = anydatetime_widget
################################################################################
def index():
form = SQLFORM(db.product)
if form.accepts(request.vars, session):
session.flash = 'submitted %s' % form.vars
redirect(URL('index'))
return dict(form=form, tests=[A('test_with_jquery_ui', _href=URL('test', args='with_jquery_ui')),
A('test_load', _href=URL('test', args=['_', 'load'])),
A('test_load_with_jquery_ui', _href=URL('test', args=['with_jquery_ui', 'load'])),
])
def test():
if request.args(0) == 'ajax':
form = SQLFORM(db.product)
if form.accepts(request.vars, session):
response.flash = DIV('submitted %s' % form.vars).xml()
return form
elif request.args(0) == 'with_jquery_ui':
response.files += (
'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css',
'http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css',
URL('static', 'js/jquery-ui.min.js'), URL('static', 'js/jquery.ui.dialog.js'),
URL('static', 'js/jquery.ui.tabs.js'), URL('static', 'js/jquery.ui.themeswitcher.js'))
jquery_ui = DIV(
DIV(_id='switcher'), BR(),
DIV(UL(LI(A('xxx', _href='#xxx')), LI(A('yyy', _href='#yyy'))),
DIV(P('xxx'), _id='xxx'), DIV(P('yyy'), _id='yyy'),
_id='example'),
SCRIPT("$().ready(function(){var $tabs = $('#example').tabs();});"))
else:
jquery_ui = ''
if request.args(1) == 'load':
form = LOAD('plugin_anytime_widget', 'test', args='ajax', ajax=True)
else:
form = SQLFORM(db.product)
if form.accepts(request.vars, session):
session.flash = 'submitted %s' % form.vars
redirect(URL('index'))
return dict(back=A('back', _href=URL('index')), jquery_ui=jquery_ui, form=form)
Source code
-
modules/plugin_anytime_widget.py
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.from gluon import *
from gluon.sqlhtml import widget_class
# For referencing static and views from other application
import os
APP = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
FILES = (URL(APP, 'static', 'plugin_anytime_widget/anytime.css'),
URL(APP, 'static', 'plugin_anytime_widget/anytime.js'))
def _set_files(files):
if current.request.ajax:
current.response.js = (current.response.js or '') + """;(function ($) {
var srcs = $('script').map(function(){return $(this).attr('src');}),
hrefs = $('link').map(function(){return $(this).attr('href');});
$.each(%s, function() {
if ((this.slice(-3) == '.js') && ($.inArray(this.toString(), srcs) == -1)) {
var el = document.createElement('script'); el.type = 'text/javascript'; el.src = this;
document.body.appendChild(el);
} else if ((this.slice(-4) == '.css') && ($.inArray(this.toString(), hrefs) == -1)) {
$('<link rel="stylesheet" type="text/css" href="' + this + '" />').prependTo('head');
if (/* for IE */ document.createStyleSheet){document.createStyleSheet(this);}
}});})(jQuery);""" % ('[%s]' % ','.join(["'%s'" % f.lower().split('?')[0] for f in files]))
else:
current.response.files[:0] = [f for f in files if f not in current.response.files]
def _get_date_option():
return """{
labelYear: "%(year)s", labelMonth: "%(month)s", labelDay: "%(day)s",
labelDayOfMonth: "%(calendar)s",
monthAbbreviations: ["%(jan)s", "%(feb)s", "%(mar)s",
"%(apr)s", "%(may)s", "%(jun)s", "%(jul)s",
"%(aug)s", "%(sep)s", "%(oct)s", "%(nov)s", "%(dec)s"],
dayAbbreviations: ["%(sun)s", "%(mon)s", "%(tue)s", "%(wed)s",
"%(thu)s", "%(fri)s", "%(sat)s"],
labelDismiss: "%(ok)s" }""" % dict(
year=current.T('Year'), month=current.T('Month'), day=current.T('Day'),
calendar=current.T('Calendar'),
jan=current.T('Jan'), feb=current.T('Feb'), mar=current.T('Mar'),
apr=current.T('Apr'), may=current.T('May'), jun=current.T('Jun'),
jul=current.T('Jul'), aug=current.T('Aug'), sep=current.T('Sep'),
oct=current.T('Oct'), nov=current.T('Nov'), dec=current.T('Dec'),
sun=current.T('Sun'), mon=current.T('Mon'), tue=current.T('Tue'),
wed=current.T('Wed'), thu=current.T('Thu'), fri=current.T('Fri'),
sat=current.T('Sat'), ok=current.T('OK'),
)
def anytime_widget(field, value, **attributes):
_set_files(FILES)
_id = '%s_%s' % (field._tablename, field.name)
attr = dict(
_type='text', value=(value != None and str(value)) or '',
_id=_id, _name=field.name, requires=field.requires,
_class='any%s' % widget_class.match(str(field.type)).group(),
)
script = SCRIPT("""
jQuery(function() { var t = 10; (function run() {if ((function() {
var el = jQuery("#%(id)s");
if (el.AnyTime_picker == undefined) { return true; }
el.AnyTime_noPicker().AnyTime_picker(
jQuery.extend({format: "%%H:%%i:%%S", labelTitle: "%(title)s",
labelHour: "%(hour)s", labelMinute: "%(minute)s", labelSecond: "%(second)s"},
%(date_option)s));
})()) {setTimeout(run, t); t = 2*t;}})();});
""" % dict(id=_id, title=current.T('Choose time'),
hour=current.T('Hour'), minute=current.T('Minute'), second=current.T('Second'),
date_option=_get_date_option()))
return SPAN(script, INPUT(**attr), **attributes)
def anydate_widget(field, value, **attributes):
_set_files(FILES)
_id = '%s_%s' % (field._tablename, field.name)
attr = dict(
_type='text', value=(value != None and str(value)) or '',
_id=_id, _name=field.name, requires=field.requires,
_class='any%s' % widget_class.match(str(field.type)).group(),
)
script = SCRIPT("""
jQuery(function() { var t = 10; (function run() {if ((function() {
var el = jQuery("#%(id)s");
if (el.AnyTime_picker == undefined) { return true; }
el.AnyTime_noPicker().AnyTime_picker(
jQuery.extend({format: "%%Y-%%m-%%d", labelTitle: "%(title)s"},
%(date_option)s));
})()) {setTimeout(run, t); t = 2*t;}})();});
""" % dict(id=_id, title=current.T('Choose date'),
date_option=_get_date_option()))
return SPAN(script, INPUT(**attr), **attributes)
def anydatetime_widget(field, value, **attributes):
_set_files(FILES)
_id = '%s_%s' % (field._tablename, field.name)
attr = dict(
_type='text', value=(value != None and str(value)) or '',
_id=_id, _name=field.name, requires=field.requires,
_class='any%s' % widget_class.match(str(field.type)).group(),
)
script = SCRIPT("""
jQuery(function() { var t = 10; (function run() {if ((function() {
var el = jQuery("#%(id)s");
if (el.AnyTime_picker == undefined) { return true; }
el.AnyTime_noPicker().AnyTime_picker(
jQuery.extend({format: "%%Y-%%m-%%d %%H:%%i:00", labelTitle: "%(title)s",
labelHour: "%(hour)s", labelMinute: "%(minute)s"},
%(date_option)s));
})()) {setTimeout(run, t); t = 2*t;}})();});
""" % dict(id=_id, title=current.T('Choose date time'),
hour=current.T('Hour'), minute=current.T('Minute'),
date_option=_get_date_option()))
return SPAN(script, INPUT(**attr), **attributes)
