Skip to content

Commit

Permalink
Merge pull request #1 from hypothesis/direct-document-link-from-stream
Browse files Browse the repository at this point in the history
Show direct links on stream page when available (FIRST!)
  • Loading branch information
robertknight authored Jun 30, 2016
2 parents 91b9a22 + c4dea46 commit 8a5ab37
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 237 deletions.
64 changes: 60 additions & 4 deletions h/static/scripts/annotation-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* uri, domain and title.
*
*/
function extractDocumentMetadata(annotation) {
function documentMetadata(annotation) {
var uri = annotation.uri;
var domain = new URL(uri).hostname;
var title = domain;
Expand All @@ -21,8 +21,8 @@ function extractDocumentMetadata(annotation) {
title = annotation.document.title[0];
}

if (title.length > 30) {
title = title.slice(0, 30) + '…';
if (domain === 'localhost') {
domain = '';
}

return {
Expand All @@ -32,6 +32,61 @@ function extractDocumentMetadata(annotation) {
};
}

/**
* Return the domain and title of an annotation for display on an annotation
* card.
*/
function domainAndTitle(annotation) {
return {
domain: domainTextFromAnnotation(annotation),
titleText: titleTextFromAnnotation(annotation),
titleLink: titleLinkFromAnnotation(annotation),
};
}

function titleLinkFromAnnotation(annotation) {
var titleLink = annotation.uri;

if (titleLink && !(titleLink.indexOf('http://') === 0 || titleLink.indexOf('https://') === 0)) {
// We only link to http(s) URLs.
titleLink = null;
}

if (annotation.links && annotation.links.incontext) {
titleLink = annotation.links.incontext;
}

return titleLink;
}

function domainTextFromAnnotation(annotation) {
var document = documentMetadata(annotation);

var domainText = '';
if (document.uri && document.uri.indexOf('file://') === 0 && document.title) {
var parts = document.uri.split('/');
var filename = parts[parts.length - 1];
if (filename) {
domainText = filename;
}
} else if (document.domain && document.domain !== document.title) {
domainText = document.domain;
}

return domainText;
}

function titleTextFromAnnotation(annotation) {
var document = documentMetadata(annotation);

var titleText = document.title;
if (titleText.length > 30) {
titleText = titleText.slice(0, 30) + '…';
}

return titleText;
}

/** Return `true` if the given annotation is a reply, `false` otherwise. */
function isReply(annotation) {
return (annotation.references || []).length > 0;
Expand Down Expand Up @@ -78,7 +133,8 @@ function location(annotation) {
}

module.exports = {
extractDocumentMetadata: extractDocumentMetadata,
documentMetadata: documentMetadata,
domainAndTitle: domainAndTitle,
isAnnotation: isAnnotation,
isNew: isNew,
isPageNote: isPageNote,
Expand Down
7 changes: 1 addition & 6 deletions h/static/scripts/directive/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
var angular = require('angular');

var annotationMetadata = require('../annotation-metadata');
var documentDomain = require('../filter/document-domain');
var documentTitle = require('../filter/document-title');
var events = require('../events');
var persona = require('../filter/persona');

var isNew = annotationMetadata.isNew;
var isReply = annotationMetadata.isReply;
var extractDocumentMetadata = annotationMetadata.extractDocumentMetadata;

/** Return a human-readable error message for the given server error.
*
Expand Down Expand Up @@ -115,9 +112,7 @@ function updateViewModel($scope, domainModel,
vm.isPrivate = permissions.isPrivate(
domainModel.permissions, domainModel.user);

var documentMetadata = extractDocumentMetadata(domainModel);
vm.documentTitle = documentTitle(documentMetadata);
vm.documentDomain = documentDomain(documentMetadata);
vm.documentMeta = annotationMetadata.domainAndTitle(domainModel);
}

/**
Expand Down
32 changes: 0 additions & 32 deletions h/static/scripts/filter/document-domain.js

This file was deleted.

30 changes: 0 additions & 30 deletions h/static/scripts/filter/document-title.js

This file was deleted.

71 changes: 0 additions & 71 deletions h/static/scripts/filter/test/document-domain-test.js

This file was deleted.

75 changes: 0 additions & 75 deletions h/static/scripts/filter/test/document-title-test.js

This file was deleted.

Loading

0 comments on commit 8a5ab37

Please sign in to comment.