Commit 4a1f8e3c authored by Robert Knight's avatar Robert Knight

Remove the <privacy> directive

The <privacy> drop-down has been replaced by the
new combined Post / privacy button.

Card 89
parent cfac0123
......@@ -137,7 +137,6 @@ module.exports = angular.module('h', [
.directive('formValidate', require('./directive/form-validate'))
.directive('groupList', require('./directive/group-list').directive)
.directive('markdown', require('./directive/markdown'))
.directive('privacy', require('./directive/privacy').directive)
.directive('simpleSearch', require('./directive/simple-search'))
.directive('statusButton', require('./directive/status-button'))
.directive('thread', require('./directive/thread'))
......
'use strict';
var STORAGE_KEY = 'hypothesis.privacy';
var SHARED = 'shared';
var PRIVATE = 'private';
// Return a descriptor object for the passed group.
function describeGroup(group) {
var type;
if (group.public) {
type = 'public';
} else {
type = 'group';
}
return {
name: group.name,
type: type
};
}
// @ngInject
function PrivacyController($scope, localStorage) {
this._level = null;
/**
* @ngdoc method
* @name PrivacyController#level
*
* Returns the current privacy level descriptor.
*/
this.level = function () {
// If the privacy level isn't set yet, we first try and set it from the
// annotation model
if (this._level === null) {
if ($scope.annotation.isPrivate()) {
this._level = PRIVATE;
} else if ($scope.annotation.isShared()) {
this._level = SHARED;
}
// If the annotation is neither (i.e. it's new) we fall through.
}
// If the privacy level still isn't set, try and retrieve it from
// localStorage, falling back to shared.
if (this._level === null) {
var fromStorage = localStorage.getItem(STORAGE_KEY);
if ([SHARED, PRIVATE].indexOf(fromStorage) !== -1) {
this._level = fromStorage;
} else {
this._level = SHARED;
}
// Since we loaded from localStorage, we need to explicitly set this so
// that the annotation model updates.
this.setLevel(this._level);
}
if (this._level === SHARED) {
return this.shared();
}
return this.private();
};
/**
* @ngdoc method
* @name PrivacyController#setLevel
*
* @param {String} level
*
* Sets the current privacy level. `level` may be either 'private' or
* 'shared'.
*/
this.setLevel = function (level) {
if (level === SHARED) {
this._level = SHARED;
$scope.annotation.setShared();
localStorage.setItem(STORAGE_KEY, SHARED);
} else if (level === PRIVATE) {
this._level = PRIVATE;
$scope.annotation.setPrivate();
localStorage.setItem(STORAGE_KEY, PRIVATE);
}
};
/**
* @ngdoc method
* @name PrivacyController#shared
*
* Returns a descriptor object for the current 'shared' privacy level.
*/
this.shared = function () {
return describeGroup($scope.annotation.group());
};
/**
* @ngdoc method
* @name PrivacyController#private
*
* Returns a descriptor object for the current 'private' privacy level.
*/
this.private = function () {
return {
name: 'Only Me',
type: 'private'
};
};
return this;
}
var directive = function () {
return {
controller: PrivacyController,
controllerAs: 'vm',
link: function (scope, elem, attrs, annotation) {
scope.annotation = annotation;
},
require: '^annotation',
restrict: 'E',
scope: {},
templateUrl: 'privacy.html'
};
};
module.exports = {
directive: directive,
Controller: PrivacyController
};
'use strict';
var PrivacyController = require('../privacy').Controller;
describe('PrivacyController', function () {
var fakeScope;
var fakeLocalStorage;
var sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
fakeScope = {
annotation: {
group: sandbox.stub().returns({name: 'Everyone', public: true}),
isPrivate: sandbox.stub().returns(false),
isShared: sandbox.stub().returns(false),
setPrivate: sandbox.spy(),
setShared: sandbox.spy()
}
};
fakeLocalStorage = {
setItem: sandbox.stub(),
getItem: sandbox.stub()
};
});
afterEach(function () {
sandbox.restore();
});
function controller() {
return new PrivacyController(fakeScope, fakeLocalStorage);
}
describe('.shared()', function () {
it('returns a correct descriptor for the public group', function () {
var c = controller();
var result = c.shared();
assert.deepEqual(result, {name: 'Everyone', type: 'public'});
});
it('returns a correct descriptor for non-public groups', function () {
var c = controller();
fakeScope.annotation.group.returns({name: 'Foo'})
var result = c.shared();
assert.deepEqual(result, {name: 'Foo', type: 'group'});
});
});
describe('.level()', function () {
it('returns the public level if the annotation is shared', function () {
var c = controller();
fakeScope.annotation.isShared.returns(true);
var result = c.level();
assert.equal(result.type, 'public');
});
it('returns the private level if the annotation is private', function () {
var c = controller();
fakeScope.annotation.isPrivate.returns(true);
var result = c.level();
assert.equal(result.type, 'private');
});
it('falls back to localStorage if the annotation is new (shared)', function () {
var c = controller();
fakeLocalStorage.getItem.returns('shared');
var result = c.level();
assert.equal(result.type, 'public');
});
it('falls back to localStorage if the annotation is new (private)', function () {
var c = controller();
fakeLocalStorage.getItem.returns('private');
var result = c.level();
assert.equal(result.type, 'private');
});
it('calls setLevel if the annotation is new to update the model', function () {
var c = controller();
sandbox.spy(c, 'setLevel');
c.level();
assert.calledWith(c.setLevel, 'shared');
});
it('ignores junk data in localStorage', function () {
var c = controller();
fakeLocalStorage.getItem.returns('aslkdhasdug');
var result = c.level();
assert.equal(result.type, 'public');
});
it('falls back to the public level by default', function () {
var c = controller();
var result = c.level();
assert.equal(result.type, 'public');
});
});
describe('.setLevel()', function () {
it('sets the controller state', function () {
var c = controller();
c.setLevel('shared');
assert.equal(c.level().type, 'public');
c.setLevel('private');
assert.equal(c.level().type, 'private');
});
it('calls setShared on the annotation when setting level to shared', function () {
var c = controller();
c.setLevel('shared');
assert.calledOnce(fakeScope.annotation.setShared);
});
it('calls setPrivate on the annotation when setting level to private', function () {
var c = controller();
c.setLevel('private');
assert.calledOnce(fakeScope.annotation.setPrivate);
});
it('stores the last permissions state in localStorage', function () {
var c = controller();
c.setLevel('shared');
assert.calledWithMatch(fakeLocalStorage.setItem,
sinon.match.any,
'shared');
c.setLevel('private');
assert.calledWithMatch(fakeLocalStorage.setItem,
sinon.match.any,
'private');
});
});
});
<div class="dropdown">
<span name="privacy"
role="button"
class="dropdown-toggle"
data-toggle="dropdown">
<i class="small"
ng-class="{'h-icon-public': vm.level().type === 'public',
'h-icon-group': vm.level().type === 'group',
'h-icon-lock': vm.level().type === 'private'}"></i>
<span ng-bind="vm.level().name"></span>
<i class="h-icon-arrow-drop-down"></i>
</span>
<ul class="dropdown-menu" role="menu">
<li ng-click="vm.setLevel('shared')">
<a href="">
<i class="small"
ng-class="{'h-icon-public': vm.shared().type === 'public',
'h-icon-group': vm.shared().type === 'group'}"></i>
<span ng-bind="vm.shared().name"></span>
</a>
</li>
<li ng-click="vm.setLevel('private')">
<a href="">
<i class="small h-icon-lock"></i>
<span ng-bind="vm.private().name"></span>
</a>
</li>
</ul>
</div>
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