-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TriplePoweredProperties functionality to fetch and cache RDF, fronten…
…d and backend URL validation, and frontend UI for viewing labels
- Loading branch information
Josh Gum
committed
Aug 12, 2016
1 parent
d9a87f8
commit d6cb237
Showing
24 changed files
with
603 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
* | ||
*= require_tree . | ||
*= require_self | ||
*= require triple_powered_properties | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<%= f.input key, | ||
as: :triple_powered_property, | ||
input_html: { | ||
class: 'form-control', | ||
data: { 'autocomplete-url' => "/authorities/search/geonames", | ||
'autocomplete' => key } | ||
}, | ||
required: f.object.required?(key), | ||
multi_value: f.object.class.multiple?(key)%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
<% if f.object.class.multiple? key %> | ||
<%= f.input key, as: :multi_value, input_html: { class: 'form-control' }, required: f.object.required?(key) %> | ||
<% if f.object.has_triple_powered_property?(key) %> | ||
<%= f.input key, | ||
as: :triple_powered_property, | ||
input_html: { class: 'form-control' }, | ||
required: f.object.required?(key), | ||
multi_value: f.object.class.multiple?(key) %> | ||
<% else %> | ||
<%= f.input key, required: f.object.required?(key) %> | ||
<% if f.object.class.multiple? key %> | ||
<%= f.input key, as: :multi_value, input_html: { class: 'form-control' }, required: f.object.required?(key) %> | ||
<% else %> | ||
<%= f.input key, required: f.object.required?(key) %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<%= f.input key, | ||
as: :triple_powered_property, | ||
input_html: { | ||
class: 'form-control', | ||
data: { 'autocomplete-url' => "/authorities/search/local/subjects", | ||
'autocomplete' => key } | ||
}, | ||
required: f.object.required?(key), | ||
multi_value: f.object.class.multiple?(key)%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SimpleForm.setup do |config| | ||
config.custom_inputs_namespaces << "ScholarsArchive::TriplePoweredProperties::Inputs" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Blacklight.onLoad(function() { | ||
var tpp = require('triple_powered_properties/control'); | ||
$('.triple_powered_property').each(function() { | ||
new tpp.TriplePoweredPropertyControl(this); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
lib/assets/javascripts/triple_powered_properties/button.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export class TriplePoweredPropertyButton { | ||
constructor(element) { | ||
this.$element = $(element); | ||
this.$list = this.$element.siblings(".list:first"); | ||
|
||
this.$element.on('click', (e) => { | ||
this.$list.toggle((e) => { | ||
if(this.$list.is(":visible")){ | ||
this.$element.text(this.$element.data('hide-all')); | ||
} else { | ||
this.$element.text(this.$element.data('show-all')); | ||
} | ||
}) | ||
}); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
lib/assets/javascripts/triple_powered_properties/control.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { TriplePoweredPropertyButton } from './button' | ||
|
||
export class TriplePoweredPropertyControl { | ||
constructor(element) { | ||
// Don't try to make sense of this URL regex, it will fry your brain. See: http://stackoverflow.com/a/9284473 | ||
this.urlPattern = new RegExp(/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/,'i'); | ||
this.$element = $(element); | ||
|
||
// Any of the triple powered property label lists that were rendered need to have their toggle button | ||
// wired up to provide the collapsable effect | ||
this.$element.find("span.toggle").each((i, el) => { | ||
new TriplePoweredPropertyButton(el); | ||
}); | ||
|
||
this.bindBlurHandler(); | ||
this.bindKeyupHandler(); | ||
this.bindManagedFieldAddHandler(); | ||
} | ||
|
||
// delegate blur events to the form-control that fired them, works with new fields that are added dynamically by way | ||
// of the "+ more" button managed by hydra-editor | ||
bindBlurHandler() { | ||
this.$element.on("blur", "input.form-control", (e) => { | ||
var $input = $(e.currentTarget); | ||
|
||
// disallow space characters in triple powered property fields, these will not be valid URIs | ||
$input.val($input.val().replace(' ', '')); | ||
|
||
// hide a previously displayed warning to revalidate the field for URLness | ||
var $warning = $(e.currentTarget).siblings('.has-warning'); | ||
$warning.text("").addClass("hidden"); | ||
|
||
// input value has text and doesn't pass the URL regex test | ||
if($input.val() !== '' && !this.urlPattern.test($(e.currentTarget).val())){ | ||
$warning.text("Invalid URL").removeClass("hidden"); | ||
} | ||
}); | ||
} | ||
|
||
// Remove the warning message when a user types into the "new" field managed by hydra-editor. This hides the warning | ||
// message "you cannot add a blank field" if it was displayed to the user previously. | ||
bindKeyupHandler() { | ||
this.$element.on("keyup", "input.form-control", (e) => { | ||
var $input = $(e.currentTarget); | ||
var $li = $input.parents("li"); | ||
|
||
// This warning message is injected by hydra-editor for mult_value fields wrapped in a UL list | ||
if($li){ | ||
if($li.find(".field-controls .btn.add").length > 0 && $input.val().length > 0) { | ||
$li.siblings(".has-warning").remove(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
// After the add button is clicked, hydra-editor clones the LI and clears its fields before injecting the new LI | ||
// to the end of the list.. Make sure this new LI has the URL validation warning field hidden since it is expecting | ||
// the user to enter a new one | ||
bindManagedFieldAddHandler(){ | ||
this.$element.on("click", ".btn.add", (e) => { | ||
$(e.delegateTarget).find("ul li:last .has-warning").addClass("hidden"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.triple_powered_property { | ||
position: relative; | ||
li.field-wrapper { | ||
span.field-controls { vertical-align: top; } | ||
div.message.has-warning { display: inline-block; margin-top: 5px; } | ||
} | ||
.triple_powered_labels { | ||
display: inline-block; | ||
padding: 0 5px; | ||
width: 100%; | ||
margin-top: 2px; | ||
span[itemprop='label'] { | ||
width: 100%; | ||
float: left; | ||
} | ||
span.error { color: #d9534f; } | ||
.toggle { | ||
font-size: 10px; | ||
cursor: pointer; | ||
font-weight: normal; | ||
background-color: #337ab7; | ||
} | ||
.toggle:hover{ background-color: #2e6da4; } | ||
ul.list { | ||
display: none; | ||
margin: 0; | ||
padding: 0; | ||
list-style: none; | ||
li { | ||
float: left; | ||
width: 100%; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module ScholarsArchive::TriplePoweredProperties | ||
module Errors | ||
|
||
## | ||
# Base error | ||
class TriplePoweredPropertyError < StandardError | ||
end | ||
|
||
## | ||
# Invalid URL | ||
class InvalidUrlError < TriplePoweredPropertyError | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
lib/scholars_archive/triple_powered_properties/forms/work_form.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module ScholarsArchive::TriplePoweredProperties::Forms | ||
class WorkForm < Sufia::Forms::WorkForm | ||
|
||
## | ||
# Checks the model of this form to evaluate if a property is triple powered | ||
# @param property [Symbol] the models property to be evaluated | ||
# @return [Boolean] true if the models triple_powered_properties includes the property | ||
def has_triple_powered_property?(property) | ||
self.model.triple_powered_properties.include?(property) | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
lib/scholars_archive/triple_powered_properties/has_url_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'uri' | ||
|
||
module ScholarsArchive::TriplePoweredProperties | ||
class HasUrlValidator < ActiveModel::Validator | ||
|
||
## | ||
# Evaluate each triple powered property value to ensure it is a valid URL | ||
# | ||
# @param record [ActiveModel] the model being validated | ||
def validate(record) | ||
return if record.triple_powered_properties.empty? | ||
|
||
record.triple_powered_properties.each do |prop| | ||
record[prop].each do |value| | ||
begin | ||
uri = URI.parse(value) | ||
rescue | ||
record.errors[prop] << "#{value} is not a URL" | ||
else | ||
record.errors[prop] << "#{value} is invalid" unless uri.kind_of?(URI::HTTP) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.