diff options
| author | Géry Debongnie <ged@odoo.com> | 2015-03-09 16:34:27 +0100 |
|---|---|---|
| committer | Géry Debongnie <ged@odoo.com> | 2015-03-18 09:01:49 +0100 |
| commit | e8a00bc50da435d3859118ca127d20a2eb9a0cab (patch) | |
| tree | 039f6ec40b37392e76c8830a8e518ac40f7e7fec /addons/web/static/src/js/widgets/date_picker.js | |
| parent | 9036084f065c7aca3b2199903f4da67db3501ba7 (diff) | |
[REF] web: add module system to the web client
This commit introduces a new module system for the javascript code.
Instead of using global (namespaced) variables everywhere (like
openerp.web.whatever, ...), each file declare one (or more) module
with its dependencies. Each module will be loaded after its
dependencies, and will be given a reference to the return values.
This change is a massive departure from the way the client worked
previously. It will sadly break some/most history, but I am sure that
the future git ecosystem will allow you to easily browse the history
past this revision (one can dream).
This commit feature:
* main namespace is now odoo instead of openerp
* a way to define modules and dependencies (inspired by requirejs)
* many files have been splitted in simpler subfiles
* basic support for debugging in odoo.__DEBUG__
* no global variable (i.e. nothing in window.openerp)
* many bug fixes, many new bugs
Future vision: this modularization should simplify future code
development, simplify debugging and will allow a serious testing effort
Diffstat (limited to 'addons/web/static/src/js/widgets/date_picker.js')
| -rw-r--r-- | addons/web/static/src/js/widgets/date_picker.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/addons/web/static/src/js/widgets/date_picker.js b/addons/web/static/src/js/widgets/date_picker.js new file mode 100644 index 00000000000..7e242cf1d6e --- /dev/null +++ b/addons/web/static/src/js/widgets/date_picker.js @@ -0,0 +1,114 @@ +odoo.define('web.datepicker', ['web.core', 'web.formats', 'web.time', 'web.Widget'], function (require) { +"use strict"; + +var core = require('web.core'); +var formats = require('web.formats'); +var time = require('web.time'); +var Widget = require('web.Widget'); + +var _t = core._t; + +var DateTimeWidget = Widget.extend({ + template: "web.datepicker", + type_of_date: "datetime", + events: { + 'dp.change .oe_datepicker_main': 'change_datetime', + 'dp.show .oe_datepicker_main': 'set_datetime_default', + 'keypress .oe_datepicker_master': 'change_datetime', + }, + init: function(parent) { + this._super(parent); + this.name = parent.name; + }, + start: function() { + var l10n = _t.database.parameters; + var options = { + pickTime: true, + useSeconds: true, + startDate: moment({ y: 1900 }), + endDate: moment().add(200, "y"), + calendarWeeks: true, + icons : { + time: 'fa fa-clock-o', + date: 'fa fa-calendar', + up: 'fa fa-chevron-up', + down: 'fa fa-chevron-down' + }, + language : moment.locale(), + format : time.strftime_to_moment_format(l10n.date_format +' '+ l10n.time_format), + }; + this.$input = this.$el.find('input.oe_datepicker_master'); + if (this.type_of_date === 'date') { + options.pickTime = false; + options.useSeconds = false; + options.format = time.strftime_to_moment_format(l10n.date_format); + } + this.picker = this.$('.oe_datepicker_main').datetimepicker(options); + this.set_readonly(false); + this.set({'value': false}); + }, + set_value: function(value_) { + this.set({'value': value_}); + this.$input.val(value_ ? this.format_client(value_) : ''); + }, + get_value: function() { + return this.get('value'); + }, + set_value_from_ui_: function() { + var value_ = this.$input.val() || false; + this.set_value(this.parse_client(value_)); + }, + set_readonly: function(readonly) { + this.readonly = readonly; + this.$input.prop('readonly', this.readonly); + }, + is_valid_: function() { + var value_ = this.$input.val(); + if (value_ === "") { + return true; + } else { + try { + this.parse_client(value_); + return true; + } catch(e) { + return false; + } + } + }, + parse_client: function(v) { + return formats.parse_value(v, {"widget": this.type_of_date}); + }, + format_client: function(v) { + return formats.format_value(v, {"widget": this.type_of_date}); + }, + set_datetime_default: function(){ + //when opening datetimepicker the date and time by default should be the one from + //the input field if any or the current day otherwise + if (this.type_of_date === 'datetime') { + var value = moment().second(0); + if (this.$input.val().length !== 0 && this.is_valid_()){ + value = this.$input.val(); + } + this.$('.oe_datepicker_main').data('DateTimePicker').setValue(value); + } + }, + change_datetime: function(e) { + if ((e.type !== "keypress" || e.which === 13) && this.is_valid_()) { + this.set_value_from_ui_(); + this.trigger("datetime_changed"); + } + }, + commit_value: function () { + this.change_datetime(); + }, +}); + +var DateWidget = DateTimeWidget.extend({ + type_of_date: "date" +}); + +return { + DateTimeWidget: DateTimeWidget, + DateWidget: DateWidget, +}; +}); |
