From cf4a283af81bcd0af8dd91cad1f66ea9e90fbca4 Mon Sep 17 00:00:00 2001 From: francesco Date: Fri, 3 Feb 2017 16:49:30 +0100 Subject: [PATCH] Added timezone handling Now original type of a falled back input is stored in the new input --- bower.json | 2 +- mvcct.enhancer.js | 64 +++++++++++++++++++++++++++++++++++++++---- mvcct.enhancer.min.js | 22 ++++++++------- package.json | 2 +- 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/bower.json b/bower.json index 25ff938..aefd1ae 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "mvcct-enhancer", - "version": "1.0.1", + "version": "1.0.2", "homepage": "https://github.com/MvcControlsToolkit/mvcct-enhancer", "description": "a javascript package to handle standard html enhancements", "main": [ diff --git a/mvcct.enhancer.js b/mvcct.enhancer.js index 9b9da43..feab596 100644 --- a/mvcct.enhancer.js +++ b/mvcct.enhancer.js @@ -170,7 +170,9 @@ //html5 enhancement (function (enhancer) { var originalSupport = autodetect(); - var processedSupport = {}; + var processedSupport = { + "clientTimeZoneOffset": new Date().getTimezoneOffset() + }; var html5Infos = { "Html5InputOriginalSupport": originalSupport, "Html5InputSupport": processedSupport @@ -212,7 +214,7 @@ function copyAttrs(elm, mains) { for (var iAttr = 0; iAttr < elm.attributes.length; iAttr++) { var attribute = elm.attributes[iAttr].name; - if (attribute === "min" || attribute === "max" || attribute === "type" || attribute === "value") continue; + if (attribute === "min" || attribute === "max" || attribute == "step" || attribute === "type" || attribute === "value") continue; else mains.setAttribute(attribute, elm.attributes[iAttr].value); } } @@ -302,16 +304,64 @@ } function processAllNodes(ancestor) { if(!html5ProcessInfos["fallbackHtml5"]) return; - if (ancestor.tagName == "INPUT") process(ancestor); + var parse = enhancer["parse"]; + var format = enhancer["format"]; + if(format && parse){ + format = function(x){return enhancer["format"]("datetime", x, true);}; + parse = function(x){return enhancer["parse"]("datetime", x, true);}; + } + if (ancestor.tagName == "INPUT") process(ancestor, parse, format); else { var allInputs = ancestor.querySelectorAll("input"); - for (var i = 0; i < allInputs.length; i++) process(allInputs[i]); + for (var i = 0; i < allInputs.length; i++) process(allInputs[i], parse, format); } } - function process(node) { + function addClientOffset(x, parse, format, ret) + { + if(!x) return x; + x=x.trim(); + if(!x) return x; + var din = parse(x); + var off = -din.getTimezoneOffset(); + if (ret) ret["off"]=off; + return format(new Date(din.getTime()+off*1000*60)); + } + function addOffsetDetectStandard(evt){ + var el=evt["target"]; + var date = enhancer["parse"]("datetime", el.value); + if(date) + el.nextElementSibling.value=-date.getTimezoneOffset(); + } + function addOffsetDetect (node) + { + + node.addEventListener('blur', addOffsetDetectStandard); + } + function process(node, parse, format) { var type = node.getAttribute("type"); var stype = type == "datetime-local" ? "datetime" : type; - if (processedSupport[stype] > 3) return; + var addListener =false; + if (stype == "datetime" && parse && format && node.getAttribute("data-is-utc")){ + var toOff = {"off": 0}; + node.value = addClientOffset(node.value, parse, format, toOff); + node.setAttribute('value', node.value); + node.nextElementSibling.value=toOff["off"]; + var min = node.getAttribute("min"); + if (min) node.setAttribute("min", addClientOffset(min, parse, format)); + min = node.getAttribute("data-val-range-min"); + if (min) node.setAttribute("data-val-range-min", addClientOffset(min, parse, format)); + var max = node.getAttribute("max"); + if (max) node.setAttribute("max", addClientOffset(max, parse, format)); + max = node.getAttribute("data-val-range-max"); + if (max) node.setAttribute("data-val-range-max", addClientOffset(max, parse, format)); + addListener = true; + + } + if (processedSupport[stype] > 3) + { + if(addListener) addOffsetDetect (node); + return; + } var replace = handlers["replace"](type, processedSupport); if (replace == type) return; if (handlers["fullReplace"]) { @@ -321,9 +371,11 @@ var input = document.createElement("input"); input.setAttribute("type", replace); input.setAttribute("value", handlers["translateVal"](node.getAttribute("value"), stype, replace)); + input.setAttribute("data-original-type", type); copyAttrs(node, input); if(type == "range") input.setAttribute("data-is-range", "true"); node.parentNode.replaceChild(input, node); + if(addListener) addOffsetDetect (input); if (handlers["enhance"] && handlers["enhance"][stype]) handlers["enhance"][stype](input, node); } enhancer["register"](null, false, function (options) { options = options || {}; preProcessOptions(options["browserSupport"]) }, "html5 support"); diff --git a/mvcct.enhancer.min.js b/mvcct.enhancer.min.js index bce8067..4010e28 100644 --- a/mvcct.enhancer.min.js +++ b/mvcct.enhancer.min.js @@ -1,10 +1,12 @@ -(function(){(function(t){var r=this||(0,eval)("this");(function(g){if("function"===typeof define&&define.amd)define(["exports","require"],g);else if("object"===typeof exports&&"object"===typeof module)g(module.exports||exports);else{var n=r.mvcct=r.mvcct||{};g(n.enhancer={})}})(function(g){function n(c){for(var b=0;bb.l?"text":"number":a};m.handlers.translateVal=a.handlers&&a.handlers.translateVal?a.handlers.translateVal:function(a){return a};l=m.handlers;l.fullReplace=a&&a.handlers?a.handlers.fullReplace:null;l.enhance=a&&a.handlers?a.handlers.enhance:{};e()}function g(a){var b=a.getAttribute("type"),c="datetime-local"==b?"datetime":b;if(!(3c.l?"text":"number":a};n.handlers.translateVal=a.handlers&&a.handlers.translateVal?a.handlers.translateVal:function(a){return a};m=n.handlers;m.fullReplace=a&&a.handlers?a.handlers.fullReplace:null;m.enhance=a&&a.handlers?a.handlers.enhance:{};f()}function h(a,c,b,d){if(!a)return a;a=a.trim();if(!a)return a;a=c(a);c=-a.getTimezoneOffset();d&&(d.off=c);return b(new Date(a.getTime()+6E4*c))}function k(a){a=a.target;var b=c.parse("datetime", +a.value);b&&(a.nextElementSibling.value=-b.getTimezoneOffset())}function q(a,c,b){var d=a.getAttribute("type"),f="datetime-local"==d?"datetime":d,e=!1;"datetime"==f&&c&&b&&a.getAttribute("data-is-utc")&&(e={off:0},a.value=h(a.value,c,b,e),a.setAttribute("value",a.value),a.nextElementSibling.value=e.off,(e=a.getAttribute("min"))&&a.setAttribute("min",h(e,c,b)),(e=a.getAttribute("data-val-range-min"))&&a.setAttribute("data-val-range-min",h(e,c,b)),(e=a.getAttribute("max"))&&a.setAttribute("max",h(e, +c,b)),(e=a.getAttribute("data-val-range-max"))&&a.setAttribute("data-val-range-max",h(e,c,b)),e=!0);if(3