Skip to content

Commit

Permalink
added basic html5 input fallback module
Browse files Browse the repository at this point in the history
  • Loading branch information
frankabbruzzese committed May 3, 2016
1 parent adf5324 commit de2d75d
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 3 deletions.
151 changes: 151 additions & 0 deletions enhancer-modules/mvcct.enhancer.input.basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
(function () {
var DEBUG = true;
(function (undefined) {
var window = this || (0, eval)('this');
(function (factory) {
if (typeof define === 'function' && define['amd']) {
// [1] AMD anonymous module
define(["../mvcct.enhancer"], factory);
} else if (typeof exports === 'object' && typeof module === 'object') {
// [2] CommonJS/Node.js
module["exports"] = factory(require("mvcct-enhancer")); // module.exports is for Node.js
} else {
// [3] No module loader (plain <script> tag) - put directly in global namespace
var mvcct = window["mvcct"] = window["mvcct"] || {};
factory(mvcct['enhancer'], window["globalize"]);
}
}(

(function (Enhancer) {

//Start actual code
var defaults = {
"dateFormat": { "date": "short" },
"timeFormat": { "skeleton": "Hms" },
"timeFormat1": { "skeleton": "Hm" },
"datetimeFormat": { "skeleton": "yMdHms" },
"datetimeFormat1": { "skeleton": "yMdHm" },
"monthFormat": { "date": "short" },
"weekFormat": { "date": "short" }
};
var options={};
var initialized = false;
function getType(node) {
var type = node.getAttribute("data-val-correcttype-type") || "3";
return parseInt(type);
}
function getDateOfISOWeek(cw) {
var parts=cw.split('-W');

var y=parseInt(parts[0]);
var w = parseInt(parts[1]);

var simple = new Date(Date.UTC(y, 0, 1 + (w - 1) * 7));
var dow = simple.getDay();
var ISOweekStart = simple;
if (dow <= 4)
ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
else
ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
return ISOweekStart;
}
function initialize(toptions, Globalize){
if(initialized) return;
var userOptions=toptions["editFormats"] || {};
for(var prop in defaults){
options[prop]=userOptions[prop] === undefined ? defaults[prop] : userOptions[prop];
}
var locale = Globalize["locale"]()["attributes"]["language"];
var nInfos = Globalize["cldr"]["get"]('main/' + locale)["numbers"]["symbols-numberSystem-latn"];

function numericInputCharHandler(charCode, el, decimalSeparator, plus, minus) {
var type = getType(el);
var canDecimal = type==3, canNegative = type != 1;
var min = el.getAttribute("min");
if (!min) min = el.getAttribute("data-val-range-min");
if (min && parseFloat(min) >= 0) canNegative = false;
if (charCode == 0 || charCode == 13 || charCode == 9 || charCode == 8 || (charCode >= 48 && charCode <= 57)) return true;
if (canNegative && (charCode == plus.charCodeAt(0) || charCode == minus.charCodeAt(0))) {
var value = el.value;
return value.indexOf(plus) < 0 && value.indexOf(minus) < 0;
}
if (canDecimal && charCode == decimalSeparator.charCodeAt(0)) {
var value = el.value;
return value.indexOf(decimalSeparator) < 0;
}
return false;
}
// event.type must be keypress
function getChar(event) {
if (event.which == null) {
return event.keyCode // IE
} else if (event.which!=0 && event.charCode!=0) {
return event.which // the rest
} else {
return null; // special key
}
}

function enhanceNumeric(input) {
if (input.getAttribute("type") != "text") return;
input.addEventListener('keypress', function (event) {
event = event || window.event;
if (!numericInputCharHandler(getChar(event), input, nInfos["decimal"], nInfos["plusSign"], nInfos["minusSign"]))
event.preventDefault();
});
}
if (!toptions["browserSupport"]) toptions["browserSupport"] = {};
var handlers = toptions["browserSupport"]["handlers"];
if (!handlers) handlers = toptions["browserSupport"]["handlers"] = {};
if (!handlers["enhance"]) handlers["enhance"] = {};
if (!handlers["enhance"]["number"]) handlers["enhance"]["number"] = enhanceNumeric;
if (!handlers["enhance"]["range"]) handlers["enhance"]["range"] = enhanceNumeric;
if (!handlers["translateVal"]) {

var neutralTimeParser = Globalize["dateParser"]({ raw: "HH:mm:ss" });
var sTimeParser = Globalize["dateParser"]({ raw: "HH:mm" });
var gtimeParser = function (x) { return neutralTimeParser(x) || sTimeParser(x); };
var neutralDateTimeParser = Globalize["dateParser"]({ raw: "yyyy-MM-ddTHH:mm:ss" });
var sDateTimeParser = Globalize["dateParser"]({ raw: "yyyy-MM-ddTHH:mm" });
var gDatetimeParser = function (x) { return neutralDateTimeParser(x) || sDateTimeParser(x);};
var neutralDateParser = Globalize["dateParser"]({ raw: "yyyy-MM-dd" });
var neutralMonthParser = Globalize["dateParser"]({ raw: "yyyy-MM" });

var numberFormatter = Globalize["numberFormatter"]();
var timeFormatter = Globalize["dateFormatter"](options["timeFormat"]);
var dateTimeFormatter = Globalize["dateFormatter"](options["datetimeFormat1"]);
var dateFormatter = Globalize["dateFormatter"](options["dateFormat"]);

var dict = {
"range": function (x) { return x ? numberFormatter(parseFloat(x)) : ""; },
"number": function (x) { return x ? numberFormatter(parseFloat(x)) : ""; },
"time": function (x) { return x ? timeFormatter(gtimeParser(x)) : ""; },
"datetime": function (x) { return x ? dateTimeFormatter(gDatetimeParser(x)) : ""; },
"date": function (x) { return x ? dateFormatter(neutralDateParser(x)) : ""; },
"month": function (x) { return x ? dateFormatter(neutralMonthParser(x)) : ""; },
"week": function (x) {
return dateFormatter(getDateOfISOWeek(x));
}

};
handlers["translateVal"] = function (value, type, input) {
var tr = dict[type];
if (tr) return tr(value, type, input);
else return value;
};
}


initialized = true;
}

Enhancer["addBasicInput"]=function(Globalize){
Enhancer["register"](null, false, function(x) {initialize(x, Globalize);}, "html5 globalized fallback", true);
}

//Finish actual code
return Enhancer;
})
));
}());
})();
6 changes: 6 additions & 0 deletions enhancer-modules/mvcct.enhancer.input.basic.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var gulp = require('gulp');
var closureCompiler = require('gulp-closure-compiler');

