miniFramework.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * the dollar sign, classic like jquery, this selector add extra methods to HTMLElement without touching its prototype</br>
  24. * it is also chainable like jquery
  25. * @param {HTMLElement|String} x pass in a css selector in string or the whole HTMLElement
  26. * @function
  27. * @return {cc.$}
  28. */
  29. cc.$ = function (x) {
  30. /** @lends cc.$# */
  31. var parent = (this == cc) ? document : this;
  32. var el = (x instanceof HTMLElement) ? x : parent.querySelector(x);
  33. if (el) {
  34. /**
  35. * find and return the child wth css selector (same as jquery.find)
  36. * @lends cc.$#
  37. * @function
  38. * @param {HTMLElement|String} x pass in a css selector in string or the whole HTMLElement
  39. * @return {cc.$}
  40. */
  41. el.find = el.find || cc.$;
  42. /**
  43. * check if a DOMNode has a specific class
  44. * @lends cc.$#
  45. * @function
  46. * @param {String} cls
  47. * @return {Boolean}
  48. */
  49. el.hasClass = el.hasClass || function (cls) {
  50. return this.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
  51. };
  52. /**
  53. * add a class to a DOMNode, returns self to allow chaining
  54. * @lends cc.$#
  55. * @function
  56. * @param {String} cls
  57. * @return {cc.$}
  58. */
  59. el.addClass = el.addClass || function (cls) {
  60. if (!this.hasClass(cls)) {
  61. if (this.className) {
  62. this.className += " ";
  63. }
  64. this.className += cls;
  65. }
  66. return this;
  67. };
  68. /**
  69. * remove a specific class from a DOMNode, returns self to allow chaining
  70. * @lends cc.$#
  71. * @function
  72. * @param {String} cls
  73. * @return {cc.$}
  74. */
  75. el.removeClass = el.removeClass || function (cls) {
  76. if (this.hasClass(cls)) {
  77. this.className = this.className.replace(cls, '');
  78. }
  79. return this;
  80. };
  81. /**
  82. * detach it self from parent
  83. * @lends cc.$#
  84. * @function
  85. */
  86. el.remove = el.remove || function () {
  87. if (this.parentNode)
  88. this.parentNode.removeChild(this);
  89. return this;
  90. };
  91. /**
  92. * add to another element as a child
  93. * @lends cc.$#
  94. * @function
  95. * @param {HTMLElement|cc.$} x
  96. * @return {cc.$}
  97. */
  98. el.appendTo = el.appendTo || function (x) {
  99. x.appendChild(this);
  100. return this;
  101. };
  102. /**
  103. * add to another element as a child and place on the top of the children list
  104. * @lends cc.$#
  105. * @function
  106. * @param {HTMLElement|cc.$} x
  107. * @return {cc.$}
  108. */
  109. el.prependTo = el.prependTo || function (x) {
  110. ( x.childNodes[0]) ? x.insertBefore(this, x.childNodes[0]) : x.appendChild(this);
  111. return this;
  112. };
  113. /**
  114. * helper function for updating the css transform
  115. * @lends cc.$#
  116. * @function
  117. * @return {cc.$}
  118. */
  119. el.transforms = el.transforms || function () {
  120. this.style[cc.$.trans] = cc.$.translate(this.position) + cc.$.rotate(this.rotation) + cc.$.scale(this.scale) + cc.$.skew(this.skew);
  121. return this;
  122. };
  123. el.position = el.position || {x: 0, y: 0};
  124. el.rotation = el.rotation || 0;
  125. el.scale = el.scale || {x: 1, y: 1};
  126. el.skew = el.skew || {x: 0, y: 0};
  127. /**
  128. * move the element
  129. * @memberOf cc.$#
  130. * @name translates
  131. * @function
  132. * @param {Number} x in pixel
  133. * @param {Number} y in pixel
  134. * @return {cc.$}
  135. */
  136. el.translates = function (x, y) {
  137. this.position.x = x;
  138. this.position.y = y;
  139. this.transforms();
  140. return this
  141. };
  142. /**
  143. * rotate the element
  144. * @memberOf cc.$#
  145. * @name rotate
  146. * @function
  147. * @param {Number} x in degrees
  148. * @return {cc.$}
  149. */
  150. el.rotate = function (x) {
  151. this.rotation = x;
  152. this.transforms();
  153. return this
  154. };
  155. /**
  156. * resize the element
  157. * @memberOf cc.$#
  158. * @name resize
  159. * @function
  160. * @param {Number} x
  161. * @param {Number} y
  162. * @return {cc.$}
  163. */
  164. el.resize = function (x, y) {
  165. this.scale.x = x;
  166. this.scale.y = y;
  167. this.transforms();
  168. return this
  169. };
  170. /**
  171. * skews the element
  172. * @memberOf cc.$#
  173. * @name setSkew
  174. * @function
  175. * @param {Number} x in degrees
  176. * @param {Number} y
  177. * @return {cc.$}
  178. */
  179. el.setSkew = function (x, y) {
  180. this.skew.x = x;
  181. this.skew.y = y;
  182. this.transforms();
  183. return this
  184. };
  185. }
  186. return el;
  187. };
  188. //getting the prefix and css3 3d support
  189. switch (cc.sys.browserType) {
  190. case cc.sys.BROWSER_TYPE_FIREFOX:
  191. cc.$.pfx = "Moz";
  192. cc.$.hd = true;
  193. break;
  194. case cc.sys.BROWSER_TYPE_CHROME:
  195. case cc.sys.BROWSER_TYPE_SAFARI:
  196. cc.$.pfx = "webkit";
  197. cc.$.hd = true;
  198. break;
  199. case cc.sys.BROWSER_TYPE_OPERA:
  200. cc.$.pfx = "O";
  201. cc.$.hd = false;
  202. break;
  203. case cc.sys.BROWSER_TYPE_IE:
  204. cc.$.pfx = "ms";
  205. cc.$.hd = false;
  206. break;
  207. default:
  208. cc.$.pfx = "webkit";
  209. cc.$.hd = true;
  210. }
  211. //cache for prefixed transform
  212. cc.$.trans = cc.$.pfx + "Transform";
  213. //helper function for constructing transform strings
  214. cc.$.translate = (cc.$.hd) ? function (a) {
  215. return "translate3d(" + a.x + "px, " + a.y + "px, 0) "
  216. } : function (a) {
  217. return "translate(" + a.x + "px, " + a.y + "px) "
  218. };
  219. cc.$.rotate = (cc.$.hd) ? function (a) {
  220. return "rotateZ(" + a + "deg) ";
  221. } : function (a) {
  222. return "rotate(" + a + "deg) ";
  223. };
  224. cc.$.scale = function (a) {
  225. return "scale(" + a.x + ", " + a.y + ") "
  226. };
  227. cc.$.skew = function (a) {
  228. return "skewX(" + -a.x + "deg) skewY(" + a.y + "deg)";
  229. };
  230. /**
  231. * Creates a new element, and adds cc.$ methods
  232. * @param {String} x name of the element tag to create
  233. * @return {cc.$}
  234. */
  235. cc.$new = function (x) {
  236. return cc.$(document.createElement(x))
  237. };
  238. cc.$.findpos = function (obj) {
  239. var curleft = 0;
  240. var curtop = 0;
  241. do {
  242. curleft += obj.offsetLeft;
  243. curtop += obj.offsetTop;
  244. } while (obj = obj.offsetParent);
  245. return {x: curleft, y: curtop};
  246. };