/********************************************************************** * JavaScript Lib * at this point this is just a place I put most of the generic stuff I * use. * * P.S. the name "jli" just stands for Java script LIb, like how it * looks... **********************************************************************/ var DEBUG = true /*********************************************************************/ // XXX find a better name for this... // this will create a function that will add/remove a css_class to elem // calling the optional callbacks before and/or after. // // elem is a jquery compatible object; default use-case: a css selector. // // the resulting function understands the folowing arguments: // - 'on' : switch mode on // - 'off' : switch mode off // - '?' : return current state ('on'|'off') // - no arguments : toggle the state // // NOTE: of only one callback is given then it will be called after the // class change... // a way around this is to pass an empty function as callback_b // function createCSSClassToggler(elem, css_class, callback_a, callback_b){ // prepare the pre/post callbacks... if(callback_b == null){ var callback_pre = null var callback_post = callback_a } else { var callback_pre = callback_a var callback_post = callback_b } // build the acual toggler function... var func = function(action){ if(action == null || action == '?'){ var getter = action == '?' ? true : false action = 'on' // get current state... if( $(elem).hasClass(css_class) ){ action = 'off' } if(getter){ // as the above actions indicate intent and not state, // we'll need to swap the values... return action == 'on' ? 'off' : 'on' } } if(callback_pre != null){ callback_pre(action) } // play with the class... if(action == 'on'){ $(elem).addClass(css_class) } else { $(elem).removeClass(css_class) } if(callback_post != null){ callback_post(action) } } func.doc = 'With no arguments this will toggle between "on" and '+ '"off".\n'+ 'If either "on" or "off" are given then this will switch '+ 'to that mode.\n'+ 'If "?" is given, this will return either "on" or "off" '+ 'depending on the current state.' return func } // show a jQuary opject in viewer overlay... // XXX need to set .scrollTop(0) when showing different UI... // ...and not set it when the UI is the same // XXX this must create it's own overlay... function showInOverlay(obj){ obj.click(function(){ return false }) // XXX $('.viewer').addClass('overlay-mode') // clean things up... $('.overlay .content').children().remove() // put it in the overlay... $('.overlay .content').append(obj) // prepare the overlay... $('.overlay') .one('click', function(){ $('.overlay') .fadeOut(function(){ $('.overlay .content') .children() .remove() $('.overlay-mode').removeClass('overlay-mode') }) }) .fadeIn() return obj } function overlayMessage(text){ return showInOverlay($('
')) } function unanimated(obj, func, time){ return function(){ if(time == null){ time = 5 } obj.addClass('unanimated') var res = func.apply(func, arguments) setTimeout(function(){obj.removeClass('unanimated')}, time) return res } } // Return a scale value for the given element(s). // NOTE: this will only return a single scale value... function getElementScale(elem){ //var transform = elem.css('transform') var vendors = ['o', 'moz', 'ms', 'webkit'] var transform = elem.css('transform') var res // go through vendor prefixes... (hate this!) if(!transform || transform == 'none'){ for(var i in vendors){ transform = elem.css('-' + vendors[i] + '-transform') if(transform && transform != 'none'){ break } } } // no transform is set... if(!transform || transform == 'none'){ return 1 } // get the scale value -- first argument of scale/matrix... return parseFloat((/(scale|matrix)\(([^,]*),.*\)/).exec(transform)[2]) } function setElementScale(elem, scale){ return elem.css({ 'transform': 'scale('+scale+')', '-moz-transform': 'scale('+scale+')', '-o-transform': 'scale('+scale+')', '-ms-transform': 'scale('+scale+')', '-webkit-transform': 'scale('+scale+')', }) } /************************************************ jQuery extensions **/ jQuery.fn.reverseChildren = function(){ return $(this).each(function(_, e){ return $(e).append($(e).children().detach().get().reverse()) }) } jQuery.fn.sortChildren = function(func){ return $(this).each(function(_, e){ return $(e).append($(e).children().detach().get().sort(func)) }) } /********************************************************************** * vim:set ts=4 sw=4 : */