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