Commit bf4a07a1 authored by Sean Hammond's avatar Sean Hammond

Remove vm.annotation -> vm.form

Clarify what this object actually is: the read-write variables for the
templates to write via ng-model.
parent d1bd2b9a
...@@ -5,9 +5,9 @@ var events = require('../events'); ...@@ -5,9 +5,9 @@ var events = require('../events');
/** Return a domainModel tags array from the given vm tags array. /** Return a domainModel tags array from the given vm tags array.
* *
* domainModel.tags and vm.annotation.tags use different formats. This * domainModel.tags and vm.form.tags use different formats. This
* function returns a domainModel.tags-formatted copy of the given * function returns a domainModel.tags-formatted copy of the given
* vm.annotation.tags-formatted array. * vm.form.tags-formatted array.
* *
*/ */
function domainModelTagsFromViewModelTags(viewModelTags) { function domainModelTagsFromViewModelTags(viewModelTags) {
...@@ -124,8 +124,8 @@ function restoreFromDrafts(drafts, permissions, domainModel, vm) { ...@@ -124,8 +124,8 @@ function restoreFromDrafts(drafts, permissions, domainModel, vm) {
} else { } else {
domainModel.permissions = permissions.shared(domainModel.group); domainModel.permissions = permissions.shared(domainModel.group);
} }
vm.annotation.tags = viewModelTagsFromDomainModelTags(draft.tags); vm.form.tags = viewModelTagsFromDomainModelTags(draft.tags);
vm.annotation.text = draft.text; vm.form.text = draft.text;
} }
} }
...@@ -147,7 +147,7 @@ function restoreFromDrafts(drafts, permissions, domainModel, vm) { ...@@ -147,7 +147,7 @@ function restoreFromDrafts(drafts, permissions, domainModel, vm) {
function saveToDrafts(drafts, domainModel, vm) { function saveToDrafts(drafts, domainModel, vm) {
drafts.update( drafts.update(
domainModel, vm.isPrivate(), domainModel, vm.isPrivate(),
domainModelTagsFromViewModelTags(vm.annotation.tags), vm.annotation.text); domainModelTagsFromViewModelTags(vm.form.tags), vm.form.text);
} }
/** Update domainModel from vm. /** Update domainModel from vm.
...@@ -164,21 +164,21 @@ function saveToDrafts(drafts, domainModel, vm) { ...@@ -164,21 +164,21 @@ function saveToDrafts(drafts, domainModel, vm) {
* *
*/ */
function updateDomainModel(domainModel, vm) { function updateDomainModel(domainModel, vm) {
domainModel.text = vm.annotation.text; domainModel.text = vm.form.text;
domainModel.tags = domainModelTagsFromViewModelTags(vm.annotation.tags); domainModel.tags = domainModelTagsFromViewModelTags(vm.form.tags);
} }
/** Update the view model from the domain model changes. */ /** Update the view model from the domain model changes. */
function updateViewModel(domainModel, vm, permissions) { function updateViewModel(domainModel, vm, permissions) {
// 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.
vm.annotation = { vm.form = {
text: domainModel.text, text: domainModel.text,
tags: viewModelTagsFromDomainModelTags(domainModel.tags), tags: viewModelTagsFromDomainModelTags(domainModel.tags),
}; };
vm.annotationURI = new URL( vm.annotationURI = new URL(
'/a/' + vm.annotation.id, vm.baseURI).href; '/a/' + domainModel.id, vm.baseURI).href;
vm.document = extractDocumentMetadata(domainModel); vm.document = extractDocumentMetadata(domainModel);
} }
...@@ -222,8 +222,8 @@ function validate(domainModel) { ...@@ -222,8 +222,8 @@ function validate(domainModel) {
/** Return a vm tags array from the given domainModel tags array. /** Return a vm tags array from the given domainModel tags array.
* *
* domainModel.tags and vm.annotation.tags use different formats. This * domainModel.tags and vm.form.tags use different formats. This
* function returns a vm.annotation.tags-formatted copy of the given * function returns a vm.form.tags-formatted copy of the given
* domainModel.tags-formatted array. * domainModel.tags-formatted array.
* *
*/ */
...@@ -268,9 +268,12 @@ function AnnotationController( ...@@ -268,9 +268,12 @@ function AnnotationController(
/** The currently active action - 'view', 'create' or 'edit'. */ /** The currently active action - 'view', 'create' or 'edit'. */
vm.action = 'view'; vm.action = 'view';
/** The view model, contains user changes to the annotation that haven't /** vm.form is the read-write part of vm for the templates: it contains
* been saved to the server yet. */ * the variables that the templates will write changes to via ng-model. */
vm.annotation = {}; vm.form = {};
// The remaining properties on vm are read-only properties for the
// templates.
/** The baseURI for the website, e.g. 'https://hypothes.is/'. */ /** The baseURI for the website, e.g. 'https://hypothes.is/'. */
vm.baseURI = $document.prop('baseURI'); vm.baseURI = $document.prop('baseURI');
...@@ -518,8 +521,8 @@ function AnnotationController( ...@@ -518,8 +521,8 @@ function AnnotationController(
* otherwise. * otherwise.
*/ */
vm.hasContent = function() { vm.hasContent = function() {
var textLength = (vm.annotation.text || '').length; var textLength = (vm.form.text || '').length;
var tagsLength = (vm.annotation.tags || []).length; var tagsLength = (vm.form.tags || []).length;
return (textLength > 0 || tagsLength > 0); return (textLength > 0 || tagsLength > 0);
}; };
...@@ -645,7 +648,7 @@ function AnnotationController( ...@@ -645,7 +648,7 @@ function AnnotationController(
} }
// Update stored tags with the new tags of this annotation. // Update stored tags with the new tags of this annotation.
var newTags = vm.annotation.tags.filter(function(tag) { var newTags = vm.form.tags.filter(function(tag) {
var tags = domainModel.tags || []; var tags = domainModel.tags || [];
return tags.indexOf(tag.text) === -1; return tags.indexOf(tag.text) === -1;
}); });
......
...@@ -289,25 +289,25 @@ describe('annotation.js', function() { ...@@ -289,25 +289,25 @@ describe('annotation.js', function() {
it('copies text from viewModel into domainModel', function() { it('copies text from viewModel into domainModel', function() {
var domainModel = {}; var domainModel = {};
var viewModel = {annotation: {text: 'bar', tags: []}}; var viewModel = {form: {text: 'bar', tags: []}};
updateDomainModel(domainModel, viewModel); updateDomainModel(domainModel, viewModel);
assert.equal(domainModel.text, viewModel.annotation.text); assert.equal(domainModel.text, viewModel.form.text);
}); });
it('overwrites text in domainModel', function() { it('overwrites text in domainModel', function() {
var domainModel = {text: 'foo'}; var domainModel = {text: 'foo'};
var viewModel = {annotation: {text: 'bar', tags: []}}; var viewModel = {form: {text: 'bar', tags: []}};
updateDomainModel(domainModel, viewModel); updateDomainModel(domainModel, viewModel);
assert.equal(domainModel.text, viewModel.annotation.text); assert.equal(domainModel.text, viewModel.form.text);
}); });
it('doesn\'t touch other properties in domainModel', function() { it('doesn\'t touch other properties in domainModel', function() {
var domainModel = {foo: 'foo', bar: 'bar'}; var domainModel = {foo: 'foo', bar: 'bar'};
var viewModel = {annotation: {foo: 'FOO', tags: []}}; var viewModel = {form: {foo: 'FOO', tags: []}};
updateDomainModel(domainModel, viewModel); updateDomainModel(domainModel, viewModel);
...@@ -320,7 +320,7 @@ describe('annotation.js', function() { ...@@ -320,7 +320,7 @@ describe('annotation.js', function() {
it('copies tag texts from viewModel into domainModel', function() { it('copies tag texts from viewModel into domainModel', function() {
var domainModel = {}; var domainModel = {};
var viewModel = { var viewModel = {
annotation: { form: {
tags: [ tags: [
{text: 'foo'}, {text: 'foo'},
{text: 'bar'} {text: 'bar'}
...@@ -1079,17 +1079,17 @@ describe('annotation.js', function() { ...@@ -1079,17 +1079,17 @@ describe('annotation.js', function() {
describe('#hasContent', function() { describe('#hasContent', function() {
it('returns false if the annotation has no tags or text', function() { it('returns false if the annotation has no tags or text', function() {
var controller = createDirective().controller; var controller = createDirective().controller;
controller.annotation.text = ''; controller.form.text = '';
controller.annotation.tags = []; controller.form.tags = [];
assert.ok(!controller.hasContent()); assert.ok(!controller.hasContent());
}); });
it('returns true if the annotation has tags or text', function() { it('returns true if the annotation has tags or text', function() {
var controller = createDirective().controller; var controller = createDirective().controller;
controller.annotation.text = 'bar'; controller.form.text = 'bar';
assert.ok(controller.hasContent()); assert.ok(controller.hasContent());
controller.annotation.text = ''; controller.form.text = '';
controller.annotation.tags = [ controller.form.tags = [
{ {
text: 'foo' text: 'foo'
} }
...@@ -1100,8 +1100,10 @@ describe('annotation.js', function() { ...@@ -1100,8 +1100,10 @@ describe('annotation.js', function() {
describe('#hasQuotes', function() { describe('#hasQuotes', function() {
it('returns false if the annotation has no quotes', function() { it('returns false if the annotation has no quotes', function() {
var controller = createDirective().controller; var annotation = defaultAnnotation();
controller.annotation.target = [{}]; annotation.target = [{}];
var controller = createDirective(annotation).controller;
assert.isFalse(controller.hasQuotes()); assert.isFalse(controller.hasQuotes());
}); });
...@@ -1414,12 +1416,12 @@ describe('annotation.js', function() { ...@@ -1414,12 +1416,12 @@ describe('annotation.js', function() {
text: 'unsaved-text' text: 'unsaved-text'
}); });
var controller = createDirective().controller; var controller = createDirective().controller;
assert.deepEqual(controller.annotation.tags, [ assert.deepEqual(controller.form.tags, [
{ {
text: 'unsaved-tag' text: 'unsaved-tag'
} }
]); ]);
assert.equal(controller.annotation.text, 'unsaved-text'); assert.equal(controller.form.text, 'unsaved-text');
}); });
it('removes the draft when changes are discarded', function() { it('removes the draft when changes are discarded', function() {
...@@ -1444,8 +1446,8 @@ describe('annotation.js', function() { ...@@ -1444,8 +1446,8 @@ describe('annotation.js', function() {
it('updates the current draft', function() { it('updates the current draft', function() {
var parts = createDirective(); var parts = createDirective();
parts.controller.edit(); parts.controller.edit();
parts.controller.annotation.text = 'unsaved-text'; parts.controller.form.text = 'unsaved-text';
parts.controller.annotation.tags = []; parts.controller.form.tags = [];
fakeDrafts.get = sinon.stub().returns({ fakeDrafts.get = sinon.stub().returns({
text: 'old-draft' text: 'old-draft'
}); });
...@@ -1788,20 +1790,20 @@ describe('annotation.js', function() { ...@@ -1788,20 +1790,20 @@ describe('annotation.js', function() {
} }
} }
}).controller; }).controller;
var originalText = controller.annotation.text; var originalText = controller.form.text;
// Simulate the user clicking the Edit button on the annotation. // Simulate the user clicking the Edit button on the annotation.
controller.edit(); controller.edit();
// Simulate the user typing some text into the annotation editor textarea. // Simulate the user typing some text into the annotation editor textarea.
controller.annotation.text = 'changed by test code'; controller.form.text = 'changed by test code';
// Simulate the user hitting the Save button and wait for the // Simulate the user hitting the Save button and wait for the
// (unsuccessful) response from the server. // (unsuccessful) response from the server.
controller.save(); controller.save();
// At this point the annotation editor controls are still open, and the // At this point the annotation editor controls are still open, and the
// annotation's text is still the modified (unsaved) text. // annotation's text is still the modified (unsaved) text.
assert(controller.annotation.text === 'changed by test code'); assert(controller.form.text === 'changed by test code');
// Simulate the user clicking the Cancel button. // Simulate the user clicking the Cancel button.
controller.revert(); controller.revert();
assert(controller.annotation.text === originalText); assert(controller.form.text === originalText);
}); });
// test that editing reverting changes to an annotation with // test that editing reverting changes to an annotation with
...@@ -1815,9 +1817,9 @@ describe('annotation.js', function() { ...@@ -1815,9 +1817,9 @@ describe('annotation.js', function() {
}).controller; }).controller;
controller.edit(); controller.edit();
assert.equal(controller.action, 'edit'); assert.equal(controller.action, 'edit');
controller.annotation.text = 'this should be reverted'; controller.form.text = 'this should be reverted';
controller.revert(); controller.revert();
assert.equal(controller.annotation.text, void 0); assert.equal(controller.form.text, void 0);
}); });
}); });
}); });
...@@ -71,7 +71,7 @@ ...@@ -71,7 +71,7 @@
<!-- Body --> <!-- Body -->
<section name="text" class="annotation-body"> <section name="text" class="annotation-body">
<excerpt enabled="vm.feature('truncate_annotations') && !vm.editing()"> <excerpt enabled="vm.feature('truncate_annotations') && !vm.editing()">
<markdown ng-model="vm.annotation.text" <markdown ng-model="vm.form.text"
read-only="!vm.editing()" read-only="!vm.editing()"
></markdown> ></markdown>
</excerpt> </excerpt>
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
<!-- Tags --> <!-- Tags -->
<div class="annotation-body form-field" ng-if="vm.editing()"> <div class="annotation-body form-field" ng-if="vm.editing()">
<tags-input ng-model="vm.annotation.tags" <tags-input ng-model="vm.form.tags"
name="tags" name="tags"
class="tags" class="tags"
placeholder="Add tags…" placeholder="Add tags…"
...@@ -94,9 +94,9 @@ ...@@ -94,9 +94,9 @@
</div> </div>
<div class="annotation-body tags tags-read-only" <div class="annotation-body tags tags-read-only"
ng-if="vm.annotation.tags.length && !vm.editing()"> ng-if="vm.form.tags.length && !vm.editing()">
<ul class="tag-list"> <ul class="tag-list">
<li class="tag-item" ng-repeat="tag in vm.annotation.tags"> <li class="tag-item" ng-repeat="tag in vm.form.tags">
<a href="/stream?q=tag:'{{tag.text|urlencode}}'" target="_blank">{{tag.text}}</a> <a href="/stream?q=tag:'{{tag.text|urlencode}}'" target="_blank">{{tag.text}}</a>
</li> </li>
</ul> </ul>
......
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