/*-----------------------------TOP----------------------------------------*/ /*---------------------------ONPAGE Function - required by File Type Limit filter applied below------------------------------------------*/ 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); } /*---------------------------BOTTOM------------------------------------------*/ /*-----------------------------TOP----------------------------------------*/ /* Show list of file types for file limitation on assignment uploads. */ /* Note: uses onPage function by RPFlorence from https://gist.github.com/rpflorence/5817898 */ onPage(/\/courses\/\d+\/assignments\/(\d+\/edit|new)/, function() { onElementRendered('div#allowed_extensions_container', function(el) { //alert('On Page!'); if(!$('div#file_type_selection').length){ var ft = { "Word" : "doc,docx", "Excel" : "xls,xlsx", "PowerPoint" : "ppt,pptx", "PDF Files" : "pdf", "Text Files" : "txt", "Web Pages" : "html,htm", "Images" : "jpg,jpeg,gif,png,tiff,tif,bmp", "Zip Files" : "zip" }; $('div#allowed_extensions_container').append(function(){ curr = $('input#assignment_allowed_extensions').val(); var return_val = '

...or select from the list of common files below:'; var c = 0; var ischecked = ''; $.each(ft, function(key, val){ if(c == 0){ return_val += ''; } if(curr.length>1 && curr.search(val) != -1){ ischecked = 'checked'; }else{ ischecked = ''; } return_val += ''; if(c == 1){ return_val += "\n"; c = 0; }else{ c++; } }); return_val += "\n
' + key + '
"; return return_val; }); } /*Check for checkbox clicks to add/remove items from the file extension list*/ $("input.selectfiletypelist").click(function(){ if($(this).attr('checked') == "checked"){ var ftlval = $('input#assignment_allowed_extensions').val(); if(ftlval.length>0 && ftlval.substr(-1) != ','){ ftlval += ','; } $('input#assignment_allowed_extensions').val(ftlval + $(this).attr('name')); }else{ var ftlval_del = $(this).attr('name'); ftlval = $('input#assignment_allowed_extensions').val(); ftlval = ftlval.replace(new RegExp(ftlval_del, 'g'),""); if(ftlval.length>1){ ftlval = ftlval.replace(new RegExp(',,', 'g'),","); if(ftlval.substr(-1) == ","){ ftlval = ftlval.substr(0,(ftlval.length - 1)); } if(ftlval.substr(0,1) == ","){ ftlval = ftlval.substr(1,ftlval.length); } }else{ ftlval = ''; } $('input#assignment_allowed_extensions').val(ftlval); } }); }); /*Sanity check of file extensions if extensions box is clicked*/ onElementRendered('input#assignment_allowed_extensions', function(el) { if(el.click){ $('input#assignment_allowed_extensions').blur(function (){ var curr = $('input#assignment_allowed_extensions').val(); curr=curr.trim(); if(curr.substr(-1) == ','){ curr=curr.substr(0,(curr.length - 1)); } if(curr.substr(0,1) == ','){ curr=curr.substr(1,(curr.length)); } $('input#assignment_allowed_extensions').val(curr); if(curr.search(/([^A-Za-z0-9,]+)/m) > 0){ alert('There are one or more errors in your Allowed File Extensions field.\n\n *This field should contain a comma (,) delimited list of file extensions (ex: doc,docx,pdf)\n *Do not include a dot (.) as part of the file extension\n *Do not include any non-Alphanumeric (A-Z 0-9) characters other than the comma (,) used to delimit the file extensions') } }); } el.click(function() { var clickval = $('input#assignment_allowed_extensions').val(); if(clickval.substr(-1) != ',' && clickval.length > 0){ $('input#assignment_allowed_extensions').val(clickval + ','); } }); }); }); /*-----------------------------BOTTOM----------------------------------------*/