Skip to content

Instantly share code, notes, and snippets.

@bagofarms
Forked from ryanflorence/instructurecon.js
Created June 20, 2013 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bagofarms/5819670 to your computer and use it in GitHub Desktop.
Save bagofarms/5819670 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