utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. DrawingBoard.Utils = {};
  2. /*!
  3. * Tim (lite)
  4. * github.com/premasagar/tim
  5. *//*
  6. A tiny, secure JavaScript micro-templating script.
  7. */
  8. DrawingBoard.Utils.tpl = (function(){
  9. "use strict";
  10. var start = "{{",
  11. end = "}}",
  12. path = "[a-z0-9_][\\.a-z0-9_]*", // e.g. config.person.name
  13. pattern = new RegExp(start + "\\s*("+ path +")\\s*" + end, "gi"),
  14. undef;
  15. return function(template, data){
  16. // Merge data into the template string
  17. return template.replace(pattern, function(tag, token){
  18. var path = token.split("."),
  19. len = path.length,
  20. lookup = data,
  21. i = 0;
  22. for (; i < len; i++){
  23. lookup = lookup[path[i]];
  24. // Property not found
  25. if (lookup === undef){
  26. throw "tim: '" + path[i] + "' not found in " + tag;
  27. }
  28. // Return the required value
  29. if (i === len - 1){
  30. return lookup;
  31. }
  32. }
  33. });
  34. };
  35. }());
  36. /**
  37. * https://github.com/jeromeetienne/microevent.js
  38. * MicroEvent - to make any js object an event emitter (server or browser)
  39. *
  40. * - pure javascript - server compatible, browser compatible
  41. * - dont rely on the browser doms
  42. * - super simple - you get it immediatly, no mistery, no magic involved
  43. *
  44. * - create a MicroEventDebug with goodies to debug
  45. * - make it safer to use
  46. */
  47. DrawingBoard.Utils.MicroEvent = function(){};
  48. DrawingBoard.Utils.MicroEvent.prototype = {
  49. bind : function(event, fct){
  50. this._events = this._events || {};
  51. this._events[event] = this._events[event] || [];
  52. this._events[event].push(fct);
  53. },
  54. unbind : function(event, fct){
  55. this._events = this._events || {};
  56. if( event in this._events === false ) return;
  57. this._events[event].splice(this._events[event].indexOf(fct), 1);
  58. },
  59. trigger : function(event /* , args... */){
  60. this._events = this._events || {};
  61. if( event in this._events === false ) return;
  62. for(var i = 0; i < this._events[event].length; i++){
  63. this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1));
  64. }
  65. }
  66. };
  67. //I know.
  68. DrawingBoard.Utils._boxBorderSize = function($el, withPadding, withMargin, direction) {
  69. withPadding = !!withPadding || true;
  70. withMargin = !!withMargin || false;
  71. var width = 0,
  72. props;
  73. if (direction == "width") {
  74. props = ['border-left-width', 'border-right-width'];
  75. if (withPadding) props.push('padding-left', 'padding-right');
  76. if (withMargin) props.push('margin-left', 'margin-right');
  77. } else {
  78. props = ['border-top-width', 'border-bottom-width'];
  79. if (withPadding) props.push('padding-top', 'padding-bottom');
  80. if (withMargin) props.push('margin-top', 'margin-bottom');
  81. }
  82. for (var i = props.length - 1; i >= 0; i--)
  83. width += parseInt($el.css(props[i]).replace('px', ''), 10);
  84. return width;
  85. };
  86. DrawingBoard.Utils.boxBorderWidth = function($el, withPadding, withMargin) {
  87. return DrawingBoard.Utils._boxBorderSize($el, withPadding, withMargin, 'width');
  88. };
  89. DrawingBoard.Utils.boxBorderHeight = function($el, withPadding, withMargin) {
  90. return DrawingBoard.Utils._boxBorderSize($el, withPadding, withMargin, 'height');
  91. };
  92. DrawingBoard.Utils.isColor = function(string) {
  93. if (!string || !string.length) return false;
  94. return (/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i).test(string) || $.inArray(string.substring(0, 3), ['rgb', 'hsl']) !== -1;
  95. };
  96. /**
  97. * Packs an RGB color into a single integer.
  98. */
  99. DrawingBoard.Utils.RGBToInt = function(r, g, b) {
  100. var c = 0;
  101. c |= (r & 255) << 16;
  102. c |= (g & 255) << 8;
  103. c |= (b & 255);
  104. return c;
  105. };
  106. /**
  107. * Returns informations on the pixel located at (x,y).
  108. */
  109. DrawingBoard.Utils.pixelAt = function(image, x, y) {
  110. var i = (y * image.width + x) * 4;
  111. var c = DrawingBoard.Utils.RGBToInt(
  112. image.data[i],
  113. image.data[i + 1],
  114. image.data[i + 2]
  115. );
  116. return [
  117. i, // INDEX
  118. x, // X
  119. y, // Y
  120. c // COLOR
  121. ];
  122. };
  123. (function() {
  124. var lastTime = 0;
  125. var vendors = ['ms', 'moz', 'webkit', 'o'];
  126. for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  127. window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
  128. window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
  129. }
  130. }());