PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/common/js/jquery.extend.js

http://lazycms.googlecode.com/
JavaScript | 163 lines | 100 code | 10 blank | 53 comment | 30 complexity | 48736e1b8ac5e038c4a67d49635b96f4 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. /**
  2. * +---------------------------------------------------------------------------+
  3. * | LL LLLL LL L LLLL LLLL |
  4. * | LL LL L LLL LL LL L LL LL |
  5. * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
  6. * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
  7. * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
  8. * | LL LL LL LL LLLL LL L LL LL LLLL LL |
  9. * | LL LL LL LL LL LL L L LL L LL LLLL LL |
  10. * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
  11. * | LL |
  12. * | LL |
  13. * +---------------------------------------------------------------------------+
  14. * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
  15. * +---------------------------------------------------------------------------+
  16. * | LazyCMS is free software. See LICENSE for copyright notices and details. |
  17. * +---------------------------------------------------------------------------+
  18. */
  19. /**
  20. * jQuery ??
  21. *
  22. * @author Lukin <my@lukin.cn>
  23. * @version $Id: jquery.extend.js 721 2011-01-17 11:01:20Z mylukin $
  24. */
  25. jQuery && (function ($) {
  26. /**
  27. * ????????
  28. *
  29. * @param val
  30. */
  31. $.fn.insertVal = function(val) {
  32. return this.each(function(){
  33. // IE support
  34. if (document.selection && document.selection.createRange){
  35. this.focus();
  36. var sel = document.selection.createRange();
  37. if (sel.text) {
  38. sel.text += val;
  39. } else {
  40. this.value += val;
  41. }
  42. }
  43. // MOZILLA/NETSCAPE support
  44. else if (this.selectionStart || this.selectionStart == '0') {
  45. var start = this.selectionStart, end = this.selectionEnd, stp = this.scrollTop;
  46. this.value = this.value.substring(0, start) + val + this.value.substring(end, this.value.length);
  47. this.focus();
  48. this.selectionStart = start + val.length;
  49. this.selectionEnd = start + val.length;
  50. this.scrollTop = stp;
  51. }
  52. // Other
  53. else {
  54. this.value += val;
  55. this.focus();
  56. }
  57. });
  58. };
  59. /**
  60. * ??????
  61. *
  62. * @param user
  63. * @param pass1
  64. * @param pass2
  65. */
  66. $.fn.check_pass_strength = function(user,pass1,pass2) {
  67. this.removeClass('short bad good strong');
  68. if ( ! pass1 ) {
  69. return this.html( _('Strength indicator') );
  70. }
  71. // Password strength meter
  72. var password_strength = function(username, password1, password2) {
  73. var short_pass = 1, bad_pass = 2, good_pass = 3, strong_pass = 4, mismatch = 5, symbol_size = 0, natLog, score;
  74. username = username || '';
  75. // password 1 != password 2
  76. if ( (password1 != password2) && password2.length > 0)
  77. return mismatch
  78. //password < 4
  79. if ( password1.length < 4 )
  80. return short_pass
  81. //password1 == username
  82. if ( password1.toLowerCase() == username.toLowerCase() )
  83. return bad_pass;
  84. if ( password1.match(/[0-9]/) )
  85. symbol_size +=10;
  86. if ( password1.match(/[a-z]/) )
  87. symbol_size +=26;
  88. if ( password1.match(/[A-Z]/) )
  89. symbol_size +=26;
  90. if ( password1.match(/[^a-zA-Z0-9]/) )
  91. symbol_size +=31;
  92. natLog = Math.log( Math.pow(symbol_size, password1.length) );
  93. score = natLog / Math.LN2;
  94. if (score < 40 )
  95. return bad_pass
  96. if (score < 56 )
  97. return good_pass
  98. return strong_pass;
  99. };
  100. var strength = password_strength(user, pass1, pass2);
  101. switch ( strength ) {
  102. case 2:
  103. this.addClass('bad').html( _('Weak') );
  104. break;
  105. case 3:
  106. this.addClass('good').html( _('Medium') );
  107. break;
  108. case 4:
  109. this.addClass('strong').html( _('Strong') );
  110. break;
  111. case 5:
  112. this.addClass('short').html( _('Mismatch') );
  113. break;
  114. default:
  115. this.addClass('short').html( _('Very weak') );
  116. }
  117. return this;
  118. };
  119. /*
  120. * JSON - JSON for jQuery
  121. *
  122. * FILE:jquery.json.js
  123. *
  124. * Example:
  125. *
  126. * $.toJSON(Object);
  127. * $.parseJSON(String);
  128. */
  129. $.toJSON = function(o){
  130. var i, v, s = $.toJSON, t;
  131. if (o == null) return 'null';
  132. t = typeof o;
  133. if (t == 'string') {
  134. v = '\bb\tt\nn\ff\rr\""\'\'\\\\';
  135. return '"' + o.replace(/([\u0080-\uFFFF\x00-\x1f\"])/g, function(a, b) {
  136. i = v.indexOf(b);
  137. if (i + 1) return '\\' + v.charAt(i + 1);
  138. a = b.charCodeAt().toString(16);
  139. return '\\u' + '0000'.substring(a.length) + a;
  140. }) + '"';
  141. }
  142. if (t == 'object') {
  143. if (o instanceof Array) {
  144. for (i=0, v = '['; i<o.length; i++) v += (i > 0 ? ',' : '') + s(o[i]);
  145. return v + ']';
  146. }
  147. v = '{';
  148. for (i in o) v += typeof o[i] != 'function' ? (v.length > 1 ? ',"' : '"') + i + '":' + s(o[i]) : '';
  149. return v + '}';
  150. }
  151. return '' + o;
  152. };
  153. })(jQuery);