Last active
August 29, 2015 14:27
-
-
Save judell/2ea4cb405e673bc0026a to your computer and use it in GitHub Desktop.
SITC Prototype: Phase 1
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
/* | |
npm install dom-anchor-text-quote | |
npm install dom-seek | |
npm install jquery | |
browserify sitc.js -o sitc-bundle.js | |
*/ | |
/* | |
<script src="./sitc-bundle.js"></script> | |
<script> | |
console.log('getting annotations'); | |
get_annotations(); | |
console.log('got annotations'); | |
</script> | |
*/ | |
function attach_annotation(category, text, exact, prefix) { | |
var TextQuoteAnchor = require ('dom-anchor-text-quote') | |
var $ = require('jQuery'); | |
var root = window.$('body')[0]; | |
console.log(root); | |
var tqa = new TextQuoteAnchor(root, exact, {'prefix':prefix}); | |
var range = tqa.toRange(); | |
var container = range.startContainer; | |
var text_node = container.splitText(range.startOffset); | |
text_node.splitText(exact.length) | |
$(text_node).wrap('<a class="annotate ' + category + '" href="#" title="' + text + '" data-hasqtip="true"></a>') | |
} | |
function get_annotations() { | |
var $ = require('jQuery'); | |
url = 'https://hypothes.is/api/search?uri=' + location.href; | |
$.ajax({ | |
url: url, | |
success: attach_annotations | |
}); | |
} | |
function get_category(tag) { | |
if ( tag == 'Glossary' ) | |
return 'llens-6'; | |
else | |
return 'undefined'; | |
} | |
function attach_annotations(data) { | |
var rows = data['rows'] | |
for ( var i=0; i < rows.length; i++ ) { | |
var row = rows[i]; | |
var selectors = row['target'][0]['selector']; | |
var selector = null; | |
for (var j=0; j<selectors.length; j++) { | |
if ( selectors[j].hasOwnProperty('exact') ) | |
selector = selectors[j]; | |
} | |
if ( selector == null ) | |
continue; | |
var exact = selector['exact']; | |
var prefix = selector['prefix']; | |
var text = row['text']; | |
var tag = row['tags'][0]; | |
var category = get_category(tag); | |
attach_annotation(category, text, exact, prefix); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment