axutils.js.orig 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. *
  3. *
  4. *
  5. *
  6. */
  7. (function() {
  8. // define the root namespace object
  9. if(!window.$axure) window.$axure = {};
  10. $axure.utils = {};
  11. // ------------------------------------------------------------------------
  12. // Makes an object bindable
  13. // ------------------------------------------------------------------------
  14. $axure.utils.makeBindable = function(obj, events) {
  15. if(obj.registeredBindings != null) return;
  16. // copy the events
  17. obj.bindableEvents = events.slice();
  18. obj.registeredBindings = {};
  19. obj.bind = function(eventName, fn) {
  20. var binding = {};
  21. binding.eventName = eventName;
  22. binding.action = fn;
  23. var bindingList = this.registeredBindings[eventName];
  24. if(bindingList == null) {
  25. bindingList = [];
  26. this.registeredBindings[eventName] = bindingList;
  27. }
  28. bindingList[bindingList.length] = binding;
  29. };
  30. obj.unbind = function(eventName) {
  31. if(eventName.indexOf('.') >= 0) {
  32. this.registeredBindings[eventName] = null;
  33. } else {
  34. var event = eventName.split('.')[0];
  35. for(var bindingKey in this.registeredBindings) {
  36. if(bindingKey.split('.')[0] == event) {
  37. this.registeredBindings[bindingKey] = null;
  38. }
  39. }
  40. }
  41. };
  42. obj.triggerEvent = function(eventName, arg) {
  43. for(var bindingKey in this.registeredBindings) {
  44. if(bindingKey.split('.')[0] == eventName) {
  45. var bindings = this.registeredBindings[bindingKey];
  46. for(var i = 0; i < bindings.length; i++) {
  47. if(arg == null) {
  48. bindings[i].action();
  49. } else {
  50. bindings[i].action(arg);
  51. }
  52. }
  53. }
  54. }
  55. };
  56. };
  57. $axure.utils.loadCSS = function(url) {
  58. $('head').append('<link text="text/css" href="' + url + '" rel="Stylesheet" />');
  59. };
  60. $axure.utils.loadJS = function(url) {
  61. $('head').append('<script text="text/javascript" language="JavaScript" src="' + url + '"></script>');
  62. };
  63. $axure.utils.curry = function(fn) {
  64. var curriedArgs = Array.prototype.slice.call(arguments, [1]);
  65. return function() {
  66. fn.apply(this, curriedArgs.concat(Array.prototype.slice.call(arguments)));
  67. };
  68. };
  69. $axure.utils.succeeded = function(result) {
  70. return result && result.success;
  71. };
  72. $axure.utils.createUniqueTag = function() {
  73. return Math.random().toString().substring(2) +
  74. Math.random().toString().substring(2) +
  75. Math.random().toString().substring(2) +
  76. Math.random().toString().substring(2);
  77. };
  78. $axure.utils.formatDate = function(date) {
  79. var months = [
  80. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  81. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  82. var hours = date.getHours();
  83. var amPm = (hours > 11 ? 'PM' : 'AM');
  84. hours = hours % 12;
  85. if(hours == '0') hours = '12';
  86. var minutes = date.getMinutes() + '';
  87. if(minutes.length == 1) {
  88. minutes = '0' + minutes;
  89. }
  90. return [
  91. months[date.getMonth()], ' ', date.getDate(), ' ', date.getFullYear(), ' ',
  92. hours, ':', minutes, ' ', amPm].join('');
  93. };
  94. $axure.utils.quickObject = function() {
  95. var returnVal = {};
  96. for(var i = 0; i < arguments.length; i += 2) {
  97. returnVal[arguments[i]] = arguments[i + 1];
  98. }
  99. return returnVal;
  100. };
  101. var matrixBase = {
  102. mul: function(val) {
  103. if(val.x !== undefined) {
  104. return $axure.utils.Vector2D(
  105. this.m11 * val.x + this.m12 * val.y + this.tx,
  106. this.m21 * val.x + this.m22 * val.y + this.ty);
  107. } else if(val.m11) {
  108. return $axure.utils.Matrix2D(
  109. this.m11 * val.m11 + this.m12 * val.m21,
  110. this.m11 * val.m12 + this.m12 * val.m22,
  111. this.m21 * val.m11 + this.m22 * val.m21,
  112. this.m21 * val.m12 + this.m22 * val.m22,
  113. val.tx + this.tx * val.m11 + this.ty * val.m21,
  114. val.ty + this.tx * val.m12 + this.ty * val.m22
  115. );
  116. } else if(Number(val)) {
  117. var num = Number(val);
  118. return $axure.utils.Matrix2D(this.m11 * num, this.m12 * num,
  119. this.m21 * num, this.m22 * num,
  120. this.tx * num, this.ty * num);
  121. } else return undefined;
  122. },
  123. rotate: function(angle) {
  124. var angleRad = angle * Math.PI / 180;
  125. var c = Math.cos(angleRad);
  126. var s = Math.sin(angleRad);
  127. return this.mul($axure.utils.Matrix2D(c, -s, s, c));
  128. },
  129. translate: function(tx, ty) {
  130. return this.mul($axure.utils.Matrix2D(1, 0, 0, 1, tx, ty));
  131. }
  132. };
  133. $axure.utils.Matrix2D = function(m11, m12, m21, m22, tx, ty) {
  134. return $.extend({
  135. m11: m11 || 0,
  136. m12: m12 || 0,
  137. m21: m21 || 0,
  138. m22: m22 || 0,
  139. tx: tx || 0,
  140. ty: ty || 0
  141. }, matrixBase);
  142. };
  143. $axure.utils.Vector2D = function(x, y) {
  144. return { x: x || 0, y: y || 0 };
  145. };
  146. $axure.utils.Matrix2D.identity = function() {
  147. return $axure.utils.Matrix2D(1, 0, 0, 1, 0, 0);
  148. };
  149. $axure.utils.fixPng = function(png) {
  150. if(!(/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32")) return;
  151. var src = png.src;
  152. if(!png.style.width) { png.style.width = $(png).width(); }
  153. if(!png.style.height) { png.style.height = $(png).height(); }
  154. png.onload = function() { };
  155. png.src = $axure.utils.getTransparentGifPath();
  156. png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
  157. };
  158. })();
  159. // TODO: [mas] simplify this
  160. if(window.$axure && window.$axure.internal) {
  161. $axure.internal(function($ax) { $ax.utils = $axure.utils; });
  162. }
  163. // Its too much of a pain to escape everything and use regular expresions, just replace manually
  164. (function () {
  165. var original = String.prototype.replace;
  166. // TODO: maybe use flags or object instead to pass options in
  167. String.prototype.replace = function (search, newVal, replaceFirst, ignoreCase) {
  168. // Use original is some cases
  169. if (search instanceof RegExp) return original.apply(this, arguments);
  170. var searchCompare = ignoreCase ? this.toLowerCase() : this;
  171. if (ignoreCase) search = search.toLowerCase();
  172. var searchLength = search.length;
  173. var thisLength = this.length;
  174. var index = 0;
  175. var retVal = '';
  176. while (index != -1) {
  177. var nextIndex = searchCompare.indexOf(search, index);
  178. if (nextIndex != -1) {
  179. retVal += this.substring(index, nextIndex) + newVal;
  180. index = nextIndex + searchLength;
  181. if (index >= thisLength) index = -1;
  182. } else {
  183. retVal += this.substring(index);
  184. index = -1;
  185. }
  186. if (replaceFirst) break;
  187. }
  188. return retVal;
  189. };
  190. if (!Array.prototype.indexOf) {
  191. Array.prototype.indexOf = function (elt /*, from*/) {
  192. var len = this.length >>> 0;
  193. var from = Number(arguments[1]) || 0;
  194. from = (from < 0)
  195. ? Math.ceil(from)
  196. : Math.floor(from);
  197. if (from < 0)
  198. from += len;
  199. for (; from < len; from++) {
  200. if (from in this &&
  201. this[from] === elt)
  202. return from;
  203. }
  204. return -1;
  205. };
  206. }
  207. })();