");
$(this).val("");
return false;
}*/
};
return autocomplete_opts;
}
var autocomplete_vocab = new Object();
autocomplete_vocab.url_var = ['subject', 'language']; // the url variable to pass to determine the vocab to attach to
autocomplete_vocab.field_name = new Array(); // the form name to attach the event for autocomplete
autocomplete_vocab.add_btn_id = new Array(); // the id of the button pressed when adding an additional form element
// loop over the autocomplete fields and attach the
// events for autocomplete and create other array values for autocomplete
for (var i=0; i < autocomplete_vocab.url_var.length; i++) {
autocomplete_vocab.field_name.push('generic_file_' + autocomplete_vocab.url_var[i]);
autocomplete_vocab.add_btn_id.push('additional_' + autocomplete_vocab.url_var[i] + '_submit');
// autocompletes
$("#" + autocomplete_vocab.field_name[i])
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete( get_autocomplete_opts(autocomplete_vocab.url_var[i]) );
}
/*
* adds additional metadata elements
*/
$('.adder').click(function() {
//this.id = additional_N_submit
//id for element to clone = additional_N_clone
//id for element to append to = additional_N_elements
//var cloneId = this.id.replace("submit", "clone");
//var newId = this.id.replace("submit", "elements");
var cloneId = this.id.replace("submit", "clone");
var newId = this.id.replace("submit", "elements");
var cloneElem = $('#'+cloneId).clone();
// change the add button to a remove button
var plusbttn = cloneElem.find('#'+this.id);
//plusbttn.attr("value","-");
plusbttn.html('-remove this '+ this.name.replace("_", " ") +'');
plusbttn.on('click',removeField);
// remove the help tag on subsequent added fields
cloneElem.find('.formHelp').remove();
cloneElem.find('i').remove();
cloneElem.find('.modal-div').remove();
//clear out the value for the element being appended
//so the new element has a blank value
cloneElem.find('input[type=text]').attr("value", "");
cloneElem.find('input[type=text]').attr("required", false);
// should we attach an auto complete based on the input
if (this.id == 'additional_based_near_submit') {
cloneElem.find('input[type=text]').autocomplete(cities_autocomplete_opts);
}
else if ( (index = $.inArray(this.id, autocomplete_vocab.add_btn_id)) != -1 ) {
cloneElem.find('input[type=text]').autocomplete(get_autocomplete_opts(autocomplete_vocab.url_var[index]));
}
$('#'+newId).append(cloneElem);
cloneElem.find('input[type=text]').focus();
return false;
});
$('.remover').click(removeField);
function removeField () {
// get parent and remove it
$(this).parent().remove();
return false;
}
// show/hide more information on the dashboard when clicking
// plus/minus
$('.icon-plus').on('click', function() {
//this.id format: "expand_NNNNNNNNNN"
var a = this.id.split("expand_");
if (a.length > 1) {
var docId = a[1];
$("#detail_"+docId).toggle();
if( $("#detail_"+docId).is(":hidden") ) {
$("#expand_"+docId).attr("class", "icon-plus icon-large");
}
else {
$("#expand_"+docId).attr("class", "icon-minus icon-large");
}
}
return false;
});
$('#add_descriptions').click(function() {
$('#more_descriptions').show();
$('#add_descriptions').hide();
return false;
});
$("a[rel=popover]").click(function() { return false;});
/*
*
*
* permissions
*
* ids that end in 'skel' are only used as elements
* to clone into real form elements that are then
* submitted
*/
// input for uids - attach function to verify uid
$('#new_user_name_skel').on('blur', function() {
// clear out any existing messages
$('#directory_user_result').html('');
var un = $('#new_user_name_skel').val();
var perm = $('#new_user_permission_skel').val();
if ( $.trim(un).length == 0 ) {
return;
}
$.ajax( {
url: "/directory/user/" + un,
success: function( data ) {
if (data != null) {
if (!data.length) {
$('#directory_user_result').html('User id ('+un+ ') does not exist.');
$('#new_user_name_skel').select();
$('#new_user_permission_skel').val('none');
return;
}
else {
$('#new_user_permission_skel').focus();
}
}
}
});
});
// add button for new user
$('#add_new_user_skel').on('click', function() {
if ($('#new_user_name_skel').val() == "" || $('#new_user_permission_skel :selected').index() == "0") {
$('#new_user_name_skel').focus();
return false;
}
if ($('#new_user_name_skel').val() == $('#file_owner').html()) {
$('#permissions_error_text').html("Cannot change owner permissions.");
$('#permissions_error').show();
$('#new_user_name_skel').val('');
$('#new_user_name_skel').focus();
return false;
}
if (!is_permission_duplicate($('#new_user_name_skel').val())) {
$('#permissions_error_text').html("This user already has a permission.");
$('#permissions_error').show();
$('#new_user_name_skel').focus();
return false;
}
$('#permissions_error').html();
$('#permissions_error').hide();
var un = $('#new_user_name_skel').val();
var perm_form = $('#new_user_permission_skel').val();
var perm = $('#new_user_permission_skel :selected').text();
// clear out the elements to add more
$('#new_user_name_skel').val('');
$('#new_user_permission_skel').val('none');
addPerm(un, perm_form, perm, 'new_user_name');
return false;
});
// add button for new user
$('#add_new_group_skel').on('click', function() {
if ($('#new_group_name_skel :selected').index() == "0" || $('#new_group_permission_skel :selected').index() == "0") {
$('#new_group_name_skel').focus();
return false;
}
var cn = $('#new_group_name_skel').val();
var perm_form = $('#new_group_permission_skel').val();
var perm = $('#new_group_permission_skel :selected').text();
if (!is_permission_duplicate($('#new_group_name_skel').val())) {
$('#permissions_error_text').html("This group already has a permission.");
$('#permissions_error').show();
$('#new_group_name_skel').focus();
return false;
}
$('#permissions_error').html();
$('#permissions_error').hide();
// clear out the elements to add more
$('#new_group_name_skel').val('');
$('#new_group_permission_skel').val('none');
addPerm(cn, perm_form, perm, 'new_group_name');
return false;
});
function addPerm(un, perm_form, perm, perm_type)
{
var tr = $(document.createElement('tr'));
var td1 = $(document.createElement('td'));
var td2 = $(document.createElement('td'));
var remove = $('');
$('#save_perm_note').show();
$('#new_perms').append(td1);
$('#new_perms').append(td2);
td1.html('');
td2.html(perm);
td2.append(remove);
remove.click(function () {
tr.remove();
});
$('').attr({
type: 'hidden',
name: 'generic_file[permissions]['+perm_type+']['+un+']',
value: perm_form
}).appendTo(td2);
tr.append(td1);
tr.append(td2);
$('#file_permissions').after(tr);
tr.effect("highlight", {}, 3000);
}
$('.remove_perm').on('click', function() {
var top = $(this).parent().parent();
top.hide(); // do not show the block
top.find('.select_perm')[0].options[0].selected= true; // select the first otion which is none
return false;
});
// called from edit object view
$('#edit_descriptions_link').on('click', function() {
descriptions_tab();
});
// called from edit object view
$('#edit_versioning_link').on('click', function() {
versions_tab();
});
// called from edit object view
$('#edit_permissions_link').on('click', function() {
permissions_tab();
});
// when user clicks on visibility, update potential access levels
$("input[name='visibility']").on("change", set_access_levels);
$('#generic_file_permissions_new_group_name').change(function (){
var edit_option = $("#generic_file_permissions_new_group_permission option[value='edit']")[0];
if (this.value.toUpperCase() == 'PUBLIC') {
edit_option.disabled =true;
} else {
edit_option.disabled =false;
}
});
/*
* facets lists
*/
$("li.expandable").click(function(){
$(this).next("ul").slideToggle();
$(this).find('i').toggleClass("icon-chevron-down");
});
$("li.expandable_new").click(function(){
$(this).find('i').toggleClass("icon-chevron-down");
});
/*
* enlarge icons on hover- on dashboard
*/
/*
$('[class^="icon-"]').hover(
//on mouseover
function(){
$(this).addClass("icon-large");
},
//on mouseout
function() {
$(this).removeClass("icon-large");
});
*/
$(".sorts-dash").click(function(){
var itag =$(this).find('i');
toggle_icon(itag);
sort = itag.attr('class') == "icon-caret-down" ? itag.attr('id')+' desc': itag.attr('id') +' asc';
$('#sort').val(sort).selected = true;
$(".icon-refresh").parent().click();
});
$(".sorts").click(function(){
var itag =$(this).find('i');
toggle_icon(itag);
sort = itag.attr('class') == "icon-caret-down" ? itag.attr('id')+' desc': itag.attr('id');
$('input[name="sort"]').attr('value', sort);
$(".icon-search").parent().click();
});
}); //closing function at the top of the page
/*
* begin functions
*/
function toggle_icon(itag){
itag.toggleClass("icon-caret-down");
itag.toggleClass("icon-caret-up");
}
// return the files visibility level (penn state, open, restricted);
function get_visibility(){
return $("input[name='visibility']:checked").val()
}
/*
* if visibility is Open or Penn State then we can't selectively
* set other users/groups to 'read' (it would be over ruled by the
* visibility of Open or Penn State) so disable the Read option
*/
function set_access_levels()
{
var vis = get_visibility();
var enabled_disabled = false;
if (vis == "open" || vis == "psu") {
enabled_disabled = true;
}
$('#new_group_permission_skel option[value=read]').attr("disabled", enabled_disabled);
$('#new_user_permission_skel option[value=read]').attr("disabled", enabled_disabled);
var perms_sel = $("select[name^='generic_file[permissions]']");
$.each(perms_sel, function(index, sel_obj) {
$.each(sel_obj, function(j, opt) {
if( opt.value == "read") {
opt.disabled = enabled_disabled;
}
});
});
}
/*
* make sure the permission being applied is not for a user/group
* that already has a permission.
*/
function is_permission_duplicate(user_or_group_name)
{
s = "[" + user_or_group_name + "]";
var patt = new RegExp(preg_quote(s), 'gi');
var perms_input = $("input[name^='generic_file[permissions]']");
var perms_sel = $("select[name^='generic_file[permissions]']");
var flag = 1;
perms_input.each(function(index, form_input) {
// if the name is already being used - return false (not valid)
if (patt.test(form_input.name)) {
flag = 0;
}
});
if (flag) {
perms_sel.each(function(index, form_input) {
// if the name is already being used - return false (not valid)
if (patt.test(form_input.name)) {
flag = 0;
}
});
}
// putting a return false inside the each block
// was not working. Not sure why would seem better
// rather than setting this flag var
return (flag ? true : false);
}
// is it worth checking to make sure users aren't filling up permissions that will be ignored.
// or when a user has already set a permission for a user then updates the visibility -- is it
// still relevant
function validate_existing_perms()
{
var vis = get_visibility();
if (vis == "open" || vis == "psu")
{
var perms = $("input[name^='generic_file[permissions]']");
$.each(perms, function(index, form_input) {
if (form_input.name != "generic_file[permissions][group][public]" && form_input.name != "generic_file[permissions][group][registered]") {
if (form_input.value != 'edit') {
alert("silly permission: " + form_input.name + " " + form_input.value );
}
}
});
}
}
// all called from edit object view
// when permissions link is clicked on edit object
function permissions_tab ()
{
$('#edit_permissions_link').attr('class', 'active');
$('#edit_versioning_link').attr('class', '');
$('#edit_descriptions_link').attr('class', '');
$('#descriptions_display').hide();
$('#versioning_display').hide();
$('#permissions_display').show();
$('#permissions_submit').show();
}
// when versions link is clicked on edit object
function versions_tab()
{
$('#edit_descriptions_link').attr('class', '');
$('#edit_versioning_link').attr('class', 'active');
$('#edit_permissions_link').attr('class', '');
$('#descriptions_display').hide();
$('#versioning_display').show();
$('#permissions_display').hide();
$('#permissions_submit').hide();
}
// when descriptions link is clicked on edit object
function descriptions_tab ()
{
$('#edit_descriptions_link').attr('class', 'active');
$('#edit_versioning_link').attr('class', '');
$('#edit_permissions_link').attr('class', '');
$('#descriptions_display').show();
$('#versioning_display').hide();
$('#permissions_display').hide();
$('#permissions_submit').hide();
}
function preg_quote( str ) {
// http://kevin.vanzonneveld.net
// + original by: booeyOH
// + improved by: Ates Goral (http://magnetiq.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// * example 1: preg_quote("$40");
// * returns 1: '\$40'
// * example 2: preg_quote("*RRRING* Hello?");
// * returns 2: '\*RRRING\* Hello\?'
// * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
// * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
}
function initialize_audio() {
if (navigator.userAgent.match("Chrome")){
$('audio').each(function() {
this.controls = true;
//$(this).attr("controls","controls");
});
}else {
$('audio').each(function() {
$(this).attr("preload","auto");
});
audiojs.events.ready(function() {
var as = audiojs.createAll({
imageLocation: '/assets/player-graphics.gif',
swfLocation: '/assets/audiojs.swf'
});
});
};
}