-
-
Save grahamb/5861289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onPage(/\/courses\/\d+\/settings/, function() { | |
// do something | |
}); | |
hasAnyRole('admin', function(hasRole) { | |
if (hasRole) { | |
// do something | |
} else { | |
// do something else | |
} | |
}); | |
isUser(1, function(isRyan) { | |
if (isRyan) { | |
// do something | |
} else { | |
// so something else | |
} | |
}); | |
onElementRendered('a[href=#create_ticket]', function(el) { | |
// do something with el (a jquery element collection) | |
}); | |
function onPage(regex, fn) { | |
if (location.pathname.match(regex)) fn(); | |
} | |
function hasAnyRole(/*roles, cb*/) { | |
var roles = [].slice.call(arguments, 0); | |
var cb = roles.pop(); | |
for (var i = 0; i < arguments.length; i++) { | |
if (ENV.current_user_roles.indexOf(arguments[i]) !== -1) { | |
return cb(true); | |
} | |
} | |
return cb(false); | |
} | |
function isUser(id, cb) { | |
cb(ENV.current_user_id == id); | |
} | |
function onElementRendered(selector, cb, _attempts) { | |
var el = $(selector); | |
_attempts = ++_attempts || 1; | |
if (el.length) return cb(el); | |
if (_attempts == 60) return; | |
setTimeout(function() { | |
onElementRendered(selector, cb, _attempts); | |
}, 250); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment