Skip to content

Commit

Permalink
Restore single use links to files
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Apr 21, 2016
1 parent 6dd4cde commit 73b4afc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 33 deletions.
36 changes: 21 additions & 15 deletions app/assets/javascripts/sufia/single_use_link.js
@@ -1,29 +1,35 @@
function getSingleUse(item) {
var url = $(item).data('generate_single_use_link_url');
var rurl = window.location.protocol+"//"+window.location.host;
var resp = $.ajax({
headers: { Accept: "application/javascript" },
type: 'get',
function getSingleUse(item, fn) {
console.log(item)
var url = $(item).data('generate-single-use-link-url');
if (!url) {
alert("No url was provided for generating a single use link");
return;
}

$.ajax({
type: 'post',
url: url,
async: false
success: fn
});
return rurl + resp.responseText;
}

// A Turbolinks-enabled link has been clicked
document.addEventListener("page:before-change", function(){
ZeroClipboard.destroy();
});

Blacklight.onLoad(function() {
$.each($(".copypaste"), function(idx, item) {
var clip = new ZeroClipboard(this);

clip.on("copy", function(client, args) {
clip.setText(getSingleUse(item));
clip.on("dataRequested", function(client, args) {
getSingleUse(item, function(data) {
client.setText(data)
});
});

clip.on("aftercopy", function(client, args) {
clip.on("complete", function(client, args) {
alert("A single use link to " + args.text + " was copied to your clipboard.");
});

clip.on("error", function(client, args) {
alert("Your single-use link (please copy): " + getSingleUse(item));
});
});
});
11 changes: 1 addition & 10 deletions app/views/collections/_show_document_list_menu.html.erb
Expand Up @@ -2,19 +2,10 @@
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown">Select an action <span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right">
<li>
<%= link_to '#',
class: "copypaste itemicon itemcode",
title: "Single-Use Link to Work",
id: "copy_link_#{id}",
data: { generate_single_use_link_url: curation_concerns.generate_show_single_use_link_url(id) } do %>
<i class="glyphicon glyphicon-link over"></i> Single-Use Link to Work
<% end %>
</li>
<li>
<%= link_to edit_curation_concerns_generic_work_path(id),
class: "itemicon itemedit",
title: "#{t('sufia.collection.generic_work.edit')}",
title: t('sufia.collection.generic_work.edit'),
id: "edit_work_link_#{id}" do %>
<i class='glyphicon glyphicon-pencil'></i> <%= t('sufia.collection.generic_work.edit') %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/curation_concerns/base/_relationships.html.erb
Expand Up @@ -11,7 +11,7 @@
<% collection_presenters.each do |collection| %>
<ul class="tabular">
<li class='attribute title'>
<%= link_to collection.title, collection_path(collection) %>
<%= link_to collection.title, main_app.collection_path(collection) %>
</li>
</ul>
<% end %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/curation_concerns/file_sets/_actions.html.erb
Expand Up @@ -18,6 +18,15 @@
<%= link_to 'Versions', edit_polymorphic_path([main_app, file_set], anchor: 'versioning_display'),
{ title: "Display previous versions" } %>
</li>

<li role="menuitem" tabindex="-1">
<%= link_to t('sufia.single_use_link'), '#',
class: "copypaste",
title: "Single-Use Link to File",
data: { generate_single_use_link_url: curation_concerns.generate_show_single_use_link_url(file_set.id) },
id: "copy_link_#{file_set.id}" %>
</li>

<% end %>
<% if can?(:destroy, file_set.id) %>
<li role="menuitem" tabindex="-1">
Expand Down
8 changes: 8 additions & 0 deletions app/views/curation_concerns/file_sets/_show_actions.html.erb
Expand Up @@ -8,7 +8,15 @@
<% if @presenter.editor? %>
<%= link_to "Edit This #{@presenter.human_readable_type}", edit_polymorphic_path([main_app, @presenter]), class: 'btn btn-default' %>
<%= link_to "Delete This #{@presenter.human_readable_type}", [main_app, @presenter], class: 'btn btn-danger', data: { confirm: "Delete this #{@presenter.human_readable_type}?" }, method: :delete %>
<%= button_tag t('sufia.single_use_link'),
class: "copypaste btn btn-default",
title: "Single-Use Link to File",
data: { generate_single_use_link_url: curation_concerns.generate_show_single_use_link_url(@presenter.id) },
id: "copy_link_#{@presenter.id}" %>
<% end %>
<%= render 'social_media' %>
</div>

1 change: 1 addition & 0 deletions config/locales/sufia.en.yml
Expand Up @@ -17,6 +17,7 @@ en:
share_button: "Share Your Work"
view_profile: "View Profile"
edit_profile: "Edit Profile"
single_use_link: "Single-Use Link to File"
directory:
suffix: "@example.org"
search:
Expand Down
10 changes: 4 additions & 6 deletions spec/javascripts/single_use_link_spec.js.coffee
Expand Up @@ -2,23 +2,21 @@ describe "single use link", ->

beforeEach ->
# setup two inputs for us to attach auto complete to
setFixtures '<a id="test_link" data-generate_single_use_link_url="/single_use_link/generate_show/abc123" />'
setFixtures '<a id="test_link" data-generate-single-use-link-url="/single_use_link/generate_show/abc123" />'

# call ajax to get a link
it "calls for the expected link", ->
# set up mock and response
resp = responseText: "/single_use_linkabc123"
options = {
headers:
Accept: 'application/javascript'
type: 'get'
type: 'post'
url: "/single_use_link/generate_show/abc123"
async: false
}
se = spyOn($, "ajax").and.returnValue resp

# get the single use link
result = getSingleUse $('#test_link')
var result
getSingleUse $('#test_link'), function(data) { result = data }

#verify the result
expect(result).toEqual "#{window.location.protocol}//#{window.location.host}/single_use_linkabc123"
Expand Down
2 changes: 1 addition & 1 deletion sufia.gemspec
Expand Up @@ -45,7 +45,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'posix-spawn'
spec.add_dependency 'kaminari_route_prefix'
spec.add_dependency 'jquery-ui-rails', '~> 5.0'
spec.add_dependency 'zeroclipboard-rails', '~> 0.1'
spec.add_dependency 'zeroclipboard-rails', '~> 0.0.13'

spec.add_development_dependency 'engine_cart', '~> 0.8'
spec.add_development_dependency 'mida', '~> 0.3'
Expand Down

0 comments on commit 73b4afc

Please sign in to comment.