picker.time.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*!
  2. * Time picker for pickadate.js v3.5.6
  3. * http://amsul.github.io/pickadate.js/time.htm
  4. */
  5. ! function (a) {
  6. "function" == typeof define && define.amd ? define(["picker", "jquery"], a) : "object" == typeof exports ? module.exports = a(require("./picker.js"), require("jquery")) : a(Picker, jQuery)
  7. }(function (a, b) {
  8. function c(a, b) {
  9. var c = this,
  10. d = a.$node[0].value,
  11. e = a.$node.data("value"),
  12. f = e || d,
  13. g = e ? b.formatSubmit : b.format;
  14. c.settings = b, c.$node = a.$node, c.queue = {
  15. interval: "i",
  16. min: "measure create",
  17. max: "measure create",
  18. now: "now create",
  19. select: "parse create validate",
  20. highlight: "parse create validate",
  21. view: "parse create validate",
  22. disable: "deactivate",
  23. enable: "activate"
  24. }, c.item = {}, c.item.clear = null, c.item.interval = b.interval || 30, c.item.disable = (b.disable || []).slice(0), c.item.enable = - function (a) {
  25. return a[0] === !0 ? a.shift() : -1
  26. }(c.item.disable), c.set("min", b.min).set("max", b.max).set("now"), f ? c.set("select", f, {
  27. format: g
  28. }) : c.set("select", null).set("highlight", c.item.now), c.key = {
  29. 40: 1,
  30. 38: -1,
  31. 39: 1,
  32. 37: -1,
  33. go: function (a) {
  34. c.set("highlight", c.item.highlight.pick + a * c.item.interval, {
  35. interval: a * c.item.interval
  36. }), this.render()
  37. }
  38. }, a.on("render", function () {
  39. var c = a.$root.children(),
  40. d = c.find("." + b.klass.viewset),
  41. e = function (a) {
  42. return ["webkit", "moz", "ms", "o", ""].map(function (b) {
  43. return (b ? "-" + b + "-" : "") + a
  44. })
  45. },
  46. f = function (a, b) {
  47. e("transform").map(function (c) {
  48. a.css(c, b)
  49. }), e("transition").map(function (c) {
  50. a.css(c, b)
  51. })
  52. };
  53. d.length && (f(c, "none"), c[0].scrollTop = ~~d.position().top - 2 * d[0].clientHeight, f(c, ""))
  54. }, 1).on("open", function () {
  55. a.$root.find("button").attr("disabled", !1)
  56. }, 1).on("close", function () {
  57. a.$root.find("button").attr("disabled", !0)
  58. }, 1)
  59. }
  60. var d = 24,
  61. e = 60,
  62. f = 12,
  63. g = d * e,
  64. h = a._;
  65. c.prototype.set = function (a, b, c) {
  66. var d = this,
  67. e = d.item;
  68. return null === b ? ("clear" == a && (a = "select"), e[a] = b, d) : (e["enable" == a ? "disable" : "flip" == a ? "enable" : a] = d.queue[a].split(" ").map(function (e) {
  69. return b = d[e](a, b, c)
  70. }).pop(), "select" == a ? d.set("highlight", e.select, c) : "highlight" == a ? d.set("view", e.highlight, c) : "interval" == a ? d.set("min", e.min, c).set("max", e.max, c) : a.match(/^(flip|min|max|disable|enable)$/) && (e.select && d.disabled(e.select) && d.set("select", b, c), e.highlight && d.disabled(e.highlight) && d.set("highlight", b, c), "min" == a && d.set("max", e.max, c)), d)
  71. }, c.prototype.get = function (a) {
  72. return this.item[a]
  73. }, c.prototype.create = function (a, c, f) {
  74. var i = this;
  75. return c = void 0 === c ? a : c, h.isDate(c) && (c = [c.getHours(), c.getMinutes()]), b.isPlainObject(c) && h.isInteger(c.pick) ? c = c.pick : b.isArray(c) ? c = +c[0] * e + +c[1] : h.isInteger(c) || (c = i.now(a, c, f)), "max" == a && c < i.item.min.pick && (c += g), "min" != a && "max" != a && (c - i.item.min.pick) % i.item.interval !== 0 && (c += i.item.interval), c = i.normalize(a, c, f), {
  76. hour: ~~(d + c / e) % d,
  77. mins: (e + c % e) % e,
  78. time: (g + c) % g,
  79. pick: c % g
  80. }
  81. }, c.prototype.createRange = function (a, c) {
  82. var d = this,
  83. e = function (a) {
  84. return a === !0 || b.isArray(a) || h.isDate(a) ? d.create(a) : a
  85. };
  86. return h.isInteger(a) || (a = e(a)), h.isInteger(c) || (c = e(c)), h.isInteger(a) && b.isPlainObject(c) ? a = [c.hour, c.mins + a * d.settings.interval] : h.isInteger(c) && b.isPlainObject(a) && (c = [a.hour, a.mins + c * d.settings.interval]), {
  87. from: e(a),
  88. to: e(c)
  89. }
  90. }, c.prototype.withinRange = function (a, b) {
  91. return a = this.createRange(a.from, a.to), b.pick >= a.from.pick && b.pick <= a.to.pick
  92. }, c.prototype.overlapRanges = function (a, b) {
  93. var c = this;
  94. return a = c.createRange(a.from, a.to), b = c.createRange(b.from, b.to), c.withinRange(a, b.from) || c.withinRange(a, b.to) || c.withinRange(b, a.from) || c.withinRange(b, a.to)
  95. }, c.prototype.now = function (a, b) {
  96. var c, d = this.item.interval,
  97. f = new Date,
  98. g = f.getHours() * e + f.getMinutes(),
  99. i = h.isInteger(b);
  100. return g -= g % d, c = 0 > b && -d >= d * b + g, g += "min" == a && c ? 0 : d, i && (g += d * (c && "max" != a ? b + 1 : b)), g
  101. }, c.prototype.normalize = function (a, b) {
  102. var c = this.item.interval,
  103. d = this.item.min && this.item.min.pick || 0;
  104. return b -= "min" == a ? 0 : (b - d) % c
  105. }, c.prototype.measure = function (a, c, f) {
  106. var g = this;
  107. return c || (c = "min" == a ? [0, 0] : [d - 1, e - 1]), "string" == typeof c ? c = g.parse(a, c) : c === !0 || h.isInteger(c) ? c = g.now(a, c, f) : b.isPlainObject(c) && h.isInteger(c.pick) && (c = g.normalize(a, c.pick, f)), c
  108. }, c.prototype.validate = function (a, b, c) {
  109. var d = this,
  110. e = c && c.interval ? c.interval : d.item.interval;
  111. return d.disabled(b) && (b = d.shift(b, e)), b = d.scope(b), d.disabled(b) && (b = d.shift(b, -1 * e)), b
  112. }, c.prototype.disabled = function (a) {
  113. var c = this,
  114. d = c.item.disable.filter(function (d) {
  115. return h.isInteger(d) ? a.hour == d : b.isArray(d) || h.isDate(d) ? a.pick == c.create(d).pick : b.isPlainObject(d) ? c.withinRange(d, a) : void 0
  116. });
  117. return d = d.length && !d.filter(function (a) {
  118. return b.isArray(a) && "inverted" == a[2] || b.isPlainObject(a) && a.inverted
  119. }).length, -1 === c.item.enable ? !d : d || a.pick < c.item.min.pick || a.pick > c.item.max.pick
  120. }, c.prototype.shift = function (a, b) {
  121. var c = this,
  122. d = c.item.min.pick,
  123. e = c.item.max.pick;
  124. for (b = b || c.item.interval; c.disabled(a) && (a = c.create(a.pick += b), !(a.pick <= d || a.pick >= e)););
  125. return a
  126. }, c.prototype.scope = function (a) {
  127. var b = this.item.min.pick,
  128. c = this.item.max.pick;
  129. return this.create(a.pick > c ? c : a.pick < b ? b : a)
  130. }, c.prototype.parse = function (a, b, c) {
  131. var d, f, g, i, j, k = this,
  132. l = {};
  133. if (!b || "string" != typeof b) return b;
  134. c && c.format || (c = c || {}, c.format = k.settings.format), k.formats.toArray(c.format).map(function (a) {
  135. var c, d = k.formats[a],
  136. e = d ? h.trigger(d, k, [b, l]) : a.replace(/^!/, "").length;
  137. d && (c = b.substr(0, e), l[a] = c.match(/^\d+$/) ? +c : c), b = b.substr(e)
  138. });
  139. for (i in l) j = l[i], h.isInteger(j) ? i.match(/^(h|hh)$/i) ? (d = j, ("h" == i || "hh" == i) && (d %= 12)) : "i" == i && (f = j) : i.match(/^a$/i) && j.match(/^p/i) && ("h" in l || "hh" in l) && (g = !0);
  140. return (g ? d + 12 : d) * e + f
  141. }, c.prototype.formats = {
  142. h: function (a, b) {
  143. return a ? h.digits(a) : b.hour % f || f
  144. },
  145. hh: function (a, b) {
  146. return a ? 2 : h.lead(b.hour % f || f)
  147. },
  148. H: function (a, b) {
  149. return a ? h.digits(a) : "" + b.hour % 24
  150. },
  151. HH: function (a, b) {
  152. return a ? h.digits(a) : h.lead(b.hour % 24)
  153. },
  154. i: function (a, b) {
  155. return a ? 2 : h.lead(b.mins)
  156. },
  157. a: function (a, b) {
  158. return a ? 4 : g / 2 > b.time % g ? "a.m." : "p.m."
  159. },
  160. A: function (a, b) {
  161. return a ? 2 : g / 2 > b.time % g ? "AM" : "PM"
  162. },
  163. toArray: function (a) {
  164. return a.split(/(h{1,2}|H{1,2}|i|a|A|!.)/g)
  165. },
  166. toString: function (a, b) {
  167. var c = this;
  168. return c.formats.toArray(a).map(function (a) {
  169. return h.trigger(c.formats[a], c, [0, b]) || a.replace(/^!/, "")
  170. }).join("")
  171. }
  172. }, c.prototype.isTimeExact = function (a, c) {
  173. var d = this;
  174. return h.isInteger(a) && h.isInteger(c) || "boolean" == typeof a && "boolean" == typeof c ? a === c : (h.isDate(a) || b.isArray(a)) && (h.isDate(c) || b.isArray(c)) ? d.create(a).pick === d.create(c).pick : b.isPlainObject(a) && b.isPlainObject(c) ? d.isTimeExact(a.from, c.from) && d.isTimeExact(a.to, c.to) : !1
  175. }, c.prototype.isTimeOverlap = function (a, c) {
  176. var d = this;
  177. return h.isInteger(a) && (h.isDate(c) || b.isArray(c)) ? a === d.create(c).hour : h.isInteger(c) && (h.isDate(a) || b.isArray(a)) ? c === d.create(a).hour : b.isPlainObject(a) && b.isPlainObject(c) ? d.overlapRanges(a, c) : !1
  178. }, c.prototype.flipEnable = function (a) {
  179. var b = this.item;
  180. b.enable = a || (-1 == b.enable ? 1 : -1)
  181. }, c.prototype.deactivate = function (a, c) {
  182. var d = this,
  183. e = d.item.disable.slice(0);
  184. return "flip" == c ? d.flipEnable() : c === !1 ? (d.flipEnable(1), e = []) : c === !0 ? (d.flipEnable(-1), e = []) : c.map(function (a) {
  185. for (var c, f = 0; f < e.length; f += 1)
  186. if (d.isTimeExact(a, e[f])) {
  187. c = !0;
  188. break
  189. }
  190. c || (h.isInteger(a) || h.isDate(a) || b.isArray(a) || b.isPlainObject(a) && a.from && a.to) && e.push(a)
  191. }), e
  192. }, c.prototype.activate = function (a, c) {
  193. var d = this,
  194. e = d.item.disable,
  195. f = e.length;
  196. return "flip" == c ? d.flipEnable() : c === !0 ? (d.flipEnable(1), e = []) : c === !1 ? (d.flipEnable(-1), e = []) : c.map(function (a) {
  197. var c, g, i, j;
  198. for (i = 0; f > i; i += 1) {
  199. if (g = e[i], d.isTimeExact(g, a)) {
  200. c = e[i] = null, j = !0;
  201. break
  202. }
  203. if (d.isTimeOverlap(g, a)) {
  204. b.isPlainObject(a) ? (a.inverted = !0, c = a) : b.isArray(a) ? (c = a, c[2] || c.push("inverted")) : h.isDate(a) && (c = [a.getFullYear(), a.getMonth(), a.getDate(), "inverted"]);
  205. break
  206. }
  207. }
  208. if (c)
  209. for (i = 0; f > i; i += 1)
  210. if (d.isTimeExact(e[i], a)) {
  211. e[i] = null;
  212. break
  213. }
  214. if (j)
  215. for (i = 0; f > i; i += 1)
  216. if (d.isTimeOverlap(e[i], a)) {
  217. e[i] = null;
  218. break
  219. }
  220. c && e.push(c)
  221. }), e.filter(function (a) {
  222. return null != a
  223. })
  224. }, c.prototype.i = function (a, b) {
  225. return h.isInteger(b) && b > 0 ? b : this.item.interval
  226. }, c.prototype.nodes = function (a) {
  227. var b = this,
  228. c = b.settings,
  229. d = b.item.select,
  230. e = b.item.highlight,
  231. f = b.item.view,
  232. g = b.item.disable;
  233. return h.node("ul", h.group({
  234. min: b.item.min.pick,
  235. max: b.item.max.pick,
  236. i: b.item.interval,
  237. node: "li",
  238. item: function (a) {
  239. a = b.create(a);
  240. var i = a.pick,
  241. j = d && d.pick == i,
  242. k = e && e.pick == i,
  243. l = g && b.disabled(a),
  244. m = h.trigger(b.formats.toString, b, [c.format, a]);
  245. return [h.trigger(b.formats.toString, b, [h.trigger(c.formatLabel, b, [a]) || c.format, a]), function (a) {
  246. return j && a.push(c.klass.selected), k && a.push(c.klass.highlighted), f && f.pick == i && a.push(c.klass.viewset), l && a.push(c.klass.disabled), a.join(" ")
  247. }([c.klass.listItem]), "data-pick=" + a.pick + " " + h.ariaAttr({
  248. role: "option",
  249. label: m,
  250. selected: j && b.$node.val() === m ? !0 : null,
  251. activedescendant: k ? !0 : null,
  252. disabled: l ? !0 : null
  253. })]
  254. }
  255. }) + h.node("li", h.node("button", c.clear, c.klass.buttonClear, "type=button data-clear=1" + (a ? "" : " disabled") + " " + h.ariaAttr({
  256. controls: b.$node[0].id
  257. })), "", h.ariaAttr({
  258. role: "presentation"
  259. })), c.klass.list, h.ariaAttr({
  260. role: "listbox",
  261. controls: b.$node[0].id
  262. }))
  263. }, c.defaults = function (a) {
  264. return {
  265. clear: "Clear",
  266. format: "h:i A",
  267. interval: 30,
  268. closeOnSelect: !0,
  269. closeOnClear: !0,
  270. klass: {
  271. picker: a + " " + a + "--time",
  272. holder: a + "__holder",
  273. list: a + "__list",
  274. listItem: a + "__list-item",
  275. disabled: a + "__list-item--disabled",
  276. selected: a + "__list-item--selected",
  277. highlighted: a + "__list-item--highlighted",
  278. viewset: a + "__list-item--viewset",
  279. now: a + "__list-item--now",
  280. buttonClear: a + "__button--clear"
  281. }
  282. }
  283. }(a.klasses().picker), a.extend("pickatime", c)
  284. });