wrtpressurize.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. *
  3. * Pressurize is badly named, yes, but a really useful feature:
  4. * We used this implementation to overcome issues with
  5. * mouse events when having touch UI
  6. *
  7. * Take a look and feel free to use!
  8. *
  9. */
  10. if (typeof util == "undefined") {
  11. var util = {};
  12. }
  13. (function(namespace) {
  14. // matches " pressed" but not " pressedstyle" or "depressed"
  15. var pressedRegexp = /\ pressed\b/;
  16. function clearTimeouts(elem) {
  17. if (elem.pressurize_timeout) {
  18. clearTimeout(elem.pressurize_timeout);
  19. elem.pressurize_timeout = undefined;
  20. }
  21. }
  22. function setPressed(elem) {
  23. removePressed(elem);
  24. // add class
  25. elem.className += " pressed";
  26. // long press does not send click event,
  27. // overcome this by having a timeout for the click
  28. elem.pressurize_timeout = setTimeout(function() { removePressed(elem); },2000);
  29. }
  30. function removePressed(elem) {
  31. // remove class
  32. elem.className = elem.className.replace(pressedRegexp,'');
  33. clearTimeouts(elem);
  34. }
  35. function matchElem(input) {
  36. if(typeof input == "object") {
  37. return input;
  38. } else {
  39. return document.getElementById(elemId);
  40. }
  41. }
  42. function pressurizeAll(className) {
  43. var elems = getElementsByClass(className);
  44. for(var i=0;i<elems.length;i++) {
  45. pressurize(elems[i]);
  46. }
  47. }
  48. function depressurizeAll(className) {
  49. var elems = getElementsByClass(className);
  50. for(var i=0;i<elems.length;i++) {
  51. depressurize(elems[i]);
  52. }
  53. }
  54. function getElementsByClass(searchClass,node,tag) {
  55. // this caused problems, at least with Prototype.js in a widget
  56. //if(document.getElementsByClassName) {
  57. // document.getElementById("test").innerHTML += "<div>using getElementsByClassName<div>";
  58. // return document.getElementsByClassName(searchClass);
  59. //} else
  60. if(typeof $$ == "function") {
  61. return $$("."+searchClass);
  62. } else {
  63. var classElements = new Array();
  64. if(node == null) node = document;
  65. if(tag == null) tag = '*';
  66. var els = node.getElementsByTagName(tag);
  67. var elsLen = els.length;
  68. var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  69. for (i = 0, j = 0; i < elsLen; i++) {
  70. if ( pattern.test(els[i].className) ) {
  71. classElements[j] = els[i];
  72. j++;
  73. }
  74. }
  75. return classElements;
  76. }
  77. }
  78. function pressurize(rawElem) {
  79. var elem = matchElem(rawElem);
  80. // mouse down on device (note, do not modify dom inside the event handler,
  81. // otherwise click event does not fire!)
  82. elem.addEventListener("mousemove", function() { setPressed(elem); }, false);
  83. // mouse up on the device
  84. elem.addEventListener("click", function() { removePressed(elem); }, false);
  85. // mouse out does not work on device, but makes behaviour
  86. // on desktop more sane
  87. elem.addEventListener("mouseout", function() { removePressed(elem); }, false);
  88. // if mouseup outside the element, scroll event sometimes sent
  89. elem.pressurize_windowListener = function() { removePressed(elem); };
  90. window.addEventListener('scroll', elem.pressurize_windowListener, false);
  91. }
  92. function depressurize(rawElem) {
  93. var elem = matchElem(rawElem);
  94. // remove the (global) scroll event handler if the element is deleted
  95. window.removeEventListener('scroll', elem.pressurize_windowListener, false);
  96. }
  97. namespace.pressurize = pressurize;
  98. namespace.depressurize = depressurize;
  99. namespace.pressurizeAll = pressurizeAll;
  100. namespace.depressurizeAll = depressurizeAll;
  101. })(util);