gulp.task('default', function() {
gulp.task('main:compile', function() {
return gulp.src(["mvcct.enhancer.js"], { base: "." })
.pipe(closureCompiler({
//compilerPath: 'node_modules/gulp-closure-compiler/node_modules/google-closure-compiler/compiler.jar',
Expand All @@ -11,4 +11,16 @@ gulp.task('default', function() {
}
}))
.pipe(gulp.dest('.'));
});
});
gulp.task('input:basic:compile', function() {
return gulp.src(["enhancer-modules/mvcct.enhancer.input.basic.js"], { base: "." })
.pipe(closureCompiler({
//compilerPath: 'node_modules/gulp-closure-compiler/node_modules/google-closure-compiler/compiler.jar',
fileName: 'mvcct.enhancer.input.basic.min.js',
compilerFlags: {
compilation_level: 'ADVANCED_OPTIMIZATIONS'
}
}))
.pipe(gulp.dest('./enhancer-modules'));
});
gulp.task("default", ["main:compile", "input:basic:compile"]);
13 changes: 12 additions & 1 deletion mvcct.enhancer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
forms: string;
fallbacks: InputSupportOptions;
}
interface Html5InputOriginalSupport {
export interface Html5InputOriginalSupport {
number: boolean;
range: boolean;
date: boolean;
Expand Down Expand Up @@ -51,8 +51,18 @@
url: number;
color: number;
}
export interface Formats{
dateFormat: string;
timeFormat: string;
timeFormat1: string;
datetimeFormat: string;
datetimeFormat1: string;
monthFormat: string;
weekFormat: string;
}
export interface Options {
browserSupport?: BrowserSupportOptions;
editFormats?: Formats;
[propName: string]: any;
}
export interface Html5Infos {
Expand All @@ -76,6 +86,7 @@
action: (targetNode: HTMLElement, sourceNode: HTMLElement) => void): any;
export function removeDependency(handle: any): void;
export function getSupport(): Html5Infos;
export function addBasicInput(globalize: any);
}
}

Expand Down

0 comments on commit de2d75d

Please sign in to comment.