Remoto - VFS: datetimeWidget.js Source File
Remoto - VFS
datetimeWidget.js
Go to the documentation of this file.
1 
2 define( [
3  'remoto!stdlib:js/widgets/widget.js',
4  'include/modal/chooseDateTime/chooseDateTime',
5  'remoto!stdlib:js/include/utils.js',
6  'remoto!stdlib:js/include/date.js',
7  'remoto!stdlib:js/widgets/widgets/datetimeWidget.css',
8  ],
9  function(widget,chooseDateTime,utils,date)
10  {
11  'use strict';
12 
33  datetimeWidget.prototype = new widget;
34  function datetimeWidget( d )
35  {
36  var value = d.value || null;
37 
38  if (value === "NOW" || !value)
39  d.value = Date.now();
40 
41  if (value === "NEXTHOUR")
42  { var dd = new Date();
43  dd.setHours( dd.getHours() + 1 );
44  d.value = dd.getTime();
45  }
46 
47  widget.call( this, d );
48  }
49 
59  datetimeWidget.prototype.createWidget = function()
60  {
61  if (this._widgetJq) return this._widgetJq;
62 
63  widget.prototype.createWidget.call(this);
64 
65  var THIS = this;
66 
67  var e = this._widgetJq;
68  var r = e;
69 
70  if (this._options.nowbutton)
71  {
72  //http://jsfiddle.net/EAEKc/
73  //http://stackoverflow.com/questions/1017880/expand-div-to-max-width-when-floatleft-is-set
74 
75  var f = $("<div>");
76  //this._widgetJq = f;
77  r = f;
78  f.css( { "position": "relative",
79  "padding-right":"20px"
80  } );
81 
82  e.appendTo(f);//.css( { "margin-right":"20px" } );
83 
84  var nb = $("<div>").addClass("dateTimeNowButton").appendTo(f);//.html("&nbsp;");//.html("&#9203;");//html("&#8986;");//.html("&#10026;");
85  var ni = $("<img>").appendTo(nb).css("width","90%");
86  ni.attr( "src","data:image/svg+xml;utf8,<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"48\" height=\"48\" viewBox=\"-10 -10 398.311 398.311\" vector-effect=\"non-scaling-stroke\" stroke=\"white\" fill=\"black\"><circle cx=\"189\" cy=\"189\" r=\"160\" fill=\"white\"/><path d=\"M189.156 0C84.858 0 .001 84.858.001 189.155c0 104.298 84.857 189.155 189.155 189.155 104.304 0 189.154-84.857 189.154-189.155C378.31 84.858 293.46 0 189.156 0zm0 347.144c-87.117 0-157.988-70.87-157.988-157.988 0-87.115 70.871-157.988 157.988-157.988s157.986 70.873 157.986 157.988c0 87.117-70.87 157.988-157.986 157.988z\"/><path d=\"M204.739 182.963V75.841c0-8.606-6.977-15.584-15.583-15.584-8.605 0-15.582 6.978-15.582 15.584v113.314c0 4.839 2.25 9.101 5.703 11.962.146.176.245.373.397.546l58.438 66.354a15.572 15.572 0 0 0 11.701 5.279 15.5 15.5 0 0 0 10.289-3.888c6.461-5.692 7.084-15.537 1.398-21.998l-56.761-64.447z\"/></svg>");
87  nb.attr("title","Set to always be current date/time.");
88 
89  nb.bind("click", function() {
90  //e.val(0);
91  THIS.value = 0;
92  return false;
93  } );
94 
95  f.val = function(v)
96  {
97  return e.val(v);
98  };
99  }
100 
101  e.val = function(v)
102  {
103  if (arguments.length > 0)
104  {
105  var vv = "";
106 
107  if (v > 0)
108  {
109  var d = new Date();
110  d.setTime(v);
111 
112  /*
113  var fmt = 'm/d/Y';
114 
115  if (THIS._options.dateOnly)
116  fmt = 'm/d/Y';
117  else
118  fmt = 'm/d/Y H:i T';
119  */
120 
121  // vv = d.dateFormat(fmt);
122 
123  if (THIS._options.dateOnly)
124  { vv = d.toLocaleDateString("en-US", {year:"numeric", month:"2-digit", day:"2-digit"});
125  }
126  else
127  { vv = d.toLocaleString("en-US", {hour12: false, year:"numeric", month:"2-digit", day:"2-digit", hour:"2-digit", minute:"2-digit", timeZoneName:"short"});
128  }
129 
130  //manually create the "m/d/Y H:i T" format.
131  /* vv += THIS.spad(d.getMonth()+1,2);
132  vv += "/" + THIS.spad(d.getDate(),2);
133  vv += "/" + d.getFullYear();
134 
135  if (!THIS._options.dateOnly)
136  { vv += " " + THIS.spad(d.getHours(),2);
137  vv += ":" + THIS.spad(d.getMinutes(),2);
138  //vv += " " + d.getTimezone(); //bad implementation that requires the jquery plugin to override the Date prototype. Ug.
139 
140  var z = d.getTimezoneOffset();
141  var zh = Math.floor(z/60);
142  var zm = Math.floor(z%60);
143  var zs = (z > 0) ? "+" : "-";
144  vv += " GMT"+zs+THIS.spad(zh,2)+":"+THIS.spad(zm,2);
145  }
146  */
147 
148  //console.log(vv);
149  }
150  else
151  {
152  vv = "(current time/date)";
153  //vv = "(will track with current time)";
154  //vv = "";
155  }
156 
157  //console.log(vv);
158  //e.attr("value",vv);
159  e.attr("date",v);
160  //THIS.value = parseInt(v);
161  e[0].value = vv;
162  }
163 
164  //return THIS._value;
165  return e.attr("date");
166  }
167 
168  this._widgetJq.val(this._value);
169 
170  //return this._widgetJq;
171  return r;
172  }
173 
182  datetimeWidget.prototype.activate = function()
183  {
184  widget.prototype.activate.call(this);
185 
186  this._widgetJq.css("cursor","pointer");
187  this._widgetJq.attr("readonly","readonly");
188 
189  var ops = {};
190  if (this._options.dateOnly)
191  {
192  ops.format = 'm/d/Y';
193  ops.timepicker = false;
194  ops.closeOnDateSelect = true;
195  }
196  else
197  {
198  ops.format = 'm/d/Y H:i T';
199  ops.timepicker = true;
200  ops.closeOnDateSelect = false;
201  }
202 
203  var THIS = this;
204 
205  var e = this._widgetJq;
206  e.focus( function() {
207 
208  var selectedSomething = false;
209 
210  var dd = new Date(THIS.value);
211 
212  e.datetimepicker({
213  //lazyInit: true,
214  step: 15,
215  //format: 'm/d/Y H:i T',
216  format: ops.format,
217  timepicker: ops.timepicker,
218  closeOnDateSelect: ops.closeOnDateSelect,
219  // format: 'unixtime',
220  formatTime: 'H:i',
221  formatDate: 'm/d/Y',
222  yearStart: 2010,
223  yearEnd: 2030,
224  validateOnBlur: false,
225  scrollMonth: false,
226  scrollTime: false,
227  scrollInput: false,
228  // value: e.attr("date"),
229  value: dd,
230  // value: THIS.value,
231  onSelectTime: function(dp,$input){
232  //e.val(dp.getTime());
233  THIS.value = dp.getTime();
234  selectedSomething = true;
235  },
236  onChangeDateTime: function(dp,$input){
237  //e.val(dp.getTime());
238  THIS.value = dp.getTime();
239  selectedSomething = true;
240  },
241  onClose: function(dp,$input){
242  //console.log(arguments);
243  //console.log(dp.getTime());
244  //if (selectedSomething)
245  // e.val(dp.getTime());
246  //THIS.value = dp.getTime();
247  // e.datetimepicker('destroy');
248  $('body').unbind("modal.dismiss modal.accept");
249  $input.blur();
250  },
251  onShow: function(dp,$input){
252  var dt = $input.data('xdsoft_datetimepicker');
253  $('body').bind("modal.dismiss modal.accept", function() {
254  //console.log("dismiss!");
255  dt.trigger('close.xdsoft');
256  dt.remove();
257  //e.datetimepicker('destroy.xdsoft');
258  } );
259  },
260  });
261  e.trigger('open.xdsoft');
262  });
263  }
264 
273  datetimeWidget.prototype.deactivate = function()
274  {
275  widget.prototype.deactivate.call(this);
276  }
277 
278 
287  datetimeWidget.prototype.spad = function(s,l)
288  {
289  l = l || 6;
290  s = "000000"+s;
291  s = s.substr(s.length-l,l);
292  return s;
293  }
294 
303  datetimeWidget.prototype.destroy = function()
304  {
305  var dt = this._widgetJq.data('xdsoft_datetimepicker');
306  if (dt)
307  {
308  dt.trigger('close.xdsoft');
309  dt.remove();
310  }
311 
312  widget.prototype.destroy.call(this);
313  }
314 
326  datetimeWidget.prototype.__defineSetter__("value", function(v)
327  {
328  v = parseInt(v,10);
329 
330  widget.prototype.__lookupSetter__("value").call(this,v);
331 
332  return v;
333  });
334 
341  datetimeWidget.prototype.__defineGetter__("value", function()
342  {
343  var v = widget.prototype.__lookupGetter__("value").call(this);
344 
345  return parseInt(v,10);
346  } );
347 
350  return datetimeWidget;
351  }
352 );
setter value
a setter DOCME
The remoto date object, which has useful features not in the default javascript Date object.
A form widget for selecting a date and time.
spad(s, l)
Pad a string by prepending '0' characters.
destroy()
Destroy a datetimeWidget, and call its base class function.
setter widget
a setter DOCME
formatDate()
Format the date of a line based on the _dateFormat setting.
Utility functions for javascript clients.
Base class for GUI form widgets.
deactivate()
Deactivate the widget, which will unbind input events from their callbacks.
destroy()
Destroy this widget.
activate()
Activate the widget, which will cause changes to propagate to the form, and therefore the submit() me...
createWidget()
Create the widget jQuery object.