Skip to content

Instantly share code, notes, and snippets.

@grahamb
Forked from ryanflorence/instructurecon.js
Created June 25, 2013 18:57
Show Gist options
  • Save grahamb/5861289 to your computer and use it in GitHub Desktop.
Save grahamb/5861289 to your computer and use it in GitHub Desktop.
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