cursor.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2005 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Functions to create special cursor styles, like "draggable"
  16. * (open hand) or "dragging" (closed hand).
  17. *
  18. * @author dgajda@google.com (Damian Gajda) Ported to closure.
  19. */
  20. goog.provide('goog.style.cursor');
  21. goog.require('goog.userAgent');
  22. /**
  23. * The file name for the open-hand (draggable) cursor.
  24. * @type {string}
  25. */
  26. goog.style.cursor.OPENHAND_FILE = 'openhand.cur';
  27. /**
  28. * The file name for the close-hand (dragging) cursor.
  29. * @type {string}
  30. */
  31. goog.style.cursor.CLOSEDHAND_FILE = 'closedhand.cur';
  32. /**
  33. * Create the style for the draggable cursor based on browser and OS.
  34. * The value can be extended to be '!important' if needed.
  35. *
  36. * @param {string} absoluteDotCurFilePath The absolute base path of
  37. * 'openhand.cur' file to be used if the browser supports it.
  38. * @param {boolean=} opt_obsolete Just for compiler backward compatibility.
  39. * @return {string} The "draggable" mouse cursor style value.
  40. */
  41. goog.style.cursor.getDraggableCursorStyle = function(
  42. absoluteDotCurFilePath, opt_obsolete) {
  43. return goog.style.cursor.getCursorStyle_(
  44. '-moz-grab', absoluteDotCurFilePath + goog.style.cursor.OPENHAND_FILE,
  45. 'default');
  46. };
  47. /**
  48. * Create the style for the dragging cursor based on browser and OS.
  49. * The value can be extended to be '!important' if needed.
  50. *
  51. * @param {string} absoluteDotCurFilePath The absolute base path of
  52. * 'closedhand.cur' file to be used if the browser supports it.
  53. * @param {boolean=} opt_obsolete Just for compiler backward compatibility.
  54. * @return {string} The "dragging" mouse cursor style value.
  55. */
  56. goog.style.cursor.getDraggingCursorStyle = function(
  57. absoluteDotCurFilePath, opt_obsolete) {
  58. return goog.style.cursor.getCursorStyle_(
  59. '-moz-grabbing',
  60. absoluteDotCurFilePath + goog.style.cursor.CLOSEDHAND_FILE, 'move');
  61. };
  62. /**
  63. * Create the style for the cursor based on browser and OS.
  64. *
  65. * @param {string} geckoNonWinBuiltInStyleValue The Gecko on non-Windows OS,
  66. * built in cursor style.
  67. * @param {string} absoluteDotCurFilePath The .cur file absolute file to be
  68. * used if the browser supports it.
  69. * @param {string} defaultStyle The default fallback cursor style.
  70. * @return {string} The computed mouse cursor style value.
  71. * @private
  72. */
  73. goog.style.cursor.getCursorStyle_ = function(
  74. geckoNonWinBuiltInStyleValue, absoluteDotCurFilePath, defaultStyle) {
  75. // Use built in cursors for Gecko on non Windows OS.
  76. // We prefer our custom cursor, but Firefox Mac and Firefox Linux
  77. // cannot do custom cursors. They do have a built-in hand, so use it:
  78. if (goog.userAgent.GECKO && !goog.userAgent.WINDOWS) {
  79. return geckoNonWinBuiltInStyleValue;
  80. }
  81. // Use the custom cursor file.
  82. var cursorStyleValue = 'url("' + absoluteDotCurFilePath + '")';
  83. // Change hot-spot for Safari.
  84. if (goog.userAgent.WEBKIT) {
  85. // Safari seems to ignore the hotspot specified in the .cur file (it uses
  86. // 0,0 instead). This causes the cursor to jump as it transitions between
  87. // openhand and pointer which is especially annoying when trying to hover
  88. // over the route for draggable routes. We specify the hotspot here as 7,5
  89. // in the css - unfortunately ie6 can't understand this and falls back to
  90. // the builtin cursors so we just do this for safari (but ie DOES correctly
  91. // use the hotspot specified in the file so this is ok). The appropriate
  92. // coordinates were determined by looking at a hex dump and the format
  93. // description from wikipedia.
  94. cursorStyleValue += ' 7 5';
  95. }
  96. // Add default cursor fallback.
  97. cursorStyleValue += ', ' + defaultStyle;
  98. return cursorStyleValue;
  99. };