PageRenderTime 50ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/text-effects/grrrid/jquery.grrrid-1.0.0.js

https://bitbucket.org/vins13pattar/handpicked.jquery.plugins.repo
JavaScript | 538 lines | 381 code | 91 blank | 66 comment | 121 complexity | be3f89881551a27810575f8dd817d117 MD5 | raw file
  1. /*
  2. * jQuery grrrid 1.0.0,
  3. *
  4. * Copyright (c) 2012 Fred Heusschen
  5. * www.frebsite.nl
  6. *
  7. * Plugin website:
  8. * grrrid.frebsite.nl
  9. *
  10. * Dual licensed under the MIT and GPL licenses.
  11. * http://en.wikipedia.org/wiki/MIT_License
  12. * http://en.wikipedia.org/wiki/GNU_General_Public_License
  13. */
  14. (function($) {
  15. if ($.fn.grrrid) return;
  16. $.fn.grrrid = function( _f, _o1, _o2, _o3 ) {
  17. if ( this.length == 0 ) {
  18. debug( true, 'No element found for "' + this.selector+'".' );
  19. return this;
  20. }
  21. var $grr = this;
  22. if ( $grr.data( 'grr_is_grrrid' ) ) {
  23. $grr.trigger( 'destroy.grr' );
  24. }
  25. $grr.add( $grr.children() ).each( function() {
  26. $(this).data( 'grr_orgCSS', {
  27. 'width' : $(this).css( 'width' ),
  28. 'height' : $(this).css( 'height' ),
  29. 'marginTop' : $(this).css( 'marginTop' ),
  30. 'marginRight' : $(this).css( 'marginRight' ),
  31. 'marginBottom' : $(this).css( 'marginBottom' ),
  32. 'marginLeft' : $(this).css( 'marginLeft' ),
  33. 'display' : $(this).css( 'display' )
  34. });
  35. });
  36. $grr.bind_events = function() {
  37. var dims = grrrid_dimensions;
  38. // The equalize event
  39. $grr.bind( 'equalize.grr', function( e, dimension, options ) {
  40. e.preventDefault();
  41. e.stopPropagation();
  42. // remove equalize
  43. if ( typeof dimension == 'boolean' && !dimension ) {
  44. $(this).trigger( 'resetCss.grr' );
  45. return false;
  46. }
  47. // set variables
  48. var opts = $.extend( true, {}, $.fn.grrrid.defaults.equalize, options );
  49. var largest, current, outer, inner
  50. // set dimension
  51. if ( dimension != 'width' ) {
  52. dimension = 'height';
  53. }
  54. dimension = dims[ dimension ];
  55. // set elements
  56. var $lms = ( opts.elements )
  57. ? gr_getElements( opts.elements, $grr )
  58. : $grr;
  59. // reset CSS?
  60. if ( opts.resetCss ) {
  61. $lms.trigger( 'resetCss.grr' );
  62. }
  63. // fix inline elements
  64. $lms.each( function() {
  65. if ( $(this).css( 'display' ) == 'inline' ) {
  66. $(this).css( 'display', 'inline-block' );
  67. }
  68. });
  69. // get largest size
  70. largest = 0;
  71. $lms.each( function() {
  72. current = $(this)[ dimension.outer ]();
  73. if ( current > largest ) {
  74. largest = current;
  75. }
  76. });
  77. // adjust to adjust
  78. opts.adjust = gr_getAdjusted( largest, opts.adjust, $grr );
  79. if ( typeof opts.adjust == 'number' ) {
  80. largest = opts.adjust;
  81. }
  82. // adjust to min / max
  83. opts.min = gr_getNumber( opts.min, $grr );
  84. opts.max = gr_getNumber( opts.max, $grr );
  85. if ( typeof opts.min == 'number' && largest < opts.min ) {
  86. largest = opts.min;
  87. }
  88. if ( typeof opts.max == 'number' && largest > opts.max ) {
  89. largest = opts.max;
  90. }
  91. // set sizes
  92. $lms.each(function() {
  93. $(this)[ dimension.outer ]( largest );
  94. });
  95. });
  96. // The justify event
  97. $grr.bind( 'justify.grr', function( e, dimension, options ) {
  98. e.preventDefault();
  99. e.stopPropagation();
  100. // remove justify
  101. if ( typeof dimension == 'boolean' && !dimension ) {
  102. $(this).trigger( 'resetCss.grr' );
  103. return false;
  104. }
  105. $(this).data( 'grr_has_children', true );
  106. // set variables
  107. var opts = $.extend( true, {}, $.fn.grrrid.defaults.justify, options );
  108. var avail, float, size, thisMargTop, thisMargBot, prevMargBot;
  109. var $first, $last;
  110. // set dimension
  111. if ( dimension != 'width' ) {
  112. dimension = 'height';
  113. }
  114. dimension = dims[ dimension ];
  115. // set elements
  116. var $lms = ( opts.elements )
  117. ? gr_getElements( opts.elements, $(this) )
  118. : $(this).children();
  119. $first = $lms.first();
  120. $last = $lms.last();
  121. // fix inline elements
  122. $lms.each( function() {
  123. if ( $(this).css( 'display' ) == 'inline' ) {
  124. $(this).css( 'display', 'inline-block' );
  125. }
  126. });
  127. // reset CSS?
  128. if ( opts.resetCss ) {
  129. $(this).trigger( 'resetCss.grr' );
  130. }
  131. // remove margins from first and last
  132. $first.css( dimension.margin[ 0 ], 0 );
  133. $last.css( dimension.margin[ 1 ], 0 );
  134. // get available size
  135. if ( !opts.size ) {
  136. opts.size = $first.parent();
  137. }
  138. avail = gr_getAvailableSize( opts.size, dimension, $(this) );
  139. if ( typeof avail != 'number' ) {
  140. avail = gr_getTrueInnerSize( $first.parent(), dimension );
  141. }
  142. // adjust to adjust
  143. opts.adjust = gr_getAdjusted( avail, opts.adjust, $grr );
  144. if ( typeof opts.adjust == 'number' ) {
  145. avail = opts.adjust;
  146. }
  147. // remove margins from available
  148. prevMargBot = null;
  149. $lms.each( function() {
  150. thisMargTop = gr_getCssNumber( $(this), dimension.margin[ 0 ] );
  151. thisMargBot = gr_getCssNumber( $(this), dimension.margin[ 1 ] );
  152. avail -= thisMargTop;
  153. avail -= thisMargBot;
  154. // if NOT floating, previous bottom margin and current top margin are overlapping,
  155. // re-add the smalles to the available size
  156. if ( prevMargBot && prevMargBot > 0 ) {
  157. float = $(this).css( 'float' );
  158. if ( float != 'left' && float != 'right' ) {
  159. avail += ( prevMargBot < thisMargTop ) ? prevMargBot : thisMargTop;
  160. }
  161. }
  162. prevMargBot = thisMargBot;
  163. });
  164. // set sizes
  165. size = avail / $lms.length;
  166. $lms.each( function() {
  167. $(this)[ dimension.outer ]( size );
  168. });
  169. });
  170. // The grid event
  171. $grr.bind( 'grid.grr', function( e, horizontal, vertical, options ) {
  172. e.preventDefault();
  173. e.stopPropagation();
  174. // remove grid
  175. if ( typeof dimension == 'boolean' && !dimension ) {
  176. $(this).trigger( 'resetCss.grr' );
  177. return false;
  178. }
  179. // set variables
  180. if ( typeof horizontal != 'number' ) {
  181. horizontal = 'auto';
  182. }
  183. if ( typeof vertical == 'object' ) {
  184. if ( typeof options != 'object' ) {
  185. options = vertical;
  186. }
  187. }
  188. if ( typeof vertical != 'number' ) {
  189. vertical = 'auto';
  190. }
  191. if ( horizontal == 'auto' && vertical == 'auto' ) {
  192. return false;
  193. }
  194. var opts = $.extend( true, {}, $.fn.grrrid.defaults.grid, options );
  195. var sizes, dimension, size, adjusted;
  196. // reset CSS?
  197. if ( opts.resetCss ) {
  198. $(this).trigger( 'resetCss.grr' );
  199. }
  200. // fix inline elements
  201. if ( $(this).css( 'display' ) == 'inline' ) {
  202. $(this).css( 'display', 'inline-block' );
  203. }
  204. // set sizes
  205. sizes = {
  206. 'width' : horizontal,
  207. 'height': vertical
  208. };
  209. var $this = $(this);
  210. $.each( sizes, function( i, v ) {
  211. if ( v != 'auto' ) {
  212. dimension = dims[ i ];
  213. size = $this[ dimension.outer ]();
  214. size = size + v - ( size % v );
  215. // adjust to adjust
  216. adjusted = gr_getAdjusted( size, opts.adjust, $this );
  217. if ( typeof opts.adjust == 'number' ) {
  218. size = adjusted;
  219. }
  220. // adjust to min / max
  221. opts.min = gr_getNumber( opts.min, $this );
  222. opts.max = gr_getNumber( opts.max, $this );
  223. if ( typeof opts.min == 'number' && size < opts.min ) {
  224. size = opts.min;
  225. }
  226. if ( typeof opts.max == 'number' && size > opts.max ) {
  227. size = opts.max;
  228. }
  229. if ( size != v ) {
  230. $this[ dimension.outer ]( size );
  231. }
  232. }
  233. });
  234. });
  235. // Reset and Destroy events
  236. $grr.bind( 'resetCss.grr', function( e ) {
  237. e.preventDefault();
  238. e.stopPropagation();
  239. $(this).css( $(this).data( 'grr_orgCSS' ) );
  240. if ( $(this).data( 'grr_has_children' ) ) {
  241. $(this).trigger( 'resetChildrenCss.grr' );
  242. }
  243. });
  244. $grr.bind( 'resetChildrenCss.grr', function( e ) {
  245. $(this).data( 'grr_has_children', false );
  246. $(this).children().each( function() {
  247. $(this).css( $(this).data( 'grr_orgCSS' ) );
  248. });
  249. });
  250. $grr.bind( 'destroy.grr', function( e ) {
  251. e.preventDefault();
  252. e.stopPropagation();
  253. $(this).trigger( 'resetCss.grr' );
  254. $(this).unbind( '.grr' );
  255. $(this).data( 'grr_is_grrrid', false );
  256. });
  257. }; // /bind_events
  258. $grr.data( 'grr_is_grrrid', true );
  259. $grr.bind_events();
  260. if ( typeof _f == 'boolean' && !_f ) {
  261. $grr.trigger( 'resetCss.grr' );
  262. return false;
  263. } else if ( _f ) {
  264. switch ( _f ) {
  265. case 'equalize':
  266. $grr.first().trigger( _f + '.grr', [ _o1, _o2, _o3 ] );
  267. break;
  268. case 'justify':
  269. case 'grid':
  270. $grr.trigger( _f + '.grr', [ _o1, _o2, _o3 ] );
  271. break;
  272. }
  273. }
  274. return $grr;
  275. };
  276. // PUBLIC
  277. $.fn.grrrid.defaults = {
  278. 'equalize': {
  279. elements: null,
  280. min: null,
  281. max: null,
  282. adjust: null,
  283. resetCss: false
  284. },
  285. 'justify': {
  286. elements: null,
  287. size: null,
  288. adjust: null,
  289. resetCss: false
  290. },
  291. 'grid': {
  292. horizontal: null,
  293. vertical: null,
  294. min: null,
  295. max: null,
  296. adjust: null,
  297. resetCss: false
  298. }
  299. };
  300. // PRIVATE
  301. var grrrid_dimensions = {
  302. 'width': {
  303. 'size' : 'width',
  304. 'outer' : 'outerWidth',
  305. 'inner' : 'innerWidth',
  306. 'padding' : [ 'paddingLeft', 'paddingRight' ],
  307. 'margin' : [ 'marginLeft', 'marginRight' ],
  308. 'border' : [ 'borderLeftWidth', 'borderRightWidth' ],
  309. },
  310. 'height': {
  311. 'size' : 'height',
  312. 'outer' : 'outerHeight',
  313. 'inner' : 'innerHeight',
  314. 'padding' : [ 'paddingTop', 'paddingBottom' ],
  315. 'margin' : [ 'marginTop', 'marginBottom' ],
  316. 'border' : [ 'borderTopWidth', 'borderBottomWidth' ]
  317. }
  318. };
  319. function gr_getElements( l, $gr ) {
  320. // jquery
  321. if ( l instanceof $ ) {
  322. return l;
  323. // string or dom element
  324. } else if ( typeof l == 'string' || l instanceof HTMLElement ) {
  325. return $(l);
  326. // function
  327. } else if ( $.isFunction( l ) ) {
  328. var $el = $([]);
  329. $gr.each( function() {
  330. var el = l.call( this );
  331. if ( el ) {
  332. $el = $el.add( gr_getElements( el ) );
  333. }
  334. });
  335. return $el;
  336. // array or object
  337. } else if ( $.isArray( l ) || typeof l == 'object' ) {
  338. switch ( l.length ) {
  339. case 1:
  340. return $( gr_getElements( l[ 0 ] ) );
  341. break;
  342. default:
  343. var $el = $([]);
  344. $.each( l, function( i, v ) {
  345. $el = $el.add( gr_getElements( v ) );
  346. });
  347. return $el;
  348. }
  349. }
  350. // no value found
  351. return l;
  352. }
  353. function gr_getNumber( n, $gr ) {
  354. // string
  355. if ( typeof n == 'string' ) {
  356. return parseInt( n, 10 );
  357. // function
  358. } else if ( $.isFunction( n ) ) {
  359. var en = n.call( $gr[ 0 ] );
  360. if ( en ) {
  361. return gr_getNumber( en, $gr );
  362. }
  363. }
  364. // no value found
  365. return n;
  366. }
  367. function gr_getAvailableSize( s, dms, $gr ) {
  368. // number
  369. if ( typeof s == 'number' ) {
  370. return s;
  371. }
  372. // jquery
  373. s = gr_getElements( s, $gr );
  374. if ( s instanceof $ ) {
  375. return gr_getTrueInnerSize( s, dms );
  376. }
  377. // string
  378. if ( typeof s == 'string' ) {
  379. return gr_getNumber( s, $gr );
  380. }
  381. return s;
  382. }
  383. function gr_getAdjusted( o, adj, $gr ) {
  384. // string
  385. if ( typeof adj == 'string' ) {
  386. // percentage
  387. if ( adj.substr( -1 ) == '%' ) {
  388. var pc = gr_getNumber( adj.substr( 0, adj.length-1 ), $gr );
  389. return Math.ceil( o * pc / 100 );
  390. }
  391. return o + gr_getNumber( adj, $gr );
  392. // function
  393. } else if ( $.isFunction( adj ) ) {
  394. var ad = adj.call( $gr[ 0 ], o );
  395. if ( ad ) {
  396. return gr_getAdjusted( o, ad, $gr );
  397. }
  398. // number
  399. } else if ( typeof adj == 'number' ) {
  400. return o + adj;
  401. }
  402. // no value found
  403. return o;
  404. }
  405. function gr_getTrueInnerSize( $el, dms ) {
  406. var size = $el[ dms.inner ]();
  407. var pad0 = gr_getCssNumber( $el, dms.padding[ 0 ] );
  408. var pad1 = gr_getCssNumber( $el, dms.padding[ 1 ] );
  409. return size - ( pad0 + pad1 );
  410. }
  411. function gr_getCssNumber( $el, prop ) {
  412. return parseInt( $el.css( prop ) , 10 )
  413. }
  414. function debug( d, m ) {
  415. if ( !d ) {
  416. return false;
  417. }
  418. if ( typeof m == 'string' ) {
  419. m = 'grrrid: ' + m;
  420. } else {
  421. m = [ 'grrrid:', m ];
  422. }
  423. if ( window.console && window.console.log ) {
  424. window.console.log( m );
  425. }
  426. return false;
  427. }
  428. // Override innerWidth / innerHeight methods to also use them as setters
  429. var _orgOuterWidth = $.fn.outerWidth;
  430. $.fn.outerWidth = function( size ) {
  431. if ( typeof size == 'number' ) {
  432. var outer = this.outerWidth();
  433. var inner = gr_getTrueInnerSize( this, grrrid_dimensions.width );
  434. return this.width( size - ( outer - inner ) ) ;
  435. }
  436. return _orgOuterWidth.call( this );
  437. };
  438. var _orgOuterHeight = $.fn.outerHeight;
  439. $.fn.outerHeight = function( size ) {
  440. if ( typeof size == 'number' ) {
  441. var outer = this.outerHeight();
  442. var inner = gr_getTrueInnerSize( this, grrrid_dimensions.height );
  443. return this.height( Math.floor( size - ( outer - inner ) ) ) ;
  444. }
  445. return _orgOuterHeight.call( this );
  446. };
  447. })(jQuery);