[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Fmsystem-commits] [12637]
From: |
Nelson Guerra |
Subject: |
[Fmsystem-commits] [12637] |
Date: |
Tue, 20 Jan 2015 23:09:31 +0000 |
Revision: 12637
http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=12637
Author: nelson224
Date: 2015-01-20 23:09:30 +0000 (Tue, 20 Jan 2015)
Log Message:
-----------
Added Paths:
-----------
branches/dev-syncromind/phpgwapi/js/jquery/number-format/
branches/dev-syncromind/phpgwapi/js/jquery/number-format/LICENSE.txt
branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.js
branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.min.js
Added: branches/dev-syncromind/phpgwapi/js/jquery/number-format/LICENSE.txt
===================================================================
--- branches/dev-syncromind/phpgwapi/js/jquery/number-format/LICENSE.txt
(rev 0)
+++ branches/dev-syncromind/phpgwapi/js/jquery/number-format/LICENSE.txt
2015-01-20 23:09:30 UTC (rev 12637)
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Digital Fusion, http://teamdf.com/
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
Added: branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.js
===================================================================
--- branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.js
(rev 0)
+++ branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.js
2015-01-20 23:09:30 UTC (rev 12637)
@@ -0,0 +1,765 @@
+/**
+ * jQuery number plug-in 2.1.5
+ * Copyright 2012, Digital Fusion
+ * Licensed under the MIT license.
+ * http://opensource.teamdf.com/license/
+ *
+ * A jQuery plugin which implements a permutation of phpjs.org's number_format
to provide
+ * simple number formatting, insertion, and as-you-type masking of a number.
+ *
+ * @author Sam Sehnert
+ * @docs http://www.teamdf.com/web/jquery-number-format-redux/196/
+ */
+(function($){
+
+ "use strict";
+
+ /**
+ * Method for selecting a range of characters in an input/textarea.
+ *
+ * @param int rangeStart : Where we want the
selection to start.
+ * @param int rangeEnd : Where we want the
selection to end.
+ *
+ * @return void;
+ */
+ function setSelectionRange( rangeStart, rangeEnd )
+ {
+ // Check which way we need to define the text range.
+ if( this.createTextRange )
+ {
+ var range = this.createTextRange();
+ range.collapse( true );
+ range.moveStart( 'character', rangeStart );
+ range.moveEnd( 'character',
rangeEnd-rangeStart );
+ range.select();
+ }
+
+ // Alternate setSelectionRange method for supporting browsers.
+ else if( this.setSelectionRange )
+ {
+ this.focus();
+ this.setSelectionRange( rangeStart, rangeEnd );
+ }
+ }
+
+ /**
+ * Get the selection position for the given part.
+ *
+ * @param string part : Options, 'Start' or 'End'.
The selection position to get.
+ *
+ * @return int : The index position of the selection part.
+ */
+ function getSelection( part )
+ {
+ var pos = this.value.length;
+
+ // Work out the selection part.
+ part = ( part.toLowerCase() == 'start' ? 'Start' : 'End' );
+
+ if( document.selection ){
+ // The current selection
+ var range = document.selection.createRange(),
stored_range, selectionStart, selectionEnd;
+ // We'll use this as a 'dummy'
+ stored_range = range.duplicate();
+ // Select all text
+ //stored_range.moveToElementText( this );
+ stored_range.expand('textedit');
+ // Now move 'dummy' end point to end point of original
range
+ stored_range.setEndPoint( 'EndToEnd', range );
+ // Now we can calculate start and end points
+ selectionStart = stored_range.text.length -
range.text.length;
+ selectionEnd = selectionStart + range.text.length;
+ return part == 'Start' ? selectionStart : selectionEnd;
+ }
+
+ else if(typeof(this['selection'+part])!="undefined")
+ {
+ pos = this['selection'+part];
+ }
+ return pos;
+ }
+
+ /**
+ * Substitutions for keydown keycodes.
+ * Allows conversion from e.which to ascii characters.
+ */
+ var _keydown = {
+ codes : {
+ 46 : 127,
+ 188 : 44,
+ 109 : 45,
+ 190 : 46,
+ 191 : 47,
+ 192 : 96,
+ 220 : 92,
+ 222 : 39,
+ 221 : 93,
+ 219 : 91,
+ 173 : 45,
+ 187 : 61, //IE Key codes
+ 186 : 59, //IE Key codes
+ 189 : 45, //IE Key codes
+ 110 : 46 //IE Key codes
+ },
+ shifts : {
+ 96 : "~",
+ 49 : "!",
+ 50 : "@",
+ 51 : "#",
+ 52 : "$",
+ 53 : "%",
+ 54 : "^",
+ 55 : "&",
+ 56 : "*",
+ 57 : "(",
+ 48 : ")",
+ 45 : "_",
+ 61 : "+",
+ 91 : "{",
+ 93 : "}",
+ 92 : "|",
+ 59 : ":",
+ 39 : "\"",
+ 44 : "<",
+ 46 : ">",
+ 47 : "?"
+ }
+ };
+
+ /**
+ * jQuery number formatter plugin. This will allow you to format
numbers on an element.
+ *
+ * @params proxied for format_number method.
+ *
+ * @return : The jQuery collection the method was called with.
+ */
+ $.fn.number = function( number, decimals, dec_point, thousands_sep ){
+
+ // Enter the default thousands separator, and the decimal
placeholder.
+ thousands_sep = (typeof thousands_sep === 'undefined') ? ','
: thousands_sep;
+ dec_point = (typeof dec_point === 'undefined') ?
'.' : dec_point;
+ decimals = (typeof decimals === 'undefined' ) ?
0 : decimals;
+
+ // Work out the unicode character for the decimal placeholder.
+ var u_dec =
('\\u'+('0000'+(dec_point.charCodeAt(0).toString(16))).slice(-4)),
+ regex_dec_num = new RegExp('[^'+u_dec+'0-9]','g'),
+ regex_dec = new RegExp(u_dec,'g');
+
+ // If we've specified to take the number from the target
element,
+ // we loop over the collection, and get the number.
+ if( number === true )
+ {
+ // If this element is a number, then we add a keyup
+ if( this.is('input:text') )
+ {
+ // Return the jquery collection.
+ return this.on({
+
+ /**
+ * Handles keyup events, re-formatting
numbers.
+ *
+ * Uses 'data' object to keep track of
important information.
+ *
+ * data.c
+ * This variable keeps track of where
the caret *should* be. It works out the position as
+ * the number of characters from the
end of the string. E.g., '1^,234.56' where ^ denotes the caret,
+ * would be index -7 (e.g., 7
characters from the end of the string). At the end of both the key down
+ * and key up events, we'll re-position
the caret to wherever data.c tells us the cursor should be.
+ * This gives us a mechanism for
incrementing the cursor position when we come across decimals, commas
+ * etc. This figure typically doesn't
increment for each keypress when to the left of the decimal,
+ * but does when to the right of the
decimal.
+ *
+ * @param object e
: the keyup event object.s
+ *
+ * @return void;
+ */
+ 'keydown.format' : function(e){
+
+ // Define variables used in the
code below.
+ var $this = $(this),
+ data =
$this.data('numFormat'),
+ code = (e.keyCode ?
e.keyCode : e.which),
+ chara = '',
//unescape(e.originalEvent.keyIdentifier.replace('U+','%u')),
+ start =
getSelection.apply(this,['start']),
+ end =
getSelection.apply(this,['end']),
+ val = '',
+ setPos = false;
+
+ // Webkit (Chrome & Safari) on
windows screws up the keyIdentifier detection
+ // for numpad characters. I've
disabled this for now, because while keyCode munging
+ // below is hackish and ugly,
it actually works cross browser & platform.
+
+// if( typeof
e.originalEvent.keyIdentifier !== 'undefined' )
+// {
+// chara =
unescape(e.originalEvent.keyIdentifier.replace('U+','%u'));
+// }
+// else
+// {
+ if
(_keydown.codes.hasOwnProperty(code)) {
+ code =
_keydown.codes[code];
+ }
+ if (!e.shiftKey &&
(code >= 65 && code <= 90)){
+ code += 32;
+ } else if (!e.shiftKey
&& (code >= 69 && code <= 105)){
+ code -= 48;
+ } else if (e.shiftKey
&& _keydown.shifts.hasOwnProperty(code)){
+ //get shifted
keyCode value
+ chara =
_keydown.shifts[code];
+ }
+
+ if( chara == '' ) chara
= String.fromCharCode(code);
+// }
+
+
+
+
+ // Stop executing if the user
didn't type a number key, a decimal character, backspace, or delete.
+ if( code != 8 && code != 45 &&
code != 127 && chara != dec_point && !chara.match(/[0-9]/) )
+ {
+ // We need the original
keycode now...
+ var key = (e.keyCode ?
e.keyCode : e.which);
+ if( // Allow control
keys to go through... (delete, backspace, tab, enter, escape etc)
+ key == 46 ||
key == 8 || key == 127 || key == 9 || key == 27 || key == 13 ||
+ // Allow:
Ctrl+A, Ctrl+R, Ctrl+P, Ctrl+S, Ctrl+F, Ctrl+H, Ctrl+B, Ctrl+J, Ctrl+T, Ctrl+Z,
Ctrl++, Ctrl+-, Ctrl+0
+ ( (key == 65 ||
key == 82 || key == 80 || key == 83 || key == 70 || key == 72 || key == 66 ||
key == 74 || key == 84 || key == 90|| key == 61 || key == 173 || key == 48) &&
( e.ctrlKey || e.metaKey ) === true ) ||
+ // Allow:
Ctrl+V, Ctrl+C, Ctrl+X
+ ( (key == 86 ||
key == 67 || key == 88) && ( e.ctrlKey || e.metaKey ) === true ) ||
+ // Allow: home,
end, left, right
+ ( (key >= 35 &&
key <= 39) ) ||
+ // Allow: F1-F12
+ ( (key >= 112
&& key <= 123) )
+ ){
+ return;
+ }
+ // But prevent all
other keys.
+ e.preventDefault();
+ return false;
+ }
+
+ // The whole lot has been
selected, or if the field is empty...
+ if( start == 0 && end ==
this.value.length || $this.val() == 0 )
+ {
+ if( code == 8 )
// Backspace
+ {
+ // Blank out
the field, but only if the data object has already been instantiated.
+ start = end = 1;
+ this.value = '';
+
+ // Reset the
cursor position.
+ data.init =
(decimals>0?-1:0);
+ data.c =
(decimals>0?-(decimals+1):0);
+
setSelectionRange.apply(this, [0,0]);
+ }
+ else if( chara ==
dec_point )
+ {
+ start = end = 1;
+ this.value =
'0'+ dec_point + (new Array(decimals+1).join('0'));
+
+ // Reset the
cursor position.
+ data.init =
(decimals>0?1:0);
+ data.c =
(decimals>0?-(decimals+1):0);
+ }
+ else if( code == 45 )
// Negative sign
+ {
+ start = end = 2;
+ this.value =
'-0'+dec_point + (new Array(decimals+1).join('0'));
+
+ // Reset the
cursor position.
+ data.init =
(decimals>0?1:0);
+ data.c =
(decimals>0?-(decimals+1):0);
+
+
setSelectionRange.apply(this, [2,2]);
+ }
+ else
+ {
+ // Reset the
cursor position.
+ data.init =
(decimals>0?-1:0);
+ data.c =
(decimals>0?-(decimals):0);
+ }
+ }
+
+ // Otherwise, we need to reset
the caret position
+ // based on the users selection.
+ else
+ {
+ data.c =
end-this.value.length;
+ }
+
+ // Track if partial selection
was used
+ data.isPartialSelection = start
== end ? false : true;
+
+ // If the start position is
before the decimal point,
+ // and the user has typed a
decimal point, we need to move the caret
+ // past the decimal place.
+ if( decimals > 0 && chara ==
dec_point && start == this.value.length-decimals-1 )
+ {
+ data.c++;
+ data.init =
Math.max(0,data.init);
+ e.preventDefault();
+
+ // Set the selection
position.
+ setPos =
this.value.length+data.c;
+ }
+
+ // Ignore negative sign unless
at beginning of number (and it's not already present)
+ else if( code == 45 && (start
!= 0 || this.value.indexOf('-') == 0) )
+ {
+ e.preventDefault();
+ }
+
+ // If the user is just typing
the decimal place,
+ // we simply ignore it.
+ else if( chara == dec_point )
+ {
+ data.init =
Math.max(0,data.init);
+ e.preventDefault();
+ }
+
+ // If hitting the delete key,
and the cursor is before a decimal place,
+ // we simply move the cursor to
the other side of the decimal place.
+ else if( decimals > 0 && code
== 127 && start == this.value.length-decimals-1 )
+ {
+ // Just prevent default
but don't actually move the caret here because it's done in the keyup event
+ e.preventDefault();
+ }
+
+ // If hitting the backspace
key, and the cursor is behind a decimal place,
+ // we simply move the cursor to
the other side of the decimal place.
+ else if( decimals > 0 && code
== 8 && start == this.value.length-decimals )
+ {
+ e.preventDefault();
+ data.c--;
+
+ // Set the selection
position.
+ setPos =
this.value.length+data.c;
+ }
+
+ // If hitting the delete key,
and the cursor is to the right of the decimal
+ // we replace the character
after the caret with a 0.
+ else if( decimals > 0 && code
== 127 && start > this.value.length-decimals-1 )
+ {
+ if( this.value === '' )
return;
+
+ // If the character
following is not already a 0,
+ // replace it with one.
+ if(
this.value.slice(start, start+1) != '0' )
+ {
+ val =
this.value.slice(0, start) + '0' + this.value.slice(start+1);
+ // The regex
replacement below removes negative sign from numbers...
+ // not sure why
they're necessary here when none of the other cases use them
+
//$this.val(val.replace(regex_dec_num,'').replace(regex_dec,dec_point));
+ $this.val(val);
+ }
+
+ e.preventDefault();
+
+ // Set the selection
position.
+ setPos =
this.value.length+data.c;
+ }
+
+ // If hitting the backspace
key, and the cursor is to the right of the decimal
+ // (but not directly to the
right) we replace the character preceding the
+ // caret with a 0.
+ else if( decimals > 0 && code
== 8 && start > this.value.length-decimals )
+ {
+ if( this.value === '' )
return;
+
+ // If the character
preceding is not already a 0,
+ // replace it with one.
+ if(
this.value.slice(start-1, start) != '0' )
+ {
+ val =
this.value.slice(0, start-1) + '0' + this.value.slice(start);
+ // The regex
replacement below removes negative sign from numbers...
+ // not sure why
they're necessary here when none of the other cases use them
+
//$this.val(val.replace(regex_dec_num,'').replace(regex_dec,dec_point));
+ $this.val(val);
+ }
+
+ e.preventDefault();
+ data.c--;
+
+ // Set the selection
position.
+ setPos =
this.value.length+data.c;
+ }
+
+ // If the delete key was
pressed, and the character immediately
+ // after the caret is a
thousands_separator character, simply
+ // step over it.
+ else if( code == 127 &&
this.value.slice(start, start+1) == thousands_sep )
+ {
+ // Just prevent default
but don't actually move the caret here because it's done in the keyup event
+ e.preventDefault();
+ }
+
+ // If the backspace key was
pressed, and the character immediately
+ // before the caret is a
thousands_separator character, simply
+ // step over it.
+ else if( code == 8 &&
this.value.slice(start-1, start) == thousands_sep )
+ {
+ e.preventDefault();
+ data.c--;
+
+ // Set the selection
position.
+ setPos =
this.value.length+data.c;
+ }
+
+ // If the caret is to the right
of the decimal place, and the user is entering a
+ // number, remove the following
character before putting in the new one.
+ else if(
+ decimals > 0 &&
+ start == end &&
+ this.value.length >
decimals+1 &&
+ start >
this.value.length-decimals-1 && isFinite(+chara) &&
+ !e.metaKey &&
!e.ctrlKey && !e.altKey && chara.length === 1
+ )
+ {
+ // If the character
preceding is not already a 0,
+ // replace it with one.
+ if( end ===
this.value.length )
+ {
+ val =
this.value.slice(0, start-1);
+ }
+ else
+ {
+ val =
this.value.slice(0, start)+this.value.slice(start+1);
+ }
+
+ // Reset the position.
+ this.value = val;
+ setPos = start;
+ }
+
+ // If we need to re-position
the characters.
+ if( setPos !== false )
+ {
+ //console.log('Setpos
keydown: ', setPos );
+
setSelectionRange.apply(this, [setPos, setPos]);
+ }
+
+ // Store the data on the
element.
+ $this.data('numFormat', data);
+
+ },
+
+ /**
+ * Handles keyup events, re-formatting
numbers.
+ *
+ * @param object e
: the keyup event object.s
+ *
+ * @return void;
+ */
+ 'keyup.format' : function(e){
+
+ // Store these variables for
use below.
+ var $this = $(this),
+ data =
$this.data('numFormat'),
+ code = (e.keyCode ?
e.keyCode : e.which),
+ start =
getSelection.apply(this,['start']),
+ end =
getSelection.apply(this,['end']),
+ setPos;
+
+
+ // Check for negative
characters being entered at the start of the string.
+ // If there's any kind of
selection, just ignore the input.
+ if( start === 0 && end === 0 &&
( code === 189 || code === 109 ) )
+ {
+
$this.val('-'+$this.val());
+
+ start = 1;
+ data.c =
1-this.value.length;
+ data.init = 1;
+
+ $this.data('numFormat',
data);
+
+ setPos =
this.value.length+data.c;
+
setSelectionRange.apply(this, [setPos, setPos]);
+ }
+
+ // Stop executing if the user
didn't type a number key, a decimal, or a comma.
+ if( this.value === '' || (code
< 48 || code > 57) && (code < 96 || code > 105 ) && code !== 8 && code !== 46
&& code !== 110 ) return;
+
+ // Re-format the textarea.
+ $this.val($this.val());
+
+ if( decimals > 0 )
+ {
+ // If we haven't marked
this item as 'initialized'
+ // then do so now. It
means we should place the caret just
+ // before the decimal.
This will never be un-initialized before
+ // the decimal
character itself is entered.
+ if( data.init < 1 )
+ {
+ start
= this.value.length-decimals-( data.init < 0 ? 1 : 0 );
+ data.c
= start-this.value.length;
+ data.init
= 1;
+
+
$this.data('numFormat', data);
+ }
+
+ // Increase the cursor
position if the caret is to the right
+ // of the decimal
place, and the character pressed isn't the backspace key.
+ else if( start >
this.value.length-decimals && code != 8 )
+ {
+ data.c++;
+
+ // Store the
data, now that it's changed.
+
$this.data('numFormat', data);
+ }
+ }
+
+ // Move caret to the right
after delete key pressed
+ if (code == 46 &&
!data.isPartialSelection)
+ {
+ data.c++;
+
+ // Store the data, now
that it's changed.
+ $this.data('numFormat',
data);
+ }
+
+ //console.log( 'Setting pos: ',
start, decimals, this.value.length + data.c, this.value.length, data.c );
+
+ // Set the selection position.
+ setPos =
this.value.length+data.c;
+ setSelectionRange.apply(this,
[setPos, setPos]);
+ },
+
+ /**
+ * Reformat when pasting into the field.
+ *
+ * @param object e :
jQuery event object.
+ *
+ * @return false : prevent default
action.
+ */
+ 'paste.format' : function(e){
+
+ // Defint $this. It's used
twice!.
+ var $this =
$(this),
+ original =
e.originalEvent,
+ val = null;
+
+ // Get the text content stream.
+ if (window.clipboardData &&
window.clipboardData.getData) { // IE
+ val =
window.clipboardData.getData('Text');
+ } else if
(original.clipboardData && original.clipboardData.getData) {
+ val =
original.clipboardData.getData('text/plain');
+ }
+
+ // Do the reformat operation.
+ $this.val(val);
+
+ // Stop the actual content from
being pasted.
+ e.preventDefault();
+ return false;
+ }
+
+ })
+
+ // Loop each element (which isn't blank) and do
the format.
+ .each(function(){
+
+ var $this = $(this).data('numFormat',{
+ c
: -(decimals+1),
+ decimals :
decimals,
+ thousands_sep : thousands_sep,
+ dec_point :
dec_point,
+ regex_dec_num : regex_dec_num,
+ regex_dec :
regex_dec,
+ init :
this.value.indexOf('.') ? true : false
+ });
+
+ // Return if the element is empty.
+ if( this.value === '' ) return;
+
+ // Otherwise... format!!
+ $this.val($this.val());
+ });
+ }
+ else
+ {
+ // return the collection.
+ return this.each(function(){
+ var $this = $(this), num =
+$this.text().replace(regex_dec_num,'').replace(regex_dec,'.');
+ $this.number( !isFinite(num) ? 0 :
+num, decimals, dec_point, thousands_sep );
+ });
+ }
+ }
+
+ // Add this number to the element as text.
+ return this.text( $.number.apply(window,arguments) );
+ };
+
+ //
+ // Create .val() hooks to get and set formatted numbers in inputs.
+ //
+
+ // We check if any hooks already exist, and cache
+ // them in case we need to re-use them later on.
+ var origHookGet = null, origHookSet = null;
+
+ // Check if a text valHook already exists.
+ if( $.isPlainObject( $.valHooks.text ) )
+ {
+ // Preserve the original valhook function
+ // we'll call this for values we're not
+ // explicitly handling.
+ if( $.isFunction( $.valHooks.text.get ) ) origHookGet =
$.valHooks.text.get;
+ if( $.isFunction( $.valHooks.text.set ) ) origHookSet =
$.valHooks.text.set;
+ }
+ else
+ {
+ // Define an object for the new valhook.
+ $.valHooks.text = {};
+ }
+
+ /**
+ * Define the valHook to return normalised field data against an input
+ * which has been tagged by the number formatter.
+ *
+ * @param object el : The raw DOM element that
we're getting the value from.
+ *
+ * @return mixed : Returns the value that was written to the element as a
+ * javascript number, or undefined to
let jQuery handle it normally.
+ */
+ $.valHooks.text.get = function( el ){
+
+ // Get the element, and its data.
+ var $this = $(el), num, negative,
+ data = $this.data('numFormat');
+
+ // Does this element have our data field?
+ if( !data )
+ {
+ // Check if the valhook function already existed
+ if( $.isFunction( origHookGet ) )
+ {
+ // There was, so go ahead and call it
+ return origHookGet(el);
+ }
+ else
+ {
+ // No previous function, return undefined to
have jQuery
+ // take care of retrieving the value
+ return undefined;
+ }
+ }
+ else
+ {
+ // Remove formatting, and return as number.
+ if( el.value === '' ) return '';
+
+
+ // Convert to a number.
+ num = +(el.value
+ .replace( data.regex_dec_num, '' )
+ .replace( data.regex_dec, '.' ));
+
+ // If we've got a finite number, return it.
+ // Otherwise, simply return 0.
+ // Return as a string... thats what we're
+ // used to with .val()
+ return (el.value.indexOf('-') === 0 ? '-' : '')+(
isFinite( num ) ? num : 0 );
+ }
+ };
+
+ /**
+ * A valhook which formats a number when run against an input
+ * which has been tagged by the number formatter.
+ *
+ * @param object el : The raw DOM element (input element).
+ * @param float : The number to set into the value
field.
+ *
+ * @return mixed : Returns the value that was written to the element,
+ * or undefined to let jQuery handle it
normally.
+ */
+ $.valHooks.text.set = function( el, val )
+ {
+ // Get the element, and its data.
+ var $this = $(el),
+ data = $this.data('numFormat');
+
+ // Does this element have our data field?
+ if( !data )
+ {
+
+ // Check if the valhook function already exists
+ if( $.isFunction( origHookSet ) )
+ {
+ // There was, so go ahead and call it
+ return origHookSet(el,val);
+ }
+ else
+ {
+ // No previous function, return undefined to
have jQuery
+ // take care of retrieving the value
+ return undefined;
+ }
+ }
+ else
+ {
+ var num = $.number( val, data.decimals, data.dec_point,
data.thousands_sep );
+
+ // Make sure empties are set with correct signs.
+// if(val.indexOf('-') === 0 && +num === 0)
+// {
+// num = '-'+num;
+// }
+ // Otherwise, don't worry about other valhooks, just
run ours.
+ return el.value = num;
+ }
+ };
+
+ /**
+ * The (modified) excellent number formatting method from PHPJS.org.
+ * http://phpjs.org/functions/number_format/
+ *
+ * @modified by Sam Sehnert (teamdf.com)
+ * - don't redefine dec_point, thousands_sep... just overwrite
with defaults.
+ * - don't redefine decimals, just overwrite as numeric.
+ * - Generate regex for normalizing pre-formatted numbers.
+ *
+ * @param float number : The number you wish to
format, or TRUE to use the text contents
+ * of
the element as the number. Please note that this won't work for
+ *
elements which have child nodes with text content.
+ * @param int decimals : The number of decimal places
that should be displayed. Defaults to 0.
+ * @param string dec_point : The character to use as a
decimal point. Defaults to '.'.
+ * @param string thousands_sep : The character to use as a thousands
separator. Defaults to ','.
+ *
+ * @return string : The formatted number as a string.
+ */
+ $.number = function( number, decimals, dec_point, thousands_sep ){
+ // Set the default values here, instead so we can use them in
the replace below.
+ thousands_sep = (typeof thousands_sep === 'undefined') ? ','
: thousands_sep;
+ dec_point = (typeof dec_point === 'undefined') ?
'.' : dec_point;
+ decimals = !isFinite(+decimals) ? 0 :
Math.abs(decimals);
+
+ // Work out the unicode representation for the decimal place
and thousand sep.
+ var u_dec =
('\\u'+('0000'+(dec_point.charCodeAt(0).toString(16))).slice(-4));
+ var u_sep =
('\\u'+('0000'+(thousands_sep.charCodeAt(0).toString(16))).slice(-4));
+
+ // Fix the number, so that it's an actual number.
+ number = (number + '')
+ .replace('\.', dec_point) // because the number if
passed in as a float (having . as decimal point per definition) we need to
replace this with the passed in decimal point character
+ .replace(new RegExp(u_sep,'g'),'')
+ .replace(new RegExp(u_dec,'g'),'.')
+ .replace(new RegExp('[^0-9+\-Ee.]','g'),'');
+
+ var n = !isFinite(+number) ? 0 : +number,
+ s = '',
+ toFixedFix = function (n, decimals) {
+ var k = Math.pow(10, decimals);
+ return '' + Math.round(n * k) / k;
+ };
+
+ // Fix for IE parseFloat(0.55).toFixed(0) = 0;
+ s = (decimals ? toFixedFix(n, decimals) : '' +
Math.round(n)).split('.');
+ if (s[0].length > 3) {
+ s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,
thousands_sep);
+ }
+ if ((s[1] || '').length < decimals) {
+ s[1] = s[1] || '';
+ s[1] += new Array(decimals - s[1].length + 1).join('0');
+ }
+ return s.join(dec_point);
+ }
+
+})(jQuery);
Added:
branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.min.js
===================================================================
---
branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.min.js
(rev 0)
+++
branches/dev-syncromind/phpgwapi/js/jquery/number-format/jquery.number.min.js
2015-01-20 23:09:30 UTC (rev 12637)
@@ -0,0 +1,2 @@
+/*! jQuery number 2.1.5 (c) github.com/teamdf/jquery-number |
opensource.teamdf.com/license */
+(function(e){"use strict";function t(e,t){if(this.createTextRange){var
n=this.createTextRange();n.collapse(true);n.moveStart("character",e);n.moveEnd("character",t-e);n.select()}else
if(this.setSelectionRange){this.focus();this.setSelectionRange(e,t)}}function
n(e){var
t=this.value.length;e=e.toLowerCase()=="start"?"Start":"End";if(document.selection){var
n=document.selection.createRange(),r,i,s;r=n.duplicate();r.expand("textedit");r.setEndPoint("EndToEnd",n);i=r.text.length-n.text.length;s=i+n.text.length;return
e=="Start"?i:s}else if(typeof
this["selection"+e]!="undefined"){t=this["selection"+e]}return t}var
r={codes:{46:127,188:44,109:45,190:46,191:47,192:96,220:92,222:39,221:93,219:91,173:45,187:61,186:59,189:45,110:46},shifts:{96:"~",49:"!",50:"@",51:"#",52:"$",53:"%",54:"^",55:"&",56:"*",57:"(",48:")",45:"_",61:"+",91:"{",93:"}",92:"|",59:":",39:'"',44:"<",46:">",47:"?"}};e.fn.number=function(i,s,o,u){u=typeof
u==="undefined"?",":u;o=typeof o==="undefined"?".":o;s=typeof
s==="undefined"?0:s;var
a="\\u"+("0000"+o.charCodeAt(0).toString(16)).slice(-4),f=new
RegExp("[^"+a+"0-9]","g"),l=new
RegExp(a,"g");if(i===true){if(this.is("input:text")){return
this.on({"keydown.format":function(i){var
a=e(this),f=a.data("numFormat"),l=i.keyCode?i.keyCode:i.which,c="",h=n.apply(this,["start"]),p=n.apply(this,["end"]),d="",v=false;if(r.codes.hasOwnProperty(l)){l=r.codes[l]}if(!i.shiftKey&&l>=65&&l<=90){l+=32}else
if(!i.shiftKey&&l>=69&&l<=105){l-=48}else
if(i.shiftKey&&r.shifts.hasOwnProperty(l)){c=r.shifts[l]}if(c=="")c=String.fromCharCode(l);if(l!=8&&l!=45&&l!=127&&c!=o&&!c.match(/[0-9]/)){var
m=i.keyCode?i.keyCode:i.which;if(m==46||m==8||m==127||m==9||m==27||m==13||(m==65||m==82||m==80||m==83||m==70||m==72||m==66||m==74||m==84||m==90||m==61||m==173||m==48)&&(i.ctrlKey||i.metaKey)===true||(m==86||m==67||m==88)&&(i.ctrlKey||i.metaKey)===true||m>=35&&m<=39||m>=112&&m<=123){return}i.preventDefault();return
false}if(h==0&&p==this.value.length||a.val()==0){if(l==8){h=p=1;this.value="";f.init=s>0?-1:0;f.c=s>0?-(s+1):0;t.apply(this,[0,0])}else
if(c==o){h=p=1;this.value="0"+o+(new
Array(s+1)).join("0");f.init=s>0?1:0;f.c=s>0?-(s+1):0}else
if(l==45){h=p=2;this.value="-0"+o+(new
Array(s+1)).join("0");f.init=s>0?1:0;f.c=s>0?-(s+1):0;t.apply(this,[2,2])}else{f.init=s>0?-1:0;f.c=s>0?-s:0}}else{f.c=p-this.value.length}f.isPartialSelection=h==p?false:true;if(s>0&&c==o&&h==this.value.length-s-1){f.c++;f.init=Math.max(0,f.init);i.preventDefault();v=this.value.length+f.c}else
if(l==45&&(h!=0||this.value.indexOf("-")==0)){i.preventDefault()}else
if(c==o){f.init=Math.max(0,f.init);i.preventDefault()}else
if(s>0&&l==127&&h==this.value.length-s-1){i.preventDefault()}else
if(s>0&&l==8&&h==this.value.length-s){i.preventDefault();f.c--;v=this.value.length+f.c}else
if(s>0&&l==127&&h>this.value.length-s-1){if(this.value==="")return;if(this.value.slice(h,h+1)!="0"){d=this.value.slice(0,h)+"0"+this.value.slice(h+1);a.val(d)}i.preventDefault();v=this.value.length+f.c}else
if(s>0&&l==8&&h>this.value.length-s){if(this.value==="")return;if(this.value.slice(h-1,h)!="0"){d=this.value.slice(0,h-1)+"0"+this.value.slice(h);a.val(d)}i.preventDefault();f.c--;v=this.value.length+f.c}else
if(l==127&&this.value.slice(h,h+1)==u){i.preventDefault()}else
if(l==8&&this.value.slice(h-1,h)==u){i.preventDefault();f.c--;v=this.value.length+f.c}else
if(s>0&&h==p&&this.value.length>s+1&&h>this.value.length-s-1&&isFinite(+c)&&!i.metaKey&&!i.ctrlKey&&!i.altKey&&c.length===1){if(p===this.value.length){d=this.value.slice(0,h-1)}else{d=this.value.slice(0,h)+this.value.slice(h+1)}this.value=d;v=h}if(v!==false){t.apply(this,[v,v])}a.data("numFormat",f)},"keyup.format":function(r){var
i=e(this),o=i.data("numFormat"),u=r.keyCode?r.keyCode:r.which,a=n.apply(this,["start"]),f=n.apply(this,["end"]),l;if(a===0&&f===0&&(u===189||u===109)){i.val("-"+i.val());a=1;o.c=1-this.value.length;o.init=1;i.data("numFormat",o);l=this.value.length+o.c;t.apply(this,[l,l])}if(this.value===""||(u<48||u>57)&&(u<96||u>105)&&u!==8&&u!==46&&u!==110)return;i.val(i.val());if(s>0){if(o.init<1){a=this.value.length-s-(o.init<0?1:0);o.c=a-this.value.length;o.init=1;i.data("numFormat",o)}else
if(a>this.value.length-s&&u!=8){o.c++;i.data("numFormat",o)}}if(u==46&&!o.isPartialSelection){o.c++;i.data("numFormat",o)}l=this.value.length+o.c;t.apply(this,[l,l])},"paste.format":function(t){var
n=e(this),r=t.originalEvent,i=null;if(window.clipboardData&&window.clipboardData.getData){i=window.clipboardData.getData("Text")}else
if(r.clipboardData&&r.clipboardData.getData){i=r.clipboardData.getData("text/plain")}n.val(i);t.preventDefault();return
false}}).each(function(){var
t=e(this).data("numFormat",{c:-(s+1),decimals:s,thousands_sep:u,dec_point:o,regex_dec_num:f,regex_dec:l,init:this.value.indexOf(".")?true:false});if(this.value==="")return;t.val(t.val())})}else{return
this.each(function(){var
t=e(this),n=+t.text().replace(f,"").replace(l,".");t.number(!isFinite(n)?0:+n,s,o,u)})}}return
this.text(e.number.apply(window,arguments))};var
i=null,s=null;if(e.isPlainObject(e.valHooks.text)){if(e.isFunction(e.valHooks.text.get))i=e.valHooks.text.get;if(e.isFunction(e.valHooks.text.set))s=e.valHooks.text.set}else{e.valHooks.text={}}e.valHooks.text.get=function(t){var
n=e(t),r,s,o=n.data("numFormat");if(!o){if(e.isFunction(i)){return
i(t)}else{return
undefined}}else{if(t.value==="")return"";r=+t.value.replace(o.regex_dec_num,"").replace(o.regex_dec,".");return(t.value.indexOf("-")===0?"-":"")+(isFinite(r)?r:0)}};e.valHooks.text.set=function(t,n){var
r=e(t),i=r.data("numFormat");if(!i){if(e.isFunction(s)){return
s(t,n)}else{return undefined}}else{var
o=e.number(n,i.decimals,i.dec_point,i.thousands_sep);return
t.value=o}};e.number=function(e,t,n,r){r=typeof r==="undefined"?",":r;n=typeof
n==="undefined"?".":n;t=!isFinite(+t)?0:Math.abs(t);var
i="\\u"+("0000"+n.charCodeAt(0).toString(16)).slice(-4);var
s="\\u"+("0000"+r.charCodeAt(0).toString(16)).slice(-4);e=(e+"").replace(".",n).replace(new
RegExp(s,"g"),"").replace(new RegExp(i,"g"),".").replace(new
RegExp("[^0-9+-Ee.]","g"),"");var o=!isFinite(+e)?0:+e,u="",a=function(e,t){var
n=Math.pow(10,t);return""+Math.round(e*n)/n};u=(t?a(o,t):""+Math.round(o)).split(".");if(u[0].length>3){u[0]=u[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,r)}if((u[1]||"").length<t){u[1]=u[1]||"";u[1]+=(new
Array(t-u[1].length+1)).join("0")}return u.join(n)}})(jQuery)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Fmsystem-commits] [12637],
Nelson Guerra <=