PageRenderTime 35ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/admin/media/js/core.js

https://code.google.com/p/mango-py/
JavaScript | 221 lines | 161 code | 21 blank | 39 comment | 42 complexity | c549fd6812ac00be6733b70cb84ce3a0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Core javascript helper functions
  2. // basic browser identification & version
  3. var isOpera = (navigator.userAgent.indexOf("Opera")>=0) && parseFloat(navigator.appVersion);
  4. var isIE = ((document.all) && (!isOpera)) && parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]);
  5. // Cross-browser event handlers.
  6. function addEvent(obj, evType, fn) {
  7. if (obj.addEventListener) {
  8. obj.addEventListener(evType, fn, false);
  9. return true;
  10. } else if (obj.attachEvent) {
  11. var r = obj.attachEvent("on" + evType, fn);
  12. return r;
  13. } else {
  14. return false;
  15. }
  16. }
  17. function removeEvent(obj, evType, fn) {
  18. if (obj.removeEventListener) {
  19. obj.removeEventListener(evType, fn, false);
  20. return true;
  21. } else if (obj.detachEvent) {
  22. obj.detachEvent("on" + evType, fn);
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. // quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]);
  29. function quickElement() {
  30. var obj = document.createElement(arguments[0]);
  31. if (arguments[2] != '' && arguments[2] != null) {
  32. var textNode = document.createTextNode(arguments[2]);
  33. obj.appendChild(textNode);
  34. }
  35. var len = arguments.length;
  36. for (var i = 3; i < len; i += 2) {
  37. obj.setAttribute(arguments[i], arguments[i+1]);
  38. }
  39. arguments[1].appendChild(obj);
  40. return obj;
  41. }
  42. // ----------------------------------------------------------------------------
  43. // Cross-browser xmlhttp object
  44. // from http://jibbering.com/2002/4/httprequest.html
  45. // ----------------------------------------------------------------------------
  46. var xmlhttp;
  47. /*@cc_on @*/
  48. /*@if (@_jscript_version >= 5)
  49. try {
  50. xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  51. } catch (e) {
  52. try {
  53. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  54. } catch (E) {
  55. xmlhttp = false;
  56. }
  57. }
  58. @else
  59. xmlhttp = false;
  60. @end @*/
  61. if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  62. xmlhttp = new XMLHttpRequest();
  63. }
  64. // ----------------------------------------------------------------------------
  65. // Find-position functions by PPK
  66. // See http://www.quirksmode.org/js/findpos.html
  67. // ----------------------------------------------------------------------------
  68. function findPosX(obj) {
  69. var curleft = 0;
  70. if (obj.offsetParent) {
  71. while (obj.offsetParent) {
  72. curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft);
  73. obj = obj.offsetParent;
  74. }
  75. // IE offsetParent does not include the top-level
  76. if (isIE && obj.parentElement){
  77. curleft += obj.offsetLeft - obj.scrollLeft;
  78. }
  79. } else if (obj.x) {
  80. curleft += obj.x;
  81. }
  82. return curleft;
  83. }
  84. function findPosY(obj) {
  85. var curtop = 0;
  86. if (obj.offsetParent) {
  87. while (obj.offsetParent) {
  88. curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
  89. obj = obj.offsetParent;
  90. }
  91. // IE offsetParent does not include the top-level
  92. if (isIE && obj.parentElement){
  93. curtop += obj.offsetTop - obj.scrollTop;
  94. }
  95. } else if (obj.y) {
  96. curtop += obj.y;
  97. }
  98. return curtop;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Date object extensions
  102. // ----------------------------------------------------------------------------
  103. Date.prototype.getCorrectYear = function() {
  104. // Date.getYear() is unreliable --
  105. // see http://www.quirksmode.org/js/introdate.html#year
  106. var y = this.getYear() % 100;
  107. return (y < 38) ? y + 2000 : y + 1900;
  108. }
  109. Date.prototype.getTwelveHours = function() {
  110. hours = this.getHours();
  111. if (hours == 0) {
  112. return 12;
  113. }
  114. else {
  115. return hours <= 12 ? hours : hours-12
  116. }
  117. }
  118. Date.prototype.getTwoDigitMonth = function() {
  119. return (this.getMonth() < 9) ? '0' + (this.getMonth()+1) : (this.getMonth()+1);
  120. }
  121. Date.prototype.getTwoDigitDate = function() {
  122. return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
  123. }
  124. Date.prototype.getTwoDigitTwelveHour = function() {
  125. return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
  126. }
  127. Date.prototype.getTwoDigitHour = function() {
  128. return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
  129. }
  130. Date.prototype.getTwoDigitMinute = function() {
  131. return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
  132. }
  133. Date.prototype.getTwoDigitSecond = function() {
  134. return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
  135. }
  136. Date.prototype.getISODate = function() {
  137. return this.getCorrectYear() + '-' + this.getTwoDigitMonth() + '-' + this.getTwoDigitDate();
  138. }
  139. Date.prototype.getHourMinute = function() {
  140. return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
  141. }
  142. Date.prototype.getHourMinuteSecond = function() {
  143. return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
  144. }
  145. Date.prototype.strftime = function(format) {
  146. var fields = {
  147. c: this.toString(),
  148. d: this.getTwoDigitDate(),
  149. H: this.getTwoDigitHour(),
  150. I: this.getTwoDigitTwelveHour(),
  151. m: this.getTwoDigitMonth(),
  152. M: this.getTwoDigitMinute(),
  153. p: (this.getHours() >= 12) ? 'PM' : 'AM',
  154. S: this.getTwoDigitSecond(),
  155. w: '0' + this.getDay(),
  156. x: this.toLocaleDateString(),
  157. X: this.toLocaleTimeString(),
  158. y: ('' + this.getFullYear()).substr(2, 4),
  159. Y: '' + this.getFullYear(),
  160. '%' : '%'
  161. };
  162. var result = '', i = 0;
  163. while (i < format.length) {
  164. if (format.charAt(i) === '%') {
  165. result = result + fields[format.charAt(i + 1)];
  166. ++i;
  167. }
  168. else {
  169. result = result + format.charAt(i);
  170. }
  171. ++i;
  172. }
  173. return result;
  174. }
  175. // ----------------------------------------------------------------------------
  176. // String object extensions
  177. // ----------------------------------------------------------------------------
  178. String.prototype.pad_left = function(pad_length, pad_string) {
  179. var new_string = this;
  180. for (var i = 0; new_string.length < pad_length; i++) {
  181. new_string = pad_string + new_string;
  182. }
  183. return new_string;
  184. }
  185. // ----------------------------------------------------------------------------
  186. // Get the computed style for and element
  187. // ----------------------------------------------------------------------------
  188. function getStyle(oElm, strCssRule){
  189. var strValue = "";
  190. if(document.defaultView && document.defaultView.getComputedStyle){
  191. strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
  192. }
  193. else if(oElm.currentStyle){
  194. strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
  195. return p1.toUpperCase();
  196. });
  197. strValue = oElm.currentStyle[strCssRule];
  198. }
  199. return strValue;
  200. }