Skip to content

Commit

Permalink
Refactor javascript.
Browse files Browse the repository at this point in the history
* Push each functional area to its own file. Thus sufia.js becomes just a
manifest.
* Leverage bootstrap's alert function instead of having our own.
* Reenabled feature spec for proxy deposit
  • Loading branch information
jcoyne committed Apr 12, 2016
1 parent 2f55aa2 commit 3a36d46
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 265 deletions.
119 changes: 10 additions & 109 deletions app/assets/javascripts/sufia.js
Expand Up @@ -6,11 +6,13 @@
//= require jquery-ui/effect
//= require jquery-ui/effect-highlight

//= require bootstrap/dropdown
//= require bootstrap/alert
//= require bootstrap/button
//= require bootstrap/modal
//= require bootstrap/collapse
//= require bootstrap/dropdown
//= require bootstrap/modal
//= require bootstrap/tooltip
// Popover requires that tooltip be loaded first
//= require bootstrap/popover
//= require bootstrap/tab

Expand All @@ -24,13 +26,13 @@
//= require flot/jquery.flot.selection

//= require batch_edit
//= require terms_of_service
//= require notifications_check
//
//= require sufia/app
//= require sufia/fileupload
//= require sufia/permissions
//= require sufia/trophy
//= require sufia/facets
//= require sufia/featured_works
//= require sufia/featured_researcher
//= require sufia/batch_select_all
Expand All @@ -39,125 +41,24 @@
//= require sufia/search
//= require sufia/editor
//= require sufia/ga_events
//= require sufia/select_submit
//= require sufia/tabs
//= require sufia/user_search
//= require sufia/transfers
//= require sufia/user_search
//= require sufia/proxy_rights
//= require sufia/sorting
//= require hydra/batch_select
//= require sufia/dashboard_actions
//= require sufia/batch
// Provide AMD module support
//= require almond
//= require sufia/save_work

//= require hydra_collections
//= require hydra-editor/hydra-editor

//
// For Browse-everything until https://github.com/projecthydra-labs/browse-everything/issues/85 is resolved:
//= require jquery.treetable
//= require browse_everything/behavior
//
//= require browse_everything
//= require nestable

// this needs to be after batch_select so that the form ids get setup correctly
//= require sufia/batch_edit

//over ride the blacklight default to submit
//form when sort by or show per page change
Blacklight.do_select_submit = function() {
$(Blacklight.do_select_submit.selector).each(function() {
var select = $(this);
select.closest("form").find("input[type=submit]").show();
select.bind("change", function() {
return false;
});
});
};
Blacklight.do_select_submit.selector = "form.sort select, form.per_page select";

Blacklight.onLoad(function() {
var sw = require('sufia/save_work/save_work_control');
new sw.SaveWorkControl($("#form-progress")).activate();

// set up global batch edit options to override the ones in the gem
window.batch_edits_options = { checked_label: "",unchecked_label: "",progress_label: "",status_label: "",css_class: "batch_toggle"};

// bootstrap alerts are closed this function
$(document).on('click', '.alert .close' , function(){
$(this).parent().hide();
});

$.fn.selectRange = function(start, end) {
return this.each(function() {
if (this.setSelectionRange) {
this.focus();
this.setSelectionRange(start, end);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};

// initialize popover helpers
$("a[data-toggle=popover]").popover({ html: true })
.click(function() { return false; });


/*
* facets lists
*/
$("li.expandable").click(function(){
$(this).next("ul").slideToggle();
$(this).find('i').toggleClass("glyphicon-chevron-right glyphicon-chevron-down");
});

$("li.expandable_new").click(function(){
$(this).find('i').toggleClass("glyphicon-chevron-right glyphicon-chevron-down");
});

$(".sorts-dash").click(function(){
var itag =$(this).find('i');
toggle_icon(itag);
sort = itag.attr('class') == "caret" ? itag.attr('id')+' desc' : itag.attr('id') +' asc';
$('#sort').val(sort).selected = true;
$("#dashboard_sort_submit").click();
});

$(".sorts").click(function(){
var itag =$(this).find('i');
toggle_icon(itag);
sort = itag.attr('class') == "caret up" ? itag.attr('id')+' desc': itag.attr('id');
$('input[name="sort"]').attr('value', sort);
$("#user_submit").click();
});

}); //closing function at the top of the page

/*
* begin functions
*/

function toggle_icon(itag){
itag.toggleClass("caret");
itag.toggleClass("caret up");
}

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");
}
24 changes: 23 additions & 1 deletion app/assets/javascripts/sufia/app.js
@@ -1 +1,23 @@
Sufia = {};
// Once, javascript is written in a modular format, all initialization
// code should be called from here.
Sufia = {
initialize: function() {
this.save_work_control();
this.popovers();
},

save_work_control: function() {
var sw = require('sufia/save_work/save_work_control');
new sw.SaveWorkControl($("#form-progress")).activate();
},

// initialize popover helpers
popovers: function() {
$("a[data-toggle=popover]").popover({ html: true })
.click(function() { return false; });
}
};

Blacklight.onLoad(function() {
Sufia.initialize();
});
15 changes: 5 additions & 10 deletions app/assets/javascripts/sufia/batch_edit.js
Expand Up @@ -163,14 +163,9 @@ function batch_edit_init () {
$(".field-save").click(runSave);
}

Blacklight.onLoad(function() {
// set up global batch edit options to override the ones in the gem
window.batch_edits_options = { checked_label: "",unchecked_label: "",progress_label: "",status_label: "",css_class: "batch_toggle"};
batch_edit_init();


// turbolinks triggers page:load events on page transition
// If app isn't using turbolinks, this event will never be triggered, no prob.
$(document).on('page:load', function() {
batch_edit_init();
});

$(document).ready(function() {
batch_edit_init();
});
}); //end of Blacklight.onload
14 changes: 14 additions & 0 deletions app/assets/javascripts/sufia/facets.js
@@ -0,0 +1,14 @@
Blacklight.onLoad(function() {
/*
* facets lists
*/
$("li.expandable").click(function(){
$(this).next("ul").slideToggle();
$(this).find('i').toggleClass("glyphicon-chevron-right glyphicon-chevron-down");
});

$("li.expandable_new").click(function(){
$(this).find('i').toggleClass("glyphicon-chevron-right glyphicon-chevron-down");
});

}); //end of Blacklight.onload
16 changes: 16 additions & 0 deletions app/assets/javascripts/sufia/permissions.js
Expand Up @@ -186,6 +186,22 @@ function set_access_levels() {
});
}

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");
}

