Commit b60af68a authored by Sean Hammond's avatar Sean Hammond

Put declarations and definitions together

Declare variables on the same line where they're first defined, instead
of at the top of the function.
parent 200c1bbf
...@@ -16,18 +16,13 @@ var events = require('../events'); ...@@ -16,18 +16,13 @@ var events = require('../events');
* *
*/ */
function validate(annotation) { function validate(annotation) {
var permissions;
var readPermissions;
var worldReadable = false;
var targets;
if (!angular.isObject(annotation)) { if (!angular.isObject(annotation)) {
return; return;
} }
permissions = annotation.permissions || {}; var permissions = annotation.permissions || {};
readPermissions = permissions.read || []; var readPermissions = permissions.read || [];
targets = annotation.target || []; var targets = annotation.target || [];
if (annotation.tags && annotation.tags.length) { if (annotation.tags && annotation.tags.length) {
return annotation.tags.length; return annotation.tags.length;
...@@ -37,6 +32,7 @@ function validate(annotation) { ...@@ -37,6 +32,7 @@ function validate(annotation) {
return annotation.text.length; return annotation.text.length;
} }
var worldReadable = false;
if (readPermissions.indexOf('group:__world__') !== -1) { if (readPermissions.indexOf('group:__world__') !== -1) {
worldReadable = true; worldReadable = true;
} }
...@@ -90,17 +86,6 @@ function AnnotationController( ...@@ -90,17 +86,6 @@ function AnnotationController(
var vm = this; var vm = this;
var highlight;
var isNewAnnotation;
/** The domain model, contains the currently saved version of the annotation
* from the server. */
var model;
var updateDomainModel;
var updateDraft;
var updateTimestamp;
/** The view model, contains user changes to the annotation that haven't /** The view model, contains user changes to the annotation that haven't
* been saved to the server yet. */ * been saved to the server yet. */
vm.annotation = {}; vm.annotation = {};
...@@ -112,7 +97,9 @@ function AnnotationController( ...@@ -112,7 +97,9 @@ function AnnotationController(
vm.preview = 'no'; vm.preview = 'no';
vm.timestamp = null; vm.timestamp = null;
model = $scope.annotationGet(); /** The domain model, contains the currently saved version of the annotation
* from the server. */
var model = $scope.annotationGet();
if (!model.user) { if (!model.user) {
model.user = session.state.userid; model.user = session.state.userid;
} }
...@@ -125,7 +112,7 @@ function AnnotationController( ...@@ -125,7 +112,7 @@ function AnnotationController(
// Set the permissions of new annotations. // Set the permissions of new annotations.
model.permissions = model.permissions || permissions['default'](model.group); model.permissions = model.permissions || permissions['default'](model.group);
highlight = model.$highlight; var highlight = model.$highlight;
/** /**
* @ngdoc method * @ngdoc method
...@@ -253,10 +240,9 @@ function AnnotationController( ...@@ -253,10 +240,9 @@ function AnnotationController(
*/ */
vm['delete'] = function() { vm['delete'] = function() {
return $timeout(function() { // Don't use confirm inside the digest cycle. return $timeout(function() { // Don't use confirm inside the digest cycle.
var onRejected;
var msg = 'Are you sure you want to delete this annotation?'; var msg = 'Are you sure you want to delete this annotation?';
if ($window.confirm(msg)) { if ($window.confirm(msg)) {
onRejected = function(reason) { var onRejected = function(reason) {
flash.error( flash.error(
errorMessage(reason), 'Deleting annotation failed'); errorMessage(reason), 'Deleting annotation failed');
}; };
...@@ -312,20 +298,20 @@ function AnnotationController( ...@@ -312,20 +298,20 @@ function AnnotationController(
* Update the given annotation domain model object with the data from the * Update the given annotation domain model object with the data from the
* given annotation view model object. * given annotation view model object.
*/ */
updateDomainModel = function(domainModel, viewModel) { function updateDomainModel(domainModel, viewModel) {
var i; var i;
var tagTexts = []; var tagTexts = [];
for (i = 0; i < viewModel.tags.length; i++) { for (i = 0; i < viewModel.tags.length; i++) {
tagTexts[tagTexts.length] = viewModel.tags[i].text; tagTexts[tagTexts.length] = viewModel.tags[i].text;
} }
angular.extend(domainModel, viewModel, {tags: tagTexts}); angular.extend(domainModel, viewModel, {tags: tagTexts});
}; }
/** /**
* Create or update the existing draft for this annotation using * Create or update the existing draft for this annotation using
* the text and tags from the domain model in `draft`. * the text and tags from the domain model in `draft`.
*/ */
updateDraft = function(draft) { function updateDraft(draft) {
// Drafts only preserve the text, tags and permissions of the annotation // Drafts only preserve the text, tags and permissions of the annotation
// (i.e. only the bits that the user can edit), changes to other // (i.e. only the bits that the user can edit), changes to other
// properties are not preserved. // properties are not preserved.
...@@ -340,7 +326,7 @@ function AnnotationController( ...@@ -340,7 +326,7 @@ function AnnotationController(
changes.permissions = draft.permissions; changes.permissions = draft.permissions;
} }
drafts.update(model, changes); drafts.update(model, changes);
}; }
/** /**
* @ngdoc method * @ngdoc method
...@@ -348,11 +334,6 @@ function AnnotationController( ...@@ -348,11 +334,6 @@ function AnnotationController(
* @description Saves any edits and returns to the viewer. * @description Saves any edits and returns to the viewer.
*/ */
vm.save = function() { vm.save = function() {
var newTags;
var onFulfilled;
var onRejected;
var updatedModel;
if (!model.user) { if (!model.user) {
return flash.info('Please sign in to save your annotations.'); return flash.info('Please sign in to save your annotations.');
} }
...@@ -362,7 +343,7 @@ function AnnotationController( ...@@ -362,7 +343,7 @@ function AnnotationController(
} }
// Update stored tags with the new tags of this annotation. // Update stored tags with the new tags of this annotation.
newTags = vm.annotation.tags.filter(function(tag) { var newTags = vm.annotation.tags.filter(function(tag) {
var tags = model.tags || []; var tags = model.tags || [];
return (tags.indexOf(tag.text) === -1); return (tags.indexOf(tag.text) === -1);
}); });
...@@ -371,18 +352,18 @@ function AnnotationController( ...@@ -371,18 +352,18 @@ function AnnotationController(
switch (vm.action) { switch (vm.action) {
case 'create': case 'create':
updateDomainModel(model, vm.annotation); updateDomainModel(model, vm.annotation);
onFulfilled = function() { var onFulfilled = function() {
$rootScope.$emit('annotationCreated', model); $rootScope.$emit('annotationCreated', model);
vm.view(); vm.view();
}; };
onRejected = function(reason) { var onRejected = function(reason) {
flash.error( flash.error(
errorMessage(reason), 'Saving annotation failed'); errorMessage(reason), 'Saving annotation failed');
}; };
return model.$create().then(onFulfilled, onRejected); return model.$create().then(onFulfilled, onRejected);
case 'edit': case 'edit':
updatedModel = angular.copy(model); var updatedModel = angular.copy(model);
updateDomainModel(updatedModel, vm.annotation); updateDomainModel(updatedModel, vm.annotation);
onFulfilled = function() { onFulfilled = function() {
angular.copy(updatedModel, model); angular.copy(updatedModel, model);
...@@ -408,8 +389,6 @@ function AnnotationController( ...@@ -408,8 +389,6 @@ function AnnotationController(
vm.reply = function() { vm.reply = function() {
var id = model.id; var id = model.id;
var references = model.references || []; var references = model.references || [];
var reply;
var uri = model.uri;
if (typeof references === 'string') { if (typeof references === 'string') {
references = [references]; references = [references];
...@@ -417,9 +396,9 @@ function AnnotationController( ...@@ -417,9 +396,9 @@ function AnnotationController(
references = references.concat(id); references = references.concat(id);
reply = annotationMapper.createAnnotation({ var reply = annotationMapper.createAnnotation({
references: references, references: references,
uri: uri uri: model.uri
}); });
reply.group = model.group; reply.group = model.group;
...@@ -438,13 +417,7 @@ function AnnotationController( ...@@ -438,13 +417,7 @@ function AnnotationController(
* @description Called to update the view when the model changes. * @description Called to update the view when the model changes.
*/ */
vm.render = function() { vm.render = function() {
var documentTitle;
var domain;
var draft = drafts.get(model); var draft = drafts.get(model);
var i;
var link;
var tagsAsObjects;
var uri = model.uri;
// Extend the view model with a copy of the domain model. // Extend the view model with a copy of the domain model.
// Note that copy is used so that deep properties aren't shared. // Note that copy is used so that deep properties aren't shared.
...@@ -460,11 +433,13 @@ function AnnotationController( ...@@ -460,11 +433,13 @@ function AnnotationController(
'/a/' + vm.annotation.id, vm.baseURI).href; '/a/' + vm.annotation.id, vm.baseURI).href;
// Extract the document metadata. // Extract the document metadata.
domain = new URL(uri).hostname; var uri = model.uri;
var domain = new URL(uri).hostname;
if (model.document) { if (model.document) {
if (uri.indexOf('urn') === 0) { if (uri.indexOf('urn') === 0) {
var i;
for (i = 0; i < model.document.link.length; i++) { for (i = 0; i < model.document.link.length; i++) {
link = model.document.link[i]; var link = model.document.link[i];
if (!(link.href.indexOf('urn'))) { if (!(link.href.indexOf('urn'))) {
continue; continue;
} }
...@@ -473,7 +448,7 @@ function AnnotationController( ...@@ -473,7 +448,7 @@ function AnnotationController(
} }
} }
documentTitle = Array.isArray( var documentTitle = Array.isArray(
model.document.title) ? model.document.title[0] : model.document.title; model.document.title) ? model.document.title[0] : model.document.title;
vm.document = { vm.document = {
...@@ -499,10 +474,9 @@ function AnnotationController( ...@@ -499,10 +474,9 @@ function AnnotationController(
}); });
}; };
updateTimestamp = function(repeat) { // We use `var foo = function() {...}` here instead of `function foo() {...}`
var fuzzyUpdate; // because updateTimestamp gets redefined later on.
var nextUpdate; var updateTimestamp = function(repeat) {
repeat = repeat || false; repeat = repeat || false;
// New (not yet saved to the server) annotations don't have any .updated // New (not yet saved to the server) annotations don't have any .updated
...@@ -517,8 +491,8 @@ function AnnotationController( ...@@ -517,8 +491,8 @@ function AnnotationController(
return; return;
} }
fuzzyUpdate = time.nextFuzzyUpdate(model.updated); var fuzzyUpdate = time.nextFuzzyUpdate(model.updated);
nextUpdate = (1000 * fuzzyUpdate) + 500; var nextUpdate = (1000 * fuzzyUpdate) + 500;
$timeout(function() { $timeout(function() {
updateTimestamp(true); updateTimestamp(true);
...@@ -574,7 +548,7 @@ function AnnotationController( ...@@ -574,7 +548,7 @@ function AnnotationController(
// If this is a new annotation or we have unsaved changes, // If this is a new annotation or we have unsaved changes,
// then start editing immediately. // then start editing immediately.
isNewAnnotation = !(model.id || (vm.isHighlight() && highlight)); var isNewAnnotation = !(model.id || (vm.isHighlight() && highlight));
if (isNewAnnotation || drafts.get(model)) { if (isNewAnnotation || drafts.get(model)) {
vm.edit(); vm.edit();
} }
...@@ -583,10 +557,6 @@ function AnnotationController( ...@@ -583,10 +557,6 @@ function AnnotationController(
// the drafts service. They will be restored when this annotation is // the drafts service. They will be restored when this annotation is
// next loaded. // next loaded.
$scope.$on(events.GROUP_FOCUSED, function() { $scope.$on(events.GROUP_FOCUSED, function() {
var draftDomainModel;
var isShared;
var newGroup;
if (!vm.editing) { if (!vm.editing) {
return; return;
} }
...@@ -594,8 +564,8 @@ function AnnotationController( ...@@ -594,8 +564,8 @@ function AnnotationController(
// Move any new annotations to the currently focused group when // Move any new annotations to the currently focused group when
// switching groups. See GH #2689 for context. // switching groups. See GH #2689 for context.
if (!model.id) { if (!model.id) {
newGroup = groups.focused().id; var newGroup = groups.focused().id;
isShared = permissions.isShared( var isShared = permissions.isShared(
vm.annotation.permissions, vm.annotation.group); vm.annotation.permissions, vm.annotation.group);
if (isShared) { if (isShared) {
model.permissions = permissions.shared(newGroup); model.permissions = permissions.shared(newGroup);
...@@ -608,7 +578,7 @@ function AnnotationController( ...@@ -608,7 +578,7 @@ function AnnotationController(
// if we have a draft, update it, otherwise (eg. when the user signs out) // if we have a draft, update it, otherwise (eg. when the user signs out)
// do not create a new one. // do not create a new one.
if (drafts.get(model)) { if (drafts.get(model)) {
draftDomainModel = {}; var draftDomainModel = {};
updateDomainModel(draftDomainModel, vm.annotation); updateDomainModel(draftDomainModel, vm.annotation);
updateDraft(draftDomainModel); updateDraft(draftDomainModel);
} }
...@@ -656,8 +626,7 @@ function annotation($document, features) { ...@@ -656,8 +626,7 @@ function annotation($document, features) {
scope.feature = features.flagEnabled; scope.feature = features.flagEnabled;
scope.share = function(event) { scope.share = function(event) {
var $container; var $container = angular.element(event.currentTarget).parent();
$container = angular.element(event.currentTarget).parent();
$container.addClass('open').find('input').focus().select(); $container.addClass('open').find('input').focus().select();
// We have to stop propagation here otherwise this click event will // We have to stop propagation here otherwise this click event will
......
...@@ -218,8 +218,6 @@ describe('annotation', function() { ...@@ -218,8 +218,6 @@ describe('annotation', function() {
}); });
describe('#reply', function() { describe('#reply', function() {
var container;
beforeEach(function() { beforeEach(function() {
createDirective(); createDirective();
annotation.permissions = { annotation.permissions = {
...@@ -231,9 +229,8 @@ describe('annotation', function() { ...@@ -231,9 +229,8 @@ describe('annotation', function() {
}); });
it('creates a new reply with the proper uri and references', function() { it('creates a new reply with the proper uri and references', function() {
var match;
controller.reply(); controller.reply();
match = sinon.match({ var match = sinon.match({
references: [annotation.id], references: [annotation.id],
uri: annotation.uri uri: annotation.uri
}); });
...@@ -241,8 +238,7 @@ describe('annotation', function() { ...@@ -241,8 +238,7 @@ describe('annotation', function() {
}); });
it('makes the annotation shared if the parent is shared', function() { it('makes the annotation shared if the parent is shared', function() {
var reply; var reply = {};
reply = {};
fakeAnnotationMapper.createAnnotation.returns(reply); fakeAnnotationMapper.createAnnotation.returns(reply);
fakePermissions.isShared.returns(true); fakePermissions.isShared.returns(true);
controller.reply(); controller.reply();
...@@ -252,12 +248,11 @@ describe('annotation', function() { ...@@ -252,12 +248,11 @@ describe('annotation', function() {
}); });
it('makes the annotation shared if the parent is shared', function() { it('makes the annotation shared if the parent is shared', function() {
var reply;
$scope.annotation.group = 'my group'; $scope.annotation.group = 'my group';
$scope.annotation.permissions = { $scope.annotation.permissions = {
read: ['my group'] read: ['my group']
}; };
reply = {}; var reply = {};
fakeAnnotationMapper.createAnnotation.returns(reply); fakeAnnotationMapper.createAnnotation.returns(reply);
fakePermissions.isShared = function(permissions, group) { fakePermissions.isShared = function(permissions, group) {
return permissions.read.indexOf(group) !== -1; return permissions.read.indexOf(group) !== -1;
...@@ -274,8 +269,7 @@ describe('annotation', function() { ...@@ -274,8 +269,7 @@ describe('annotation', function() {
it( it(
'does not add the world readable principal if the parent is private', 'does not add the world readable principal if the parent is private',
function() { function() {
var reply; var reply = {};
reply = {};
fakeAnnotationMapper.createAnnotation.returns(reply); fakeAnnotationMapper.createAnnotation.returns(reply);
fakePermissions.isShared.returns(false); fakePermissions.isShared.returns(false);
controller.reply(); controller.reply();
...@@ -286,9 +280,8 @@ describe('annotation', function() { ...@@ -286,9 +280,8 @@ describe('annotation', function() {
); );
it('sets the reply\'s group to be the same as its parent\'s', function() { it('sets the reply\'s group to be the same as its parent\'s', function() {
var reply;
$scope.annotation.group = 'my group'; $scope.annotation.group = 'my group';
reply = {}; var reply = {};
fakeAnnotationMapper.createAnnotation.returns(reply); fakeAnnotationMapper.createAnnotation.returns(reply);
controller.reply(); controller.reply();
assert.equal(reply.group, $scope.annotation.group); assert.equal(reply.group, $scope.annotation.group);
...@@ -914,10 +907,6 @@ describe('annotation', function() { ...@@ -914,10 +907,6 @@ describe('annotation', function() {
}); });
describe('AnnotationController', function() { describe('AnnotationController', function() {
var createAnnotationDirective;
var getCompileService;
var getRootScope;
before(function() { before(function() {
angular.module('h', []) angular.module('h', [])
.directive('annotation', require('../annotation').directive); .directive('annotation', require('../annotation').directive);
...@@ -928,30 +917,27 @@ describe('AnnotationController', function() { ...@@ -928,30 +917,27 @@ describe('AnnotationController', function() {
beforeEach(module('h.templates')); beforeEach(module('h.templates'));
/** Return Angular's $compile service. */ /** Return Angular's $compile service. */
getCompileService = function() { function getCompileService() {
var $compile; var $compile;
$compile = null;
inject(function(_$compile_) { inject(function(_$compile_) {
$compile = _$compile_; $compile = _$compile_;
}); });
return $compile; return $compile;
}; }
/** Return Angular's $rootScope. */ /** Return Angular's $rootScope. */
getRootScope = function() { function getRootScope() {
var $rootScope; var $rootScope;
$rootScope = null;
inject(function(_$rootScope_) { inject(function(_$rootScope_) {
$rootScope = _$rootScope_; $rootScope = _$rootScope_;
}); });
return $rootScope; return $rootScope;
}; }
/** /**
Return an annotation directive instance and stub services etc. Return an annotation directive instance and stub services etc.
*/ */
createAnnotationDirective = function(args) { function createAnnotationDirective(args) {
var compiledElement;
var session = args.session || { var session = args.session || {
state: { state: {
userid: 'acct:fred@hypothes.is' userid: 'acct:fred@hypothes.is'
...@@ -1050,7 +1036,7 @@ describe('AnnotationController', function() { ...@@ -1050,7 +1036,7 @@ describe('AnnotationController', function() {
$provide.value('localStorage', locals.localStorage); $provide.value('localStorage', locals.localStorage);
}); });
locals.element = angular.element('<div annotation="annotation">'); locals.element = angular.element('<div annotation="annotation">');
compiledElement = getCompileService()(locals.element); var compiledElement = getCompileService()(locals.element);
locals.$rootScope = getRootScope(); locals.$rootScope = getRootScope();
locals.parentScope = locals.$rootScope.$new(); locals.parentScope = locals.$rootScope.$new();
locals.parentScope.annotation = args.annotation || {}; locals.parentScope.annotation = args.annotation || {};
...@@ -1059,7 +1045,7 @@ describe('AnnotationController', function() { ...@@ -1059,7 +1045,7 @@ describe('AnnotationController', function() {
locals.controller = locals.element.controller('annotation'); locals.controller = locals.element.controller('annotation');
locals.isolateScope = locals.element.isolateScope(); locals.isolateScope = locals.element.isolateScope();
return locals; return locals;
}; }
describe('createAnnotationDirective', function() { describe('createAnnotationDirective', function() {
it('creates the directive without crashing', function() { it('creates the directive without crashing', function() {
...@@ -1281,7 +1267,6 @@ describe('validate()', function() { ...@@ -1281,7 +1267,6 @@ describe('validate()', function() {
var validate = require('../annotation').validate; var validate = require('../annotation').validate;
it('returns undefined if value is not an object', function() { it('returns undefined if value is not an object', function() {
var value;
var i; var i;
var values = [2, 'foo', true, null]; var values = [2, 'foo', true, null];
for (i = 0; i < values.length; i++) { for (i = 0; i < values.length; i++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment