simple_form_datetimepicker 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54a4c93b53073c21437a4ceb983bf1d6c3317bc5
4
- data.tar.gz: d0c1760fb1292998ad886c2138de0706db5bd27d
3
+ metadata.gz: 905cc7870041bb996d7df59d295d8be1feabe45a
4
+ data.tar.gz: 70817c7e3e2dfe7ef3d587272b8875e3f27070f0
5
5
  SHA512:
6
- metadata.gz: b15d8a380260774a9261a17aa6037e067a3a4291fd4b88a0dc84bbcfbca475792e8eed842b1df59598d6f8aea4fc3656a32088b0306d43d71e62d2e4d7dac1d7
7
- data.tar.gz: a7391741dc85806d18ba46097afc621bebe77ce1814a95213b99e4065a3099a4d3892ee650fc7a9a8abf74fb5b5091ed159e02bc485f6df3f3b51ea7d223f610
6
+ metadata.gz: 73b818c05df1162ac27fc77b3395ea50013aaf8d0cc0692d7e95c60f7b86d0cc114f8f592fcb6bfad7b31556f48820c76e4e62cc7f00c4ebe3594214eacb3d51
7
+ data.tar.gz: 1eb891a58c1edf2313adec724933f92da675c1327e2f99f9d7638ccc04109eab9a8392cbb495c4213f7dcfbdf20bb0c2a6958c4d3f92fd6b61308ba16d927e9d
@@ -1,3 +1,3 @@
1
1
  module SimpleFormDatetimepicker
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1189,31 +1189,97 @@
1189
1189
  timestamp = timestamp.slice(1, timestamp.length - 1);
1190
1190
  }
1191
1191
  timestamp = timestamp.replace(/ UTC/, '');
1192
- // console.log("Replacing timestamp " + timestamp);
1193
1192
  regex = /\ [-+][0-9]+$/;
1194
1193
  output = regex.test(timestamp) ? (r = timestamp.replace(regex, ''), moment(r, "YYYY-MM-DD HH:mm:ss")) : (regex = /\.[0-9]{3}[-+][0-9]{2}\:[0-9]{2}$/, regex.test(timestamp) ? (r = timestamp.replace(regex, '').replace('T', ' '), moment(r, "YYYY-MM-DD HH:mm:ss")) : moment(timestamp));
1195
1194
  return output;
1196
1195
  };
1196
+
1197
+ var immutable_field;
1198
+ var setDtp = function(dtp, m) {
1199
+ var picker = dtp.data('DateTimePicker');
1200
+ if (picker == null) return;
1201
+ var format = picker.format || 'MM/DD/YYYY h:mm A';
1202
+ picker.setDate(m.format(format));
1203
+ };
1204
+ var adjustMomentForDtp = function(m, amount, unit) {
1205
+ if (unit == null) unit = 'day';
1206
+ m.add(amount, unit);
1207
+ return m;
1208
+ };
1209
+ var adjustDtp = function(dtp, amount, unit) {
1210
+ var picker = dtp.data('DateTimePicker');
1211
+ if (picker == null) return;
1212
+ if (unit == null) unit = 'day';
1213
+ var m = moment(dtp.val());
1214
+ m.add(amount, unit);
1215
+ setDtp(dtp, m);
1216
+ return m;
1217
+ };
1218
+ var updateDependentDates = function(dtp) {
1219
+ var v = dtp.val();
1220
+ if (!v.length) return;
1221
+ var m = moment(v);
1222
+ var ao = dtp.data('aheadof');
1223
+ var sb = dtp.data('staybehind');
1224
+ var if_backup = immutable_field;
1225
+ immutable_field = '#' + dtp.attr('id');
1226
+ if (ao != null) $(ao).each(function(){
1227
+ if (immutable_field == null || !$(this).is(immutable_field)) {
1228
+ if (!$(this).val().length) setDtp($(this), m);
1229
+ var mm = moment($(this).val());
1230
+ var aav = $(this).data('autoadvance') || 'day';
1231
+ while (!m.isAfter(mm)) mm = adjustMomentForDtp(mm, -1, aav);
1232
+ setDtp($(this), mm);
1233
+ }
1234
+ });
1235
+ if (sb != null) $(sb).each(function(){
1236
+ if (immutable_field == null || !$(this).is(immutable_field)) {
1237
+ if (!$(this).val().length) setDtp($(this), m);
1238
+ var mm = moment($(this).val());
1239
+ var aav = $(this).data('autoadvance') || 'day';
1240
+ while (!m.isBefore(mm)) mm = adjustMomentForDtp(mm, 1, aav);
1241
+ setDtp($(this), mm);
1242
+ }
1243
+ });
1244
+ immutable_field = if_backup;
1245
+ };
1246
+
1197
1247
  $(".dtp").each(function() {
1198
1248
  var dV, format, m, v;
1199
- v = $(this).val();
1200
- dV = $(this).data('defaultvalue');
1249
+ var dtp = $(this);
1250
+ v = dtp.val();
1251
+ dV = dtp.data('defaultvalue');
1201
1252
  if ((v != null) && v.length > 0 && v !== 'Invalid date') {
1202
1253
  m = momentFromRubyTimestamp(v);
1203
1254
  } else if (dV != null) {
1255
+ if (typeof dV === 'string') {
1204
1256
  m = momentFromRubyTimestamp(dV);
1257
+ } else if (typeof dV === 'object') {
1258
+ m = moment();
1259
+ if (dV['year'] != null) m = m.year(dV.year);
1260
+ if (dV['month'] != null) m = m.month(dV.month - 1);
1261
+ if (dV['day'] != null) m = m.date(dV.day);
1262
+ if (dV['hour'] != null) m = m.hour(dV.hour);
1263
+ if (dV['minute'] != null) m = m.minute(dV.minute);
1264
+ if (dV['second'] != null) m = m.second(dV.second);
1265
+ }
1205
1266
  }
1206
1267
  if (m != null) {
1207
- $(this).val(m.format('MM/DD/YYYY h:mm A'));
1268
+ var aav = dtp.data('autoadvance');
1269
+ if (aav != null && m.isBefore()) m.add(1, aav);
1270
+ dtp.data('valuecache', dtp.val());
1208
1271
  }
1209
- $(this).attr('autocomplete', 'off').datetimepicker({
1272
+ dtp.attr('autocomplete', 'off').datetimepicker({
1210
1273
  sideBySide: true
1211
1274
  });
1212
- format = $(this).data('DateTimePicker').format;
1213
1275
  if (m != null) {
1214
- $(this).data('DateTimePicker').setDate(m.format(format));
1276
+ setDtp(dtp, m);
1277
+ setTimeout(function(){
1278
+ updateDependentDates(dtp);
1279
+ dtp.change(function(){ updateDependentDates(dtp); });
1280
+ }, 1);
1215
1281
  }
1216
- $(this).siblings('.input-group-addon').click((function(_this) {
1282
+ dtp.siblings('.input-group-addon').click((function(_this) {
1217
1283
  return function(e) {
1218
1284
  return $(_this).data('DateTimePicker').show();
1219
1285
  };
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_datetimepicker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - smit1625
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,3 +122,4 @@ signing_key:
122
122
  specification_version: 4
123
123
  summary: Uses bootstrap-datetimepicker to enhance SimpleForm's default datetime input
124
124
  test_files: []
125
+ has_rdoc: