140medley.js.svn-base 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * 140 medley
  3. * (c) 2011 - Honza Pokorny
  4. * Licensed under the terms of the BSD license
  5. *
  6. * This is a micro-framework or a collection of small, helpful utilities for
  7. * common javascript tasks.
  8. *
  9. * Size:
  10. * Source: 8.6 kb
  11. * Minified: 821 bytes
  12. * gzipped: 504 bytes
  13. *
  14. * Features:
  15. * - templating - t();
  16. * - local storage - s();
  17. * - bind/unbind events - b();
  18. * - create DOM elements - m();
  19. * - DOM selector - $();
  20. * - Get cross-browser xhr - j();
  21. *
  22. */
  23. /*
  24. * Templating
  25. *
  26. * Usage:
  27. * var hello = t("Hello, #{this.name || 'world'}!")
  28. *
  29. * console.log( // => "Hello, Jed!"
  30. * hello({name: "Jed"})
  31. * )
  32. *
  33. * Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
  34. * More: https://gist.github.com/964762
  35. */
  36. var t = function(
  37. a, // the string source from which the template is compiled
  38. b // the default `with` context of the template (optional)
  39. ){
  40. return function(
  41. c, // the object called as `this` in the template
  42. d // the `with` context of this template call (optional)
  43. ){
  44. return a.replace(
  45. /#{([^}]*)}/g, // a regexp that finds the interpolated code: "#{<code>}"
  46. function(
  47. a, // not used, only positional
  48. e // the code matched by the interpolation
  49. ){
  50. return Function(
  51. "x",
  52. "with(x)return " + e // the result of the interpolated code
  53. ).call(
  54. c, // pass the data object as `this`, with
  55. d // the most
  56. || b // specific
  57. || {} // context.
  58. )
  59. }
  60. )
  61. }
  62. };
  63. /*
  64. * LocalStorage
  65. *
  66. * Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
  67. * More: https://gist.github.com/966030
  68. *
  69. */
  70. var s = function(
  71. a, // placeholder for storage object
  72. b // placeholder for JSON
  73. ){
  74. return b
  75. ? { // if JSON is supported
  76. get: function( // provide a getter function
  77. c // that takes a key
  78. ){
  79. return a[c] && // and if the key exists
  80. b.parse(a[c]) // parses and returns it,
  81. },
  82. set: function( // and a setter function
  83. c, // that takes a key
  84. d // and a value
  85. ){
  86. a[c] = // and sets
  87. b.stringify(d) // its serialization.
  88. }
  89. }
  90. : {} // if JSON isn't supported, provide a shim.
  91. }(
  92. this.localStorage // use native localStorage if available
  93. || {}, // or an object otherwise
  94. JSON // use native JSON (required)
  95. )
  96. /*
  97. * Bind/Unbind events
  98. *
  99. * Usage:
  100. * var el = document.getElementyById('#container');
  101. * b(el, 'click', function() {
  102. * console.log('clicked');
  103. * });
  104. *
  105. * Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
  106. * More: https://gist.github.com/968186
  107. *
  108. */
  109. var p = function(
  110. a, // a DOM element
  111. b, // an event name such as "click"
  112. c, // (placeholder)
  113. d // (placeholder)
  114. ){
  115. c = c || document; // use the document by default
  116. d = c[ // save the current onevent handler
  117. b = "on" + b // prepent the event name with "on"
  118. ];
  119. a = c[b] = // cache and replace the current handler
  120. function(e) { // with a function that
  121. d = d && d( // executes/caches the previous handler
  122. e = e || c.event // with a cross-browser object,
  123. );
  124. return (a = a && b(e)) // and calls the passed function,
  125. ? b // returning the current handler if it rebinds
  126. : d // and the previous handler otherwise.
  127. };
  128. c = this // cache the window to fetch IE events
  129. };
  130. /*
  131. * Create DOM element
  132. *
  133. * Usage:
  134. * var el = m('<h1>Hello</h1>');
  135. * document.body.appendChild(el);
  136. *
  137. *
  138. * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  139. * Version 2, December 2004
  140. *
  141. * Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
  142. * More: https://gist.github.com/966233
  143. *
  144. */
  145. var m = function(
  146. a, // an HTML string
  147. b, // placeholder
  148. c // placeholder
  149. ){
  150. b = document; // get the document,
  151. c = b.createElement("p"); // create a container element,
  152. c.innerHTML = a; // write the HTML to it, and
  153. a = b.createDocumentFragment(); // create a fragment.
  154. while ( // while
  155. b = c.firstChild // the container element has a first child
  156. ) a.appendChild(b); // append the child to the fragment,
  157. return a // and then return the fragment.
  158. }
  159. /*
  160. * DOM selector
  161. *
  162. * Usage:
  163. * $('div');
  164. * $('#name');
  165. * $('.name');
  166. *
  167. *
  168. * Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
  169. * More: https://gist.github.com/991057
  170. *
  171. */
  172. var $ = function(
  173. a, // take a simple selector like "name", "#name", or ".name", and
  174. b // an optional context, and
  175. ){
  176. a = a.match(/^(\W)?(.*)/); // split the selector into name and symbol.
  177. return( // return an element or list, from within the scope of
  178. b // the passed context
  179. || document // or document,
  180. )[
  181. "getElement" + ( // obtained by the appropriate method calculated by
  182. a[1]
  183. ? a[1] == "#"
  184. ? "ById" // the node by ID,
  185. : "sByClassName" // the nodes by class name, or
  186. : "sByTagName" // the nodes by tag name,
  187. )
  188. ](
  189. a[2] // called with the name.
  190. )
  191. }
  192. /*
  193. * Get cross browser xhr object
  194. *
  195. *
  196. * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  197. * Version 2, December 2004
  198. *
  199. * Copyright (C) 2011 Jed Schmidt <http://jed.is>
  200. * More: https://gist.github.com/993585
  201. *
  202. */
  203. var j = function(
  204. a // cursor placeholder
  205. ){
  206. for( // for all a
  207. a=0; // from 0
  208. a<4; // to 4,
  209. a++ // incrementing
  210. ) try { // try
  211. return a // returning
  212. ? new ActiveXObject( // a new ActiveXObject
  213. [ // reflecting
  214. , // (elided)
  215. "Msxml2", // the various
  216. "Msxml3", // working
  217. "Microsoft" // options
  218. ][a] + // for Microsoft implementations, and
  219. ".XMLHTTP" // the appropriate suffix,
  220. ) // but make sure to
  221. : new XMLHttpRequest // try the w3c standard first, and
  222. }
  223. catch(e){} // ignore when it fails.
  224. }