Commit 57ae83d4 authored by Randall Leeds's avatar Randall Leeds

Merge pull request #1741 from hypothesis/sticky-visibility-level-object

Use an object for privacy level models
parents eabb1f59 1c2303aa
...@@ -2,6 +2,18 @@ privacy = ['$window', ($window) -> ...@@ -2,6 +2,18 @@ privacy = ['$window', ($window) ->
VISIBILITY_KEY ='hypothesis.visibility' VISIBILITY_KEY ='hypothesis.visibility'
VISIBILITY_PUBLIC = 'public' VISIBILITY_PUBLIC = 'public'
VISIBILITY_PRIVATE = 'private' VISIBILITY_PRIVATE = 'private'
levels = [
{name: VISIBILITY_PUBLIC, text: 'Public'}
{name: VISIBILITY_PRIVATE, text: 'Only Me'}
]
getLevel = (name) ->
for level in levels
if level.name == name
return level
undefined
# Detection is needed because we run often as a third party widget and # Detection is needed because we run often as a third party widget and
# third party storage blocking often blocks cookies and local storage # third party storage blocking often blocks cookies and local storage
# https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js # https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js
...@@ -18,8 +30,6 @@ privacy = ['$window', ($window) -> ...@@ -18,8 +30,6 @@ privacy = ['$window', ($window) ->
setItem: (key, value) -> setItem: (key, value) ->
memoryStorage[key] = value memoryStorage[key] = value
levels = ['Public', 'Only Me']
link: (scope, elem, attrs, controller) -> link: (scope, elem, attrs, controller) ->
return unless controller? return unless controller?
...@@ -27,16 +37,16 @@ privacy = ['$window', ($window) -> ...@@ -27,16 +37,16 @@ privacy = ['$window', ($window) ->
return unless permissions? return unless permissions?
if 'group:__world__' in (permissions.read or []) if 'group:__world__' in (permissions.read or [])
'Public' getLevel(VISIBILITY_PUBLIC)
else else
'Only Me' getLevel(VISIBILITY_PRIVATE)
controller.$parsers.push (privacy) -> controller.$parsers.push (privacy) ->
return unless privacy? return unless privacy?
permissions = controller.$modelValue permissions = controller.$modelValue
if privacy is 'Public' if privacy.name is VISIBILITY_PUBLIC
permissions.read = ['group:__world__'] permissions.read = ['group:__world__']
else else
permissions.read = [attrs.user] permissions.read = [attrs.user]
...@@ -49,22 +59,15 @@ privacy = ['$window', ($window) -> ...@@ -49,22 +59,15 @@ privacy = ['$window', ($window) ->
controller.$render = -> controller.$render = ->
unless controller.$modelValue.read.length unless controller.$modelValue.read.length
visibility = storage.getItem VISIBILITY_KEY name = storage.getItem VISIBILITY_KEY
level = if visibility is VISIBILITY_PUBLIC level = getLevel(name)
'Public'
else
'Only Me'
controller.$setViewValue level controller.$setViewValue level
scope.level = controller.$viewValue scope.level = controller.$viewValue
scope.levels = levels scope.levels = levels
scope.setLevel = (level) -> scope.setLevel = (level) ->
visibility = if level is 'Public' storage.setItem VISIBILITY_KEY, level.name
VISIBILITY_PUBLIC
else
VISIBILITY_PRIVATE
storage.setItem VISIBILITY_KEY, visibility
controller.$setViewValue level controller.$setViewValue level
controller.$render() controller.$render()
require: '?ngModel' require: '?ngModel'
......
...@@ -3,15 +3,16 @@ ...@@ -3,15 +3,16 @@
role="button" role="button"
class="dropdown-toggle" class="dropdown-toggle"
data-toggle="dropdown"> data-toggle="dropdown">
<i class="small" ng-class="{'h-icon-earth': level == 'Public', 'h-icon-locked': level == 'Only Me'}"></i> <i class="small" ng-class="{'h-icon-earth': level.name == 'public',
{{level}} 'h-icon-locked': level.name == 'private'}"></i>
{{level.text}}
<i class="h-icon-triangle"></i> <i class="h-icon-triangle"></i>
</span> </span>
<ul class="dropdown-menu" role="menu"> <ul class="dropdown-menu" role="menu">
<li ng-repeat="p in levels" ng-click="setLevel(p)"> <li ng-repeat="level in levels" ng-click="setLevel(level)">
<i class="small" ng-class="{'h-icon-earth': p == 'Public', <i class="small" ng-class="{'h-icon-earth': level.name == 'public',
'h-icon-locked': p == 'Only Me'}"></i> 'h-icon-locked': level.name == 'private'}"></i>
{{p}} {{level.text}}
</li> </li>
</ul> </ul>
</div> </div>
...@@ -42,7 +42,7 @@ describe 'h.directives.privacy', -> ...@@ -42,7 +42,7 @@ describe 'h.directives.privacy', ->
$element = $compile('<privacy ng-model="permissions">')($scope) $element = $compile('<privacy ng-model="permissions">')($scope)
$scope.$digest() $scope.$digest()
$isolateScope = $element.isolateScope() $isolateScope = $element.isolateScope()
$isolateScope.setLevel('Public') $isolateScope.setLevel(name: VISIBILITY_PUBLIC)
$scope2.permissions = {read: []} $scope2.permissions = {read: []}
$element = $compile('<privacy ng-model="permissions">')($scope2) $element = $compile('<privacy ng-model="permissions">')($scope2)
...@@ -71,7 +71,7 @@ describe 'h.directives.privacy', -> ...@@ -71,7 +71,7 @@ describe 'h.directives.privacy', ->
$element = $compile('<privacy ng-model="permissions">')($scope) $element = $compile('<privacy ng-model="permissions">')($scope)
$scope.$digest() $scope.$digest()
$isolateScope = $element.isolateScope() $isolateScope = $element.isolateScope()
$isolateScope.setLevel('Public') $isolateScope.setLevel(name: VISIBILITY_PUBLIC)
expected = VISIBILITY_PUBLIC expected = VISIBILITY_PUBLIC
stored = store.getItem VISIBILITY_KEY stored = store.getItem VISIBILITY_KEY
...@@ -94,14 +94,14 @@ describe 'h.directives.privacy', -> ...@@ -94,14 +94,14 @@ describe 'h.directives.privacy', ->
$isolateScope = $element.isolateScope() $isolateScope = $element.isolateScope()
it 'sets the initial permissions based on the stored privacy level', -> it 'sets the initial permissions based on the stored privacy level', ->
assert.equal $isolateScope.level, 'Public' assert.equal $isolateScope.level.name, VISIBILITY_PUBLIC
it 'does not alter the level on subsequent renderings', -> it 'does not alter the level on subsequent renderings', ->
modelCtrl = $element.controller('ngModel') modelCtrl = $element.controller('ngModel')
store.setItem VISIBILITY_KEY, VISIBILITY_PRIVATE store.setItem VISIBILITY_KEY, VISIBILITY_PRIVATE
$scope.permissions.read = ['acct:user@example.com'] $scope.permissions.read = ['acct:user@example.com']
$scope.$digest() $scope.$digest()
assert.equal $isolateScope.level, 'Public' assert.equal $isolateScope.level.name, VISIBILITY_PUBLIC
describe 'when permissions.read is filled', -> describe 'when permissions.read is filled', ->
it 'does not alter the level', -> it 'does not alter the level', ->
...@@ -111,7 +111,7 @@ describe 'h.directives.privacy', -> ...@@ -111,7 +111,7 @@ describe 'h.directives.privacy', ->
$element = $compile('<privacy ng-model="permissions">')($scope) $element = $compile('<privacy ng-model="permissions">')($scope)
$scope.$digest() $scope.$digest()
$isolateScope = $element.isolateScope() $isolateScope = $element.isolateScope()
assert.equal($isolateScope.level, 'Public') assert.equal($isolateScope.level.name, VISIBILITY_PUBLIC)
describe 'user attribute', -> describe 'user attribute', ->
beforeEach -> beforeEach ->
......
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