Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

util.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Utility functions.
  3. */
  4. var Util = (function (my) {
  5. /**
  6. * Returns the text width for the given element.
  7. *
  8. * @param el the element
  9. */
  10. my.getTextWidth = function(el) {
  11. return (el.clientWidth + 1);
  12. };
  13. /**
  14. * Returns the text height for the given element.
  15. *
  16. * @param el the element
  17. */
  18. my.getTextHeight = function(el) {
  19. return (el.clientHeight + 1);
  20. };
  21. /**
  22. * Casts the given number to integer.
  23. *
  24. * @param number the number to cast
  25. */
  26. my.toInteger = function(number) {
  27. return Math.round(Number(number));
  28. };
  29. /**
  30. * Plays the sound given by id.
  31. *
  32. * @param id the identifier of the audio element.
  33. */
  34. my.playSoundNotification = function(id) {
  35. document.getElementById(id).play();
  36. };
  37. /**
  38. * Escapes the given text.
  39. */
  40. my.escapeHtml = function(unsafeText) {
  41. return $('<div/>').text(unsafeText).html();
  42. };
  43. /**
  44. * Indicates if the given string is an alphanumeric string.
  45. * Note that some special characters are also allowed (-, _ , /) for the
  46. * purpose of checking URIs. (FIXME: This should maybe moved to another not
  47. * so generic method in the future.)
  48. */
  49. my.isAlphanumeric = function(unsafeText) {
  50. var regex = /^[a-z0-9-_\/]+$/i;
  51. return regex.test(unsafeText);
  52. };
  53. return my;
  54. }(Util || {}));