-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix display and update of the privacy selector
This commit reworks the privacy directive in order to make management of the privacy state of individual annotations (and their privacy dropdown) much clearer. It fixes a bug where the privacy dropdown didn't make any distinction between publishing to a group and publishing to "world". In addition, it makes the implementation of the groups dropdown more consistent with the rest of the application.
- Loading branch information
1 parent
dc76973
commit 9be6890
Showing
21 changed files
with
605 additions
and
537 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
'use strict'; | ||
|
||
// This is called visibility for backwards compatibility | ||
var STORAGE_KEY = 'hypothesis.visibility'; | ||
|
||
var PUBLIC = 'public'; | ||
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 | ||
}; | ||
} | ||
|
||
|
||
// Determine the privacy level for an annotation with permissions and group | ||
// fields as passed. If the permissions object is empty, return null. | ||
function annotationLevel(permissions, group) { | ||
if (permissions === null || typeof permissions === 'undefined') { | ||
return null; | ||
} | ||
if (permissions.read === null || typeof permissions.read === 'undefined') { | ||
return null; | ||
} | ||
|
||
var entry; | ||
if (group === null || typeof group === 'undefined') { | ||
entry = 'group:__world__'; | ||
} else { | ||
entry = 'group:' + group; | ||
} | ||
if (permissions.read.indexOf(entry) !== -1) { | ||
return PUBLIC; | ||
} else { | ||
return PRIVATE; | ||
} | ||
} | ||
|
||
|
||
// @ngInject | ||
function PrivacyController($scope, groups, localStorage, permissions) { | ||
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) { | ||
this._level = annotationLevel($scope.model.permissions, | ||
$scope.model.group); | ||
} | ||
|
||
// If the privacy level still isn't set, try and retrieve it from | ||
// localStorage, falling back to public. | ||
if (this._level === null) { | ||
var fromStorage = localStorage.getItem(STORAGE_KEY); | ||
if ([PUBLIC, PRIVATE].indexOf(fromStorage) !== -1) { | ||
this._level = fromStorage; | ||
} else { | ||
this._level = PUBLIC; | ||
} | ||
} | ||
|
||
if (this._level === PUBLIC) { | ||
return this.public(); | ||
} | ||
return this.private(); | ||
}; | ||
|
||
/** | ||
* @ngdoc method | ||
* @name PrivacyController#setLevel | ||
* | ||
* @param {String} level | ||
* | ||
* Sets the current privacy level. `level` may be either 'private' or | ||
* 'public'. | ||
*/ | ||
this.setLevel = function (level) { | ||
if (level === PUBLIC) { | ||
this._level = PUBLIC; | ||
localStorage.setItem(STORAGE_KEY, PUBLIC); | ||
$scope.model.permissions = permissions.public($scope.model.group); | ||
} else if (level === PRIVATE) { | ||
this._level = PRIVATE; | ||
localStorage.setItem(STORAGE_KEY, PRIVATE); | ||
$scope.model.permissions = permissions.private(); | ||
} | ||
}; | ||
|
||
/** | ||
* @ngdoc method | ||
* @name PrivacyController#public | ||
* | ||
* Returns a descriptor object for the current 'public' privacy level. | ||
*/ | ||
this.public = function () { | ||
// If the annotation has a group set on it, then return the data for that | ||
// group, otherwise just use the currently-focused group. | ||
if ($scope.model.group) { | ||
return describeGroup(groups.get($scope.model.group)); | ||
} else { | ||
return describeGroup(groups.focused()); | ||
} | ||
}; | ||
|
||
/** | ||
* @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', | ||
restrict: 'E', | ||
scope: { | ||
model: '=' | ||
}, | ||
templateUrl: 'privacy.html' | ||
}; | ||
}; | ||
|
||
|
||
exports.PrivacyController = PrivacyController; | ||
exports.directive = directive; |
Oops, something went wrong.