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