diff options
Diffstat (limited to 'framework/Web/Javascripts/extended')
-rw-r--r-- | framework/Web/Javascripts/extended/base.js | 1 | ||||
-rw-r--r-- | framework/Web/Javascripts/extended/date.js | 147 | ||||
-rw-r--r-- | framework/Web/Javascripts/extended/dom.js | 232 | ||||
-rw-r--r-- | framework/Web/Javascripts/extended/string.js | 63 | ||||
-rw-r--r-- | framework/Web/Javascripts/extended/util.js | 94 |
5 files changed, 210 insertions, 327 deletions
diff --git a/framework/Web/Javascripts/extended/base.js b/framework/Web/Javascripts/extended/base.js index 59a3c3a0..d3d8fb63 100644 --- a/framework/Web/Javascripts/extended/base.js +++ b/framework/Web/Javascripts/extended/base.js @@ -32,3 +32,4 @@ Function.prototype.bindEvent = function() { return __method.call(object, [event || window.event].concat(args));
}
}
+
diff --git a/framework/Web/Javascripts/extended/date.js b/framework/Web/Javascripts/extended/date.js new file mode 100644 index 00000000..375c59df --- /dev/null +++ b/framework/Web/Javascripts/extended/date.js @@ -0,0 +1,147 @@ +
+Object.extend(Date.prototype,
+{
+ SimpleFormat: function(format)
+ {
+ var bits = new Array();
+ bits['d'] = this.getDate();
+ bits['dd'] = Prado.Util.pad(this.getDate(),2);
+
+ bits['M'] = this.getMonth()+1;
+ bits['MM'] = Prado.Util.pad(this.getMonth()+1,2);
+
+ var yearStr = "" + this.getFullYear();
+ yearStr = (yearStr.length == 2) ? '19' + yearStr: yearStr;
+ bits['yyyy'] = yearStr;
+ bits['yy'] = bits['yyyy'].toString().substr(2,2);
+
+ // do some funky regexs to replace the format string
+ // with the real values
+ var frm = new String(format);
+ for (var sect in bits)
+ {
+ var reg = new RegExp("\\b"+sect+"\\b" ,"g");
+ frm = frm.replace(reg, bits[sect]);
+ }
+ return frm;
+ },
+
+ toISODate : function()
+ {
+ var y = this.getFullYear();
+ var m = Prado.Util.pad(this.getMonth() + 1);
+ var d = Prado.Util.pad(this.getDate());
+ return String(y) + String(m) + String(d);
+ }
+});
+
+Object.extend(Date,
+{
+ SimpleParse: function(value, format)
+ {
+ val=String(value);
+ format=String(format);
+
+ if(val.length <= 0) return null;
+
+ if(format.length <= 0) return new Date(value);
+
+ var isInteger = function (val)
+ {
+ var digits="1234567890";
+ for (var i=0; i < val.length; i++)
+ {
+ if (digits.indexOf(val.charAt(i))==-1) { return false; }
+ }
+ return true;
+ };
+
+ var getInt = function(str,i,minlength,maxlength)
+ {
+ for (var x=maxlength; x>=minlength; x--)
+ {
+ var token=str.substring(i,i+x);
+ if (token.length < minlength) { return null; }
+ if (isInteger(token)) { return token; }
+ }
+ return null;
+ };
+
+ var i_val=0;
+ var i_format=0;
+ var c="";
+ var token="";
+ var token2="";
+ var x,y;
+ var now=new Date();
+ var year=now.getFullYear();
+ var month=now.getMonth()+1;
+ var date=1;
+
+ while (i_format < format.length)
+ {
+ // Get next token from format string
+ c=format.charAt(i_format);
+ token="";
+ while ((format.charAt(i_format)==c) && (i_format < format.length))
+ {
+ token += format.charAt(i_format++);
+ }
+
+ // Extract contents of value based on format token
+ if (token=="yyyy" || token=="yy" || token=="y")
+ {
+ if (token=="yyyy") { x=4;y=4; }
+ if (token=="yy") { x=2;y=2; }
+ if (token=="y") { x=2;y=4; }
+ year=getInt(val,i_val,x,y);
+ if (year==null) { return null; }
+ i_val += year.length;
+ if (year.length==2)
+ {
+ if (year > 70) { year=1900+(year-0); }
+ else { year=2000+(year-0); }
+ }
+ }
+
+ else if (token=="MM"||token=="M")
+ {
+ month=getInt(val,i_val,token.length,2);
+ if(month==null||(month<1)||(month>12)){return null;}
+ i_val+=month.length;
+ }
+ else if (token=="dd"||token=="d")
+ {
+ date=getInt(val,i_val,token.length,2);
+ if(date==null||(date<1)||(date>31)){return null;}
+ i_val+=date.length;
+ }
+ else
+ {
+ if (val.substring(i_val,i_val+token.length)!=token) {return null;}
+ else {i_val+=token.length;}
+ }
+ }
+
+ // If there are any trailing characters left in the value, it doesn't match
+ if (i_val != val.length) { return null; }
+
+ // Is date valid for month?
+ if (month==2)
+ {
+ // Check for leap year
+ if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
+ if (date > 29){ return null; }
+ }
+ else { if (date > 28) { return null; } }
+ }
+
+ if ((month==4)||(month==6)||(month==9)||(month==11))
+ {
+ if (date > 30) { return null; }
+ }
+
+ var newdate=new Date(year,month-1,date, 0, 0, 0);
+ return newdate;
+ }
+});
\ No newline at end of file diff --git a/framework/Web/Javascripts/extended/dom.js b/framework/Web/Javascripts/extended/dom.js index f29cf37d..21016b03 100644 --- a/framework/Web/Javascripts/extended/dom.js +++ b/framework/Web/Javascripts/extended/dom.js @@ -5,235 +5,3 @@ Object.extend(Element, { }
});
-Prado.Element =
-{
- /**
- * Set the value of a particular element.
- * @param string element id
- * @param string new element value.
- */
- setValue : function(element, value)
- {
- var el = $(element);
- if(el && typeof(el.value) != "undefined")
- el.value = value;
- },
-
- select : function(element, method, value)
- {
- var el = $(element);
- var isList = element.indexOf('[]') > -1;
- if(!el && !isList) return;
- method = isList ? 'check'+method : el.tagName.toLowerCase()+method;
- var selection = Prado.Element.Selection;
- if(isFunction(selection[method]))
- selection[method](isList ? element : el,value);
- },
-
- click : function(element)
- {
- var el = $(element);
- //Logger.info(el);
- if(!el) return;
- if(document.createEvent)
- {
- var evt = document.createEvent('HTMLEvents');
- evt.initEvent('click', true, true);
- el.dispatchEvent(evt);
- //Logger.warn("dispatching click for "+el.id);
- }
- else if(el.fireEvent)
- {
- el.fireEvent('onclick');
- if(isFunction(el.onclick))
- el.onclick();
- }
- },
-
- setAttribute : function(element, attribute, value)
- {
- var el = $(element);
- if(attribute == "disabled" && value==false)
- el.removeAttribute(attribute);
- else
- el.setAttribute(attribute, value);
- },
-
- setOptions : function(element, options)
- {
- var el = $(element);
- if(el && el.tagName.toLowerCase() == "select")
- {
- while(el.length > 0)
- el.remove(0);
- for(var i = 0; i<options.length; i++)
- el.options[el.options.length] = new Option(options[i][0],options[i][1]);
- }
- },
-/**
- * A delayed focus on a particular element
- * @param {element} element to apply focus()
- */
- focus : function(element)
- {
- var obj = $(element);
- if(isObject(obj) && isdef(obj.focus))
- setTimeout(function(){ obj.focus(); }, 100);
- return false;
- }
-}
-
-Prado.Element.Selection =
-{
- inputValue : function(el, value)
- {
- switch(el.type.toLowerCase())
- {
- case 'checkbox':
- case 'radio':
- return el.checked = value;
- }
- },
-
- selectValue : function(el, value)
- {
- $A(el.options).each(function(option)
- {
- option.selected = option.value == value;
- });
- },
-
- selectIndex : function(el, index)
- {
- if(el.type == 'select-one')
- el.selectedIndex = index;
- else
- {
- for(var i = 0; i<el.length; i++)
- {
- if(i == index)
- el.options[i].selected = true;
- }
- }
- },
-
- selectClear : function(el)
- {
- el.selectedIndex = -1;
- },
-
- selectAll : function(el)
- {
- $A(el.options).each(function(option)
- {
- option.selected = true;
- Logger.warn(option.value);
- });
- },
-
- selectInvert : function(el)
- {
- $A(el.options).each(function(option)
- {
- option.selected = !option.selected;
- });
- },
-
- checkValue : function(name, value)
- {
- $A(document.getElementsByName(name)).each(function(el)
- {
- el.checked = el.value == value
- });
- },
-
- checkIndex : function(name, index)
- {
- var elements = $A(document.getElementsByName(name));
- for(var i = 0; i<elements.length; i++)
- {
- if(i == index)
- elements[i].checked = true;
- }
- },
-
- checkClear : function(name)
- {
- $A(document.getElementsByName(name)).each(function(el)
- {
- el.checked = false;
- });
- },
-
- checkAll : function(name)
- {
- $A(document.getElementsByName(name)).each(function(el)
- {
- el.checked = true;
- });
- },
- checkInvert : function(name)
- {
- $A(document.getElementsByName(name)).each(function(el)
- {
- el.checked = !el.checked;
- });
- }
-};
-
-
-/**
- * Alias some of the prototype functions.
- * Insert a html fragment relative to an element.
- */
-Object.extend(Prado.Element,
-{
- /**
- *
- */
- Insert :
- {
- /**
- * Insert directly after the element.
- */
- After : function(element, innerHTML)
- {
- new Insertion.After(element, innerHTML);
- },
-
- /**
- * Insert directly after the element
- */
- Before : function(element, innerHTML)
- {
- new Insertion.Before(element. innerHTML);
- },
-
- /**
- * Insert below the element container.
- */
- Below : function(element, innerHTML)
- {
- new Insertion.Bottom(element, innerHTML);
- },
-
- /**
- * Insert above the element container.
- */
- Above : function(element, innerHTML)
- {
- new Insertion.Top(element, innerHTML);
- }
- },
- CssClass :
- {
- /**
- * Set the css class name of an element.
- */
- set : function(element, cssClass)
- {
- element = new Element.ClassNames(element);
- element.set(cssClass);
- }
- }
-});
\ No newline at end of file diff --git a/framework/Web/Javascripts/extended/string.js b/framework/Web/Javascripts/extended/string.js index fe586cfb..9b4c501b 100644 --- a/framework/Web/Javascripts/extended/string.js +++ b/framework/Web/Javascripts/extended/string.js @@ -57,6 +57,67 @@ Object.extend(String.prototype, { Logger.error("Missing function", this);
return Prototype.emptyFunction;
}
- }
+ },
+
+ /**
+ * Convert a string into integer, returns null if not integer.
+ * @return {integer|null} null if string does not represent an integer.
+ */
+ toInteger : function()
+ {
+ var exp = /^\s*[-\+]?\d+\s*$/;
+ if (this.match(exp) == null)
+ return null;
+ var num = parseInt(this, 10);
+ return (isNaN(num) ? null : num);
+ },
+ /**
+ * Convert a string into a double/float value. <b>Internationalization
+ * is not supported</b>
+ * @param {string} the decimal character
+ * @return {float|null} null if string does not represent a float value
+ */
+ toDouble : function(decimalchar)
+ {
+ decimalchar = decimalchar || ".";
+ var exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + decimalchar + "(\\d+))?\\s*$");
+ var m = this.match(exp);
+ if (m == null)
+ return null;
+ var cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
+ var num = parseFloat(cleanInput);
+ return (isNaN(num) ? null : num);
+ },
+
+ /**
+ * Convert strings that represent a currency value (e.g. a float with grouping
+ * characters) to float. E.g. "10,000.50" will become "10000.50". The number
+ * of dicimal digits, grouping and decimal characters can be specified.
+ * <i>The currency input format is <b>very</b> strict, null will be returned if
+ * the pattern does not match</i>.
+ * @param {string} the grouping character, default is ","
+ * @param {int} number of decimal digits
+ * @param {string} the decimal character, default is "."
+ * @type {float|null} the currency value as float.
+ */
+ toCurrency : function(groupchar, digits, decimalchar)
+ {
+ groupchar = groupchar || ",";
+ decimalchar = decimalchar || ".";
+ digits = typeof(digits) == "undefined" ? 2 : digits;
+
+ var exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + groupchar + ")*)(\\d+)"
+ + ((digits > 0) ? "(\\" + decimalchar + "(\\d{1," + digits + "}))?" : "")
+ + "\\s*$");
+ var m = this.match(exp);
+ if (m == null)
+ return null;
+ var intermed = m[2] + m[5] ;
+ var cleanInput = m[1] + intermed.replace(
+ new RegExp("(\\" + groupchar + ")", "g"), "")
+ + ((digits > 0) ? "." + m[7] : "");
+ var num = parseFloat(cleanInput);
+ return (isNaN(num) ? null : num);
+ }
});
diff --git a/framework/Web/Javascripts/extended/util.js b/framework/Web/Javascripts/extended/util.js index 7cc836f4..73a27992 100644 --- a/framework/Web/Javascripts/extended/util.js +++ b/framework/Web/Javascripts/extended/util.js @@ -89,97 +89,3 @@ function isElement(o, strict) { function isList(o) { return o && isObject(o) && (isArray(o) || o.item) }
-if(!Prado) var Prado = {};
-
-Prado.Util = {}
-
-/**
- * Pad a number with zeros from the left.
- * @param integer number
- * @param integer total string length
- * @return string zero padded number
- */
-Prado.Util.pad = function(number, X)
-{
- X = (!X ? 2 : X);
- number = ""+number;
- while (number.length < X)
- number = "0" + number;
- return number;
-}
-
-/**
- * Convert a string into integer, returns null if not integer.
- * @param {string} the string to convert to integer
- * @type {integer|null} null if string does not represent an integer.
- */
-Prado.Util.toInteger = function(value)
-{
- var exp = /^\s*[-\+]?\d+\s*$/;
- if (value.match(exp) == null)
- return null;
- var num = parseInt(value, 10);
- return (isNaN(num) ? null : num);
-}
-
-/**
- * Convert a string into a double/float value. <b>Internationalization
- * is not supported</b>
- * @param {string} the string to convert to double/float
- * @param {string} the decimal character
- * @return {float|null} null if string does not represent a float value
- */
-Prado.Util.toDouble = function(value, decimalchar)
-{
- decimalchar = undef(decimalchar) ? "." : decimalchar;
- var exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + decimalchar + "(\\d+))?\\s*$");
- var m = value.match(exp);
- if (m == null)
- return null;
- var cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
- var num = parseFloat(cleanInput);
- return (isNaN(num) ? null : num);
-}
-
-/**
- * Convert strings that represent a currency value (e.g. a float with grouping
- * characters) to float. E.g. "10,000.50" will become "10000.50". The number
- * of dicimal digits, grouping and decimal characters can be specified.
- * <i>The currency input format is <b>very</b> strict, null will be returned if
- * the pattern does not match</i>.
- * @param {string} the currency value
- * @param {string} the grouping character, default is ","
- * @param {int} number of decimal digits
- * @param {string} the decimal character, default is "."
- * @type {float|null} the currency value as float.
- */
-Prado.Util.toCurrency = function(value, groupchar, digits, decimalchar)
-{
- groupchar = undef(groupchar) ? "," : groupchar;
- decimalchar = undef(decimalchar) ? "." : decimalchar;
- digits = undef(digits) ? 2 : digits;
-
- var exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + groupchar + ")*)(\\d+)"
- + ((digits > 0) ? "(\\" + decimalchar + "(\\d{1," + digits + "}))?" : "")
- + "\\s*$");
- var m = value.match(exp);
- if (m == null)
- return null;
- var intermed = m[2] + m[5] ;
- var cleanInput = m[1] + intermed.replace(
- new RegExp("(\\" + groupchar + ")", "g"), "")
- + ((digits > 0) ? "." + m[7] : "");
- var num = parseFloat(cleanInput);
- return (isNaN(num) ? null : num);
-}
-
-/**
- * Trim the value, if the value is undefined, empty string is return.
- * @param {string} string to be trimmed.
- * @type {string} trimmed string.
- */
-Prado.Util.trim = function(value)
-{
- if(!isString(value)) return "";
- return value.replace(/^\s+|\s+$/g, "");
-}
|