PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/static/scripts/ie7-recalc.js

https://bitbucket.org/cistrome/cistrome-harvard/
JavaScript | 166 lines | 109 code | 23 blank | 34 comment | 18 complexity | ac0bc06ec87e877e09a5b687ba05257e MD5 | raw file
  1. // =========================================================================
  2. // ie7-recalc.js
  3. // =========================================================================
  4. (function() {
  5. /* ---------------------------------------------------------------------
  6. This allows refreshing of IE7 style rules. If you modify the DOM
  7. you can update IE7 by calling document.recalc().
  8. This should be the LAST module included.
  9. --------------------------------------------------------------------- */
  10. if (!IE7.loaded) return;
  11. // remove all IE7 classes from an element
  12. CLASSES = /\sie7_class\d+/g;
  13. IE7.CSS.extend({
  14. // store for elements that have style properties calculated
  15. elements: {},
  16. handlers: [],
  17. // clear IE7 classes and styles
  18. reset: function() {
  19. this.removeEventHandlers();
  20. // reset IE7 classes here
  21. var elements = this.elements;
  22. for (var i in elements) elements[i].runtimeStyle.cssText = "";
  23. this.elements = {};
  24. // reset runtimeStyle here
  25. var elements = IE7.Rule.elements;
  26. for (var i in elements) {
  27. with (elements[i]) className = className.replace(CLASSES, "");
  28. }
  29. IE7.Rule.elements = {};
  30. },
  31. reload: function() {
  32. this.rules = [];
  33. this.getInlineStyles();
  34. this.screen.load();
  35. if (this.print) this.print.load();
  36. this.refresh();
  37. this.trash();
  38. },
  39. addRecalc: function(propertyName, test, handler, replacement) {
  40. // call the ancestor method to add a wrapped recalc method
  41. this.base(propertyName, test, function(element) {
  42. // execute the original recalc method
  43. handler(element);
  44. // store a reference to this element so we can clear its style later
  45. IE7.CSS.elements[element.uniqueID] = element;
  46. }, replacement);
  47. },
  48. recalc: function() {
  49. // clear IE7 styles and classes
  50. this.reset();
  51. // execute the ancestor method to perform recalculations
  52. this.base();
  53. },
  54. addEventHandler: function(element, type, handler) {
  55. element.attachEvent(type, handler);
  56. // store the handler so it can be detached later
  57. this.handlers.push(arguments);
  58. },
  59. removeEventHandlers: function() {
  60. var handler;
  61. while (handler = this.handlers.pop()) {
  62. handler[0].detachEvent(handler[1], handler[2]);
  63. }
  64. },
  65. getInlineStyles: function() {
  66. // load inline styles
  67. var styleSheets = document.getElementsByTagName("style"), styleSheet;
  68. for (var i = styleSheets.length - 1; (styleSheet = styleSheets[i]); i--) {
  69. if (!styleSheet.disabled && !styleSheet.ie7) {
  70. var cssText = styleSheet.cssText || styleSheet.innerHTML;
  71. this.styles.push(cssText);
  72. styleSheet.cssText = cssText;
  73. }
  74. }
  75. },
  76. trash: function() {
  77. // trash the old style sheets
  78. var styleSheets = document.styleSheets, styleSheet, i;
  79. for (i = 0; i < styleSheets.length; i++) {
  80. styleSheet = styleSheets[i];
  81. if (!styleSheet.ie7 && !styleSheet.cssText) {
  82. styleSheet.cssText = styleSheet.cssText;
  83. }
  84. }
  85. this.base();
  86. },
  87. getText: function(styleSheet) {
  88. return styleSheet.cssText || this.base(styleSheet);
  89. }
  90. });
  91. // remove event handlers (they eat memory)
  92. IE7.CSS.addEventHandler(window, "onunload", function() {
  93. IE7.CSS.removeEventHandlers();
  94. });
  95. // store all elements with an IE7 class assigned
  96. IE7.Rule.elements = {};
  97. IE7.Rule.prototype.extend({
  98. add: function(element) {
  99. // execute the ancestor "add" method
  100. this.base(element);
  101. // store a reference to this element so we can clear its classes later
  102. IE7.Rule.elements[element.uniqueID] = element;
  103. }
  104. });
  105. // store created pseudo elements
  106. if (IE7.PseudoElement) {
  107. IE7.PseudoElement.hash = {};
  108. IE7.PseudoElement.prototype.extend({
  109. create: function(target) {
  110. var key = this.selector + ":" + target.uniqueID;
  111. if (!IE7.PseudoElement.hash[key]) {
  112. IE7.PseudoElement.hash[key] = true;
  113. this.base(target);
  114. }
  115. }
  116. });
  117. }
  118. IE7.HTML.extend({
  119. elements: {},
  120. addRecalc: function(selector, handler) {
  121. // call the ancestor method to add a wrapped recalc method
  122. this.base(selector, function(element) {
  123. if (!this.elements[element.uniqueID]) {
  124. // execute the original recalc method
  125. handler(element);
  126. // store a reference to this element so that
  127. // it is not "fixed" again
  128. this.elements[element.uniqueID] = element;
  129. }
  130. });
  131. }
  132. });
  133. // allow refreshing of IE7 fixes
  134. document.recalc = function(reload) {
  135. if (IE7.CSS.screen) {
  136. if (reload) IE7.CSS.reload();
  137. IE7.recalc();
  138. }
  139. };
  140. })();