/*
* make sure the permission being applied is not for a user/group
* that already has a permission.
Expand Down
Expand Up @@ -25,6 +25,7 @@ export class SaveWorkControl {
}
this.element = element
this.form = element.closest('form')
element.data('save_work_control', this)
}

/**
Expand Down
12 changes: 12 additions & 0 deletions app/assets/javascripts/sufia/select_submit.js
@@ -0,0 +1,12 @@
//override the blacklight default to submit
//form when sort by or show per page change
Blacklight.do_select_submit = function() {
$(Blacklight.do_select_submit.selector).each(function() {
var select = $(this);
select.closest("form").find("input[type=submit]").show();
select.bind("change", function() {
return false;
});
});
};
Blacklight.do_select_submit.selector = "form.sort select, form.per_page select";
22 changes: 22 additions & 0 deletions app/assets/javascripts/sufia/sorting.js
@@ -0,0 +1,22 @@
Blacklight.onLoad(function() {
function toggle_icon(itag){
itag.toggleClass("caret");
itag.toggleClass("caret up");
}

$(".sorts-dash").click(function(){
var itag =$(this).find('i');
toggle_icon(itag);
sort = itag.attr('class') == "caret" ? itag.attr('id')+' desc' : itag.attr('id') +' asc';
$('#sort').val(sort).selected = true;
$("#dashboard_sort_submit").click();
});

$(".sorts").click(function(){
var itag =$(this).find('i');
toggle_icon(itag);
sort = itag.attr('class') == "caret up" ? itag.attr('id')+' desc': itag.attr('id');
$('input[name="sort"]').attr('value', sort);
$("#user_submit").click();
});
}); //end of Blacklight.onload
41 changes: 0 additions & 41 deletions app/assets/javascripts/terms_of_service.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/search_builders/sufia/my_shares_search_builder.rb
Expand Up @@ -2,5 +2,5 @@
class Sufia::MySharesSearchBuilder < Sufia::SearchBuilder
include Sufia::MySearchBuilderBehavior

self.default_processor_chain = default_processor_chain - [:filter_models] + [:show_only_shared_files, :show_only_file_sets]
self.default_processor_chain = default_processor_chain - [:filter_models] + [:show_only_shared_files]
end
7 changes: 0 additions & 7 deletions app/search_builders/sufia/search_builder.rb
Expand Up @@ -18,13 +18,6 @@ def show_only_resources_deposited_by_current_user(solr_parameters)
]
end

def show_only_file_sets(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] += [
ActiveFedora::SolrQueryBuilder.construct_query_for_rel(has_model: ::FileSet.to_class_uri)
]
end

def show_only_generic_works(solr_parameters)
solr_parameters[:fq] ||= []
solr_parameters[:fq] += [
Expand Down
4 changes: 2 additions & 2 deletions app/views/_flash_msg.html.erb
@@ -1,8 +1,8 @@
<% flash_types_to_classes = { notice: 'alert-success', error: 'alert-danger', alert: 'alert-warning' } %>
<% [:notice, :error, :alert].each do |type| %>
<% if flash[type] %>
<div class="alert <%= flash_types_to_classes[type] rescue '' %>">
<a class="close" data-dismiss="alert" href="#">&times;</a>
<div class="alert <%= flash_types_to_classes[type] rescue '' %> alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<% if flash[type].respond_to? :join %>
<%= flash[type].join("<br/>").html_safe %>
<% else %>
Expand Down
@@ -1,5 +1,5 @@
<div class="alert alert-success">
<%= t("sufia.upload.cloud_timeout_message", contact_href: link_to(t("sufia.upload.alert.contact_href_text"), sufia.contact_form_index_path)) %>
<%= t("sufia.upload.cloud_timeout_message", contact_href: link_to(t("sufia.upload.alert.contact_href_text"), sufia.contact_form_index_path)) %>
</div>
<%= form_tag(sufia.uploads_path, id: 'browse_everything_form', method: 'post') do %>
<%= hidden_field_tag(:upload_set_id, @upload_set_id) %>
Expand Down
10 changes: 4 additions & 6 deletions app/views/curation_concerns/base/_permission_form.html.erb
Expand Up @@ -8,12 +8,10 @@
</h2>

<div class="alert alert-info hidden" id="save_perm_note">Permissions are <strong>not</strong> saved until the &quot;Save&quot; button is pressed at the bottom of the page.</div>
<div class="alert alert-warning hidden" id="permissions_error">
<a class="close" data-dismiss="alert" href="#">
<span class="sr-only">Close this alert</span>
<span aria-hidden="true">×</span>
</a>
<span id="permissions_error_text"></span></div>
<div class="alert alert-warning hidden alert-dismissable" role="alert" id="permissions_error">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<span id="permissions_error_text"></span>
</div>

<%= render 'curation_concerns/base/form_permission', f: f %>

Expand Down

0 comments on commit 3a36d46

Please sign in to comment.