Guest User

Untitled

a guest
Apr 27th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. //
  2. // How to use:
  3. //
  4. // $('#price,#sum').cleave({ numeral: true, numeralThousandsGroupStyle: 'thousand', autoUnmask: true});
  5. //
  6. // or use pre-defined sets:
  7. //
  8. // $('#price').cleave('money');
  9. // $('#date_at').cleave('date');
  10. // $('#qty').cleave('integer');
  11. //
  12. (function($, window, undefined){ 'use strict'
  13.  
  14. $.fn.cleave = function(opts) {
  15.  
  16. var defaults = {autoUnmask: false}, options = {};
  17.  
  18. var defDate = {date: true, datePattern: ['d','m','Y'], delimiter: '-'},
  19. defTime = {numericOnly: true, delimiters: [':'], blocks: [2, 2]},
  20. defDateTime= {numericOnly: true, delimiters: ['-', '-', ' ', ':'], blocks: [2, 2, 4, 2, 2]},
  21. defMoney = {numeral: true, numeralThousandsGroupStyle: 'thousand', numeralPositiveOnly: true, autoUnmask: true},
  22. defInteger = {numeral: true, numeralThousandsGroupStyle: 'thousand', numeralPositiveOnly: true, autoUnmask: true, numeralDecimalScale: 0};
  23.  
  24. if( typeof opts === 'string') {
  25. switch( opts ) {
  26. case 'date': $.extend(options, defaults, defDate); break;
  27. case 'time': $.extend(options, defaults, defTime); break;
  28. case 'datetime': $.extend(options, defaults, defDateTime); break;
  29. case 'money': $.extend(options, defaults, defMoney); break;
  30. case 'integer': $.extend(options, defaults, defInteger); break;
  31. }
  32. } else if( $.isPlainObject( opts ) ) {
  33. $.extend(options, defaults, opts || {});
  34. }
  35.  
  36. return this.each(function(){
  37.  
  38. if( this.id == undefined || this.id == null || this.id == '' ) {
  39. throw 'jquery-cleave.js requires objectID to be initialized';
  40. }
  41.  
  42. var cleave = new Cleave('#'+this.id, options), $this = $(this);
  43.  
  44. $this.data('cleave-auto-unmask', options['autoUnmask']);;
  45. $this.data('cleave',cleave)
  46. });
  47. }
  48.  
  49. var origGetHook, origSetHook;
  50.  
  51. if ($.valHooks.input) {
  52.  
  53. origGetHook = $.valHooks.input.get;
  54. origSetHook = $.valHooks.input.set;
  55.  
  56. } else {
  57.  
  58. $.valHooks.input = {};
  59. }
  60.  
  61. $.valHooks.input.get = function (el) {
  62.  
  63. var $el = $(el), cleave = $el.data('cleave');
  64.  
  65. if( cleave ) {
  66.  
  67. return $el.data('cleave-auto-unmask') ? cleave.getRawValue() : el.value;
  68.  
  69. } else if( origGetHook ) {
  70.  
  71. return origGetHook(el);
  72.  
  73. } else {
  74.  
  75. return undefined;
  76. }
  77. }
  78.  
  79. $.valHooks.input.set = function (el,val) {
  80.  
  81. var $el = $(el), cleave = $el.data('cleave');
  82.  
  83. if( cleave ) {
  84.  
  85. cleave.setRawValue(val);
  86. return $el;
  87.  
  88. } else if( origSetHook ) {
  89.  
  90. return origSetHook(el);
  91.  
  92. } else {
  93. return undefined;
  94. }
  95. }
  96. })(jQuery, window);
Add Comment
Please, Sign In to add comment