fixed a couple of bugs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-02-26 19:05:29 +04:00
parent 21fca66036
commit 0d4e7aaeae
2 changed files with 25 additions and 12 deletions

View File

@ -13,14 +13,14 @@
/*********************************************************************/ /*********************************************************************/
// this will create a function that will cycle through a class_list on elem // This will create a function that will cycle through a class_list on elem
// calling the optional callbacks before and/or after. // calling the optional callbacks before and/or after.
// if class_list is given as a string, then this will create a toggler that // If class_list is given as a string, then this will create a toggler that
// will turn the given class on the element on and off. // will turn the given class on the element on and off.
// //
// elem is a jquery compatible object; default use-case: a css selector. // Elem is a jquery compatible object; default use-case: a css selector.
// //
// if class_list is a string, the resulting function understands the // If class_list is a string, the resulting function understands the
// folowing arguments: // folowing arguments:
// - <index> : 0 for 'off' and 1 for 'on' (see below) // - <index> : 0 for 'off' and 1 for 'on' (see below)
// - 'on' : switch mode on -- add class // - 'on' : switch mode on -- add class
@ -28,7 +28,7 @@
// - '?' : return current state ('on'|'off') // - '?' : return current state ('on'|'off')
// - no arguments : toggle the state // - no arguments : toggle the state
// //
// otherwise, if class_list is a list of strings: // Otherwise, if class_list is a list of strings:
// - <index> : explicitly set the state to index in class_list // - <index> : explicitly set the state to index in class_list
// - <class-name> : explicitly set a class from the list // - <class-name> : explicitly set a class from the list
// - '?' : return current state ('on'|'off') // - '?' : return current state ('on'|'off')
@ -45,6 +45,17 @@
// to write the classes without leading dots, this now will normalize // to write the classes without leading dots, this now will normalize
// the class list, so now this will correctly treat both dotted // the class list, so now this will correctly treat both dotted
// and non-dotted class names... // and non-dotted class names...
//
//
// This also takes one or two callbacks. If only one is given then it is
// called after (post) the change is made. If two are given then the first
// is called before the change and the second after the change.
// The callbacks are passed the current action.
//
// NOTE: the pre-callback will get the "intent" action, i.e. the state the
// we are changing into but the changes are not yet made.
// NOTE: if the pre-callback explicitly returns false, then the change will
// not be made.
function createCSSClassToggler(elem, class_list, callback_a, callback_b){ function createCSSClassToggler(elem, class_list, callback_a, callback_b){
var bool_action = false var bool_action = false
if(typeof(class_list) == typeof('')){ if(typeof(class_list) == typeof('')){
@ -55,8 +66,8 @@ function createCSSClassToggler(elem, class_list, callback_a, callback_b){
// NOTE: this is here because I've made the error of including a // NOTE: this is here because I've made the error of including a
// leading "." almost every time I use this after I forget // leading "." almost every time I use this after I forget
// the UI... // the UI...
class_list = $(class_list).map(function(i, e){ class_list = $(class_list).map(function(_, e){
return $(e.split(' ')).map(function(i, c){ return $(e.split(' ')).map(function(_, c){
c = c.trim() c = c.trim()
return c[0] == '.' ? c.slice(1) : c return c[0] == '.' ? c.slice(1) : c
}).toArray().join(' ') }).toArray().join(' ')
@ -122,8 +133,11 @@ function createCSSClassToggler(elem, class_list, callback_a, callback_b){
} }
// pre callback... // pre callback...
if(callback_a != null){ if(callback_pre != null){
callback_a(action) var res = callback_pre(action)
if(res === false){
return
}
} }
// update the element... // update the element...
elem.removeClass(class_list.join(' ')) elem.removeClass(class_list.join(' '))
@ -131,8 +145,8 @@ function createCSSClassToggler(elem, class_list, callback_a, callback_b){
elem.addClass(cls) elem.addClass(cls)
} }
// post callback... // post callback...
if(callback_b != null){ if(callback_post != null){
callback_b(action) callback_post(action)
} }
return action return action

View File

@ -93,7 +93,6 @@ var _PAGE_VIEW
var togglePageView = createCSSClassToggler( var togglePageView = createCSSClassToggler(
'.viewer', '.viewer',
'page-view-mode', 'page-view-mode',
null,
// post-change callback... // post-change callback...
function(action){ function(action){
if(action == 'on'){ if(action == 'on'){