Init share app, client refactoring

This commit is contained in:
jendib
2013-08-15 00:43:06 +02:00
parent ecb2afd628
commit ed85909d00
53 changed files with 377 additions and 133 deletions
+222
View File
@@ -0,0 +1,222 @@
'use strict';
/**
* Sismics Docs application.
*/
var App = angular.module('docs',
// Dependencies
['ui.state', 'ui.bootstrap', 'ui.route', 'ui.keypress', 'ui.validate',
'ui.sortable', 'restangular', 'ngSanitize', 'ngMobile', 'colorpicker.module']
)
/**
* Configuring modules.
*/
.config(function($stateProvider, $httpProvider, $routeProvider, RestangularProvider) {
// Configuring UI Router
$stateProvider
.state('main', {
url: '',
views: {
'page': {
templateUrl: 'partial/docs/main.html',
controller: 'Main'
}
}
})
.state('tag', {
url: '/tag',
views: {
'page': {
templateUrl: 'partial/docs/tag.html',
controller: 'Tag'
}
}
})
.state('settings', {
url: '/settings',
abstract: true,
views: {
'page': {
templateUrl: 'partial/docs/settings.html',
controller: 'Settings'
}
}
})
.state('settings.default', {
url: '',
views: {
'settings': {
templateUrl: 'partial/docs/settings.default.html',
controller: 'SettingsDefault'
}
}
})
.state('settings.account', {
url: '/account',
views: {
'settings': {
templateUrl: 'partial/docs/settings.account.html',
controller: 'SettingsAccount'
}
}
})
.state('settings.session', {
url: '/session',
views: {
'settings': {
templateUrl: 'partial/docs/settings.session.html',
controller: 'SettingsSession'
}
}
})
.state('settings.log', {
url: '/log',
views: {
'settings': {
templateUrl: 'partial/docs/settings.log.html',
controller: 'SettingsLog'
}
}
})
.state('settings.user', {
url: '/user',
views: {
'settings': {
templateUrl: 'partial/docs/settings.user.html',
controller: 'SettingsUser'
}
}
})
.state('settings.user.edit', {
url: '/edit/:username',
views: {
'user': {
templateUrl: 'partial/docs/settings.user.edit.html',
controller: 'SettingsUserEdit'
}
}
})
.state('settings.user.add', {
url: '/add',
views: {
'user': {
templateUrl: 'partial/docs/settings.user.edit.html',
controller: 'SettingsUserEdit'
}
}
})
.state('document', {
url: '/document',
abstract: true,
views: {
'page': {
templateUrl: 'partial/docs/document.html',
controller: 'Document'
}
}
})
.state('document.default', {
url: '',
views: {
'document': {
templateUrl: 'partial/docs/document.default.html',
controller: 'DocumentDefault'
}
}
})
.state('document.add', {
url: '/add',
views: {
'document': {
templateUrl: 'partial/docs/document.edit.html',
controller: 'DocumentEdit'
}
}
})
.state('document.edit', {
url: '/edit/:id',
views: {
'document': {
templateUrl: 'partial/docs/document.edit.html',
controller: 'DocumentEdit'
}
}
})
.state('document.view', {
url: '/view/:id',
views: {
'document': {
templateUrl: 'partial/docs/document.view.html',
controller: 'DocumentView'
}
}
})
.state('document.view.file', {
url: '/file/:fileId',
views: {
'file': {
controller: 'FileView'
}
}
})
.state('login', {
url: '/login',
views: {
'page': {
templateUrl: 'partial/docs/login.html',
controller: 'Login'
}
}
});
// Configuring Restangular
RestangularProvider.setBaseUrl('api');
// Configuring $http to act like jQuery.ajax
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$httpProvider.defaults.transformRequest = [function(data) {
var param = function(obj) {
var query = '';
var name, value, fullSubName, subName, subValue, innerObj, i;
for(name in obj) {
value = obj[name];
if(value instanceof Array) {
for(i=0; i<value.length; ++i) {
subValue = value[i];
fullSubName = name;
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
} else if(value instanceof Object) {
for(subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
else if(value !== undefined && value !== null) {
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
}
return query.length ? query.substr(0, query.length - 1) : query;
};
return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
}];
})
/**
* Application initialization.
*/
.run(function($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
});
@@ -0,0 +1,112 @@
'use strict';
/**
* Document controller.
*/
App.controller('Document', function($scope, $state, Restangular) {
/**
* Documents table sort status.
*/
$scope.sortColumn = 3;
$scope.asc = false;
$scope.offset = 0;
$scope.currentPage = 1;
$scope.limit = 10;
$scope.isAdvancedSearchCollapsed = true;
/**
* Initialize search criterias.
*/
$scope.initSearch = function() {
$scope.search = {
query: '',
createDateMin: null,
createDateMax: null,
tags: []
};
};
$scope.initSearch();
/**
* Load new documents page.
*/
$scope.pageDocuments = function() {
Restangular.one('document')
.getList('list', {
offset: $scope.offset,
limit: $scope.limit,
sort_column: $scope.sortColumn,
asc: $scope.asc,
search: $scope.search.query,
create_date_min: $scope.isAdvancedSearchCollapsed || !$scope.search.createDateMin ? null : $scope.search.createDateMin.getTime(),
create_date_max: $scope.isAdvancedSearchCollapsed || !$scope.search.createDateMax ? null : $scope.search.createDateMax.getTime(),
'tags': $scope.isAdvancedSearchCollapsed ? null : _.pluck($scope.search.tags, 'id')
})
.then(function(data) {
$scope.documents = data.documents;
$scope.totalDocuments = data.total;
$scope.numPages = Math.ceil(data.total / $scope.limit);
});
};
/**
* Reload documents.
*/
$scope.loadDocuments = function() {
$scope.offset = 0;
$scope.currentPage = 1;
$scope.pageDocuments();
};
/**
* Watch for current page change.
*/
$scope.$watch('currentPage', function(prev, next) {
if (prev == next) {
return;
}
$scope.offset = ($scope.currentPage - 1) * $scope.limit;
$scope.pageDocuments();
});
/**
* Watch for search change.
*/
$scope.$watch('search', function(prev, next) {
$scope.loadDocuments();
}, true);
/**
* Sort documents.
*/
$scope.sortDocuments = function(sortColumn) {
if (sortColumn == $scope.sortColumn) {
$scope.asc = !$scope.asc;
} else {
$scope.asc = true;
}
$scope.sortColumn = sortColumn;
$scope.loadDocuments();
};
/**
* Go to add document form.
*/
$scope.addDocument = function() {
$state.transitionTo('document.add');
};
/**
* Go to edit document form.
*/
$scope.editDocument = function(id) {
$state.transitionTo('document.edit', { id: id });
};
/**
* Display a document.
*/
$scope.viewDocument = function(id) {
$state.transitionTo('document.view', { id: id });
};
});
@@ -0,0 +1,9 @@
'use strict';
/**
* Document default controller.
*/
App.controller('DocumentDefault', function($scope, $state, Restangular) {
// Load app data
$scope.app = Restangular.one('app').get();
});
@@ -0,0 +1,154 @@
'use strict';
/**
* Document edition controller.
*/
App.controller('DocumentEdit', function($scope, $q, $http, $state, $stateParams, Restangular, Tag) {
// Alerts
$scope.alerts = [];
/**
* Close an alert.
*/
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
/**
* Returns a promise for typeahead title.
*/
$scope.getTitleTypeahead = function($viewValue) {
var deferred = $q.defer();
Restangular.one('document')
.getList('list', {
limit: 5,
sort_column: 1,
asc: true,
search: $viewValue
}).then(function(data) {
deferred.resolve(_.uniq(_.pluck(data.documents, 'title'), true));
});
return deferred.promise;
};
/**
* Returns true if in edit mode (false in add mode).
*/
$scope.isEdit = function() {
return $stateParams.id;
};
/**
* In edit mode, load the current document.
*/
if ($scope.isEdit()) {
Restangular.one('document', $stateParams.id).get().then(function(data) {
$scope.document = data;
});
} else {
$scope.document = { tags: [] };
}
/**
* Edit a document.
*/
$scope.edit = function() {
var promise = null;
var document = angular.copy($scope.document);
// Transform date to timestamp
if (document.create_date instanceof Date) {
document.create_date = document.create_date.getTime();
}
// Extract ids from tags
document.tags = _.pluck(document.tags, 'id');
if ($scope.isEdit()) {
promise = Restangular
.one('document', $stateParams.id)
.post('', document);
} else {
promise = Restangular
.one('document')
.put(document);
}
// Upload files after edition
promise.then(function(data) {
$scope.fileProgress = 0;
// When all files upload are over, move on
var navigateNext = function() {
if ($scope.isEdit()) {
$scope.pageDocuments();
$state.transitionTo('document.view', { id: $stateParams.id });
} else {
var fileUploadCount = _.size($scope.newFiles);
$scope.alerts.unshift({
type: 'success',
msg: 'Document successfully added (with ' + fileUploadCount + ' file' + (fileUploadCount > 1 ? 's' : '') + ')'
});
$scope.document = { tags: [] };
$scope.newFiles = [];
$scope.loadDocuments();
}
}
if (_.size($scope.newFiles) == 0) {
navigateNext();
} else {
$scope.fileIsUploading = true;
// Send a file from the input file array and return a promise
var sendFile = function(key) {
// Build the payload
var file = $scope.newFiles[key];
var formData = new FormData();
formData.append('id', data.id);
formData.append('file', file);
// Send the file
var promiseFile = $http.put('api/file',
formData, {
headers: { 'Content-Type': false },
transformRequest: function(data) { return data; }
});
// TODO Handle progression when $q.notify will be released
promiseFile.then(function() {
$scope.fileProgress += 100 / _.size($scope.newFiles);
});
return promiseFile;
};
// Upload files sequentially
var key = 0;
var then = function() {
key++;
if ($scope.newFiles[key]) {
sendFile(key).then(then);
} else {
$scope.fileIsUploading = false;
$scope.fileProgress = 0;
navigateNext();
}
};
sendFile(key).then(then);
}
});
};
/**
* Cancel edition.
*/
$scope.cancel = function() {
if ($scope.isEdit()) {
$state.transitionTo('document.view', { id: $stateParams.id });
} else {
$state.transitionTo('document.default');
}
};
});
@@ -0,0 +1,157 @@
'use strict';
/**
* Document view controller.
*/
App.controller('DocumentView', function ($scope, $state, $stateParams, $location, $dialog, Restangular) {
// Load data from server
Restangular.one('document', $stateParams.id).get().then(function(data) {
$scope.document = data;
});
/**
* Configuration for file sorting.
*/
$scope.fileSortableOptions = {
forceHelperSize: true,
forcePlaceholderSize: true,
tolerance: 'pointer',
handle: '.handle',
stop: function (e, ui) {
// Send new positions to server
$scope.$apply(function () {
Restangular.one('file').post('reorder', {
id: $stateParams.id,
order: _.pluck($scope.files, 'id')
});
});
}
};
/**
* Load files from server.
*/
$scope.loadFiles = function () {
Restangular.one('file').getList('list', { id: $stateParams.id }).then(function (data) {
$scope.files = data.files;
});
};
$scope.loadFiles();
/**
* Navigate to the selected file.
*/
$scope.openFile = function (file) {
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: file.id })
};
/**
* Delete a document.
*/
$scope.deleteDocument = function (document) {
var title = 'Delete document';
var msg = 'Do you really want to delete this document?';
var btns = [
{result: 'cancel', label: 'Cancel'},
{result: 'ok', label: 'OK', cssClass: 'btn-primary'}
];
$dialog.messageBox(title, msg, btns)
.open()
.then(function (result) {
if (result == 'ok') {
Restangular.one('document', document.id).remove().then(function () {
$scope.loadDocuments();
$state.transitionTo('document.default');
});
}
});
};
/**
* Delete a file.
*/
$scope.deleteFile = function (file) {
var title = 'Delete file';
var msg = 'Do you really want to delete this file?';
var btns = [
{result: 'cancel', label: 'Cancel'},
{result: 'ok', label: 'OK', cssClass: 'btn-primary'}
];
$dialog.messageBox(title, msg, btns)
.open()
.then(function (result) {
if (result == 'ok') {
Restangular.one('file', file.id).remove().then(function () {
$scope.loadFiles();
});
}
});
};
/**
* Open the share dialog.
*/
$scope.share = function () {
$dialog.dialog({
backdrop: false,
keyboard: true,
templateUrl: 'partial/docs/document.share.html',
controller: function ($scope, dialog) {
$scope.name = '';
$scope.close = function (name) {
dialog.close(name);
}
}
}).open().then(function (name) {
if (name == null) {
return;
}
// Share the document
Restangular.one('share').put({
name: name,
id: $stateParams.id
}).then(function (data) {
var share = {
name: name,
id: data.id
};
// Display the new share and add it to the local shares
$scope.showShare(share);
$scope.document.shares.push(share);
})
});
};
/**
* Display a share.
*/
$scope.showShare = function(share) {
// Show the link
var link = $location.absUrl().replace($location.path(), '').replace('#', '') + 'share.html#/share/' + $stateParams.id + '/' + share.id;
var title = 'Shared document';
var msg = 'You can share this document by giving this link. ' +
'Note that everyone having this link can see the document.<br/>' +
'<input class="input-block-level share-link" type="text" readonly="readonly" value="' + link + '" />';
var btns = [
{result: 'unshare', label: 'Unshare', cssClass: 'btn-danger'},
{result: 'close', label: 'Close'}
];
$dialog.messageBox(title, msg, btns)
.open()
.then(function (result) {
if (result == 'unshare') {
// Unshare this document and update the local shares
Restangular.one('share', share.id).remove().then(function () {
$scope.document.shares = _.reject($scope.document.shares, function(s) {
return share.id == s.id;
});
});
}
});
};
});
@@ -0,0 +1,77 @@
'use strict';
/**
* File view controller.
*/
App.controller('FileView', function($dialog, $state, $stateParams) {
var dialog = $dialog.dialog({
keyboard: true,
templateUrl: 'partial/docs/file.view.html',
controller: function($scope, $state, $stateParams, Restangular, dialog) {
$scope.id = $stateParams.fileId;
// Load files
Restangular.one('file').getList('list', { id: $stateParams.id }).then(function(data) {
$scope.files = data.files;
// Search current file
_.each($scope.files, function(value, key, list) {
if (value.id == $scope.id) {
$scope.file = value;
}
});
});
/**
* Navigate to the next file.
*/
$scope.nextFile = function() {
_.each($scope.files, function(value, key, list) {
if (value.id == $scope.id) {
var next = $scope.files[key + 1];
if (next) {
dialog.close({});
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: next.id });
}
}
});
};
/**
* Navigate to the previous file.
*/
$scope.previousFile = function() {
_.each($scope.files, function(value, key, list) {
if (value.id == $scope.id) {
var previous = $scope.files[key - 1];
if (previous) {
dialog.close({});
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: previous.id });
}
}
});
};
/**
* Open the file in a new window.
*/
$scope.openFile = function() {
window.open('api/file/' + $scope.id + '/data');
};
/**
* Close the file preview.
*/
$scope.closeFile = function () {
dialog.close();
};
}
});
// Returns to document view on file close
dialog.open().then(function(result) {
if (result == null) {
$state.transitionTo('document.view', { id: $stateParams.id });
}
});
});
@@ -0,0 +1,19 @@
'use strict';
/**
* Login controller.
*/
App.controller('Login', function($scope, $rootScope, $state, $dialog, User) {
$scope.login = function() {
User.login($scope.user).then(function() {
$rootScope.userInfo = User.userInfo(true);
$state.transitionTo('document.default');
}, function() {
var title = 'Login failed';
var msg = 'Username or password invalid';
var btns = [{result:'ok', label: 'OK', cssClass: 'btn-primary'}];
$dialog.messageBox(title, msg, btns).open();
});
};
});
@@ -0,0 +1,14 @@
'use strict';
/**
* Main controller.
*/
App.controller('Main', function($scope, $rootScope, $state, User) {
User.userInfo().then(function(data) {
if (data.anonymous) {
$state.transitionTo('login');
} else {
$state.transitionTo('document.default');
}
});
});
@@ -0,0 +1,19 @@
'use strict';
/**
* Navigation controller.
*/
App.controller('Navigation', function($scope, $state, $rootScope, User, Restangular) {
$rootScope.userInfo = User.userInfo();
/**
* User logout.
*/
$scope.logout = function($event) {
User.logout().then(function() {
$rootScope.userInfo = User.userInfo(true);
$state.transitionTo('main');
});
$event.preventDefault();
};
});
@@ -0,0 +1,11 @@
'use strict';
/**
* Settings controller.
*/
App.controller('Settings', function($scope, Restangular) {
// Flag if the user is admin
$scope.userInfo.then(function (data) {
$scope.isAdmin = data.base_functions.indexOf('ADMIN') != -1;
});
});
@@ -0,0 +1,28 @@
'use strict';
/**
* Settings account controller.
*/
App.controller('SettingsAccount', function($scope, Restangular) {
$scope.editUserAlert = false;
// Alerts
$scope.alerts = [];
/**
* Close an alert.
*/
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
/**
* Edit user.
*/
$scope.editUser = function() {
Restangular.one('user').post('', $scope.user).then(function() {
$scope.user = {};
$scope.alerts.push({ type: 'success', msg: 'Account successfully updated' });
});
};
});
@@ -0,0 +1,7 @@
'use strict';
/**
* Settings default page controller.
*/
App.controller('SettingsDefault', function($scope, Restangular) {
});
@@ -0,0 +1,12 @@
'use strict';
/**
* Settings logs controller.
*/
App.controller('SettingsLog', function($scope, Restangular) {
Restangular.one('app/log').get({
limit: 100
}).then(function(data) {
$scope.logs = data.logs;
});
});
@@ -0,0 +1,26 @@
'use strict';
/**
* Settings session controller.
*/
App.controller('SettingsSession', function($scope, Restangular) {
/**
* Load sessions.
*/
$scope.loadSession = function() {
Restangular.one('user').getList('session').then(function(data) {
$scope.sessions = data.sessions;
});
};
/**
* Clear all active sessions.
*/
$scope.deleteSession = function() {
Restangular.one('user/session').remove().then(function() {
$scope.loadSession();
})
};
$scope.loadSession();
});
@@ -0,0 +1,24 @@
'use strict';
/**
* Settings user page controller.
*/
App.controller('SettingsUser', function($scope, $state, Restangular) {
/**
* Load users from server.
*/
$scope.loadUsers = function() {
Restangular.one('user/list').get({ limit: 100 }).then(function(data) {
$scope.users = data.users;
});
};
$scope.loadUsers();
/**
* Edit a user.
*/
$scope.editUser = function(user) {
$state.transitionTo('settings.user.edit', { username: user.username });
};
});
@@ -0,0 +1,67 @@
'use strict';
/**
* Settings user edition page controller.
*/
App.controller('SettingsUserEdit', function($scope, $dialog, $state, $stateParams, Restangular) {
/**
* Returns true if in edit mode (false in add mode).
*/
$scope.isEdit = function() {
return $stateParams.username;
};
/**
* In edit mode, load the current user.
*/
if ($scope.isEdit()) {
Restangular.one('user', $stateParams.username).get().then(function(data) {
$scope.user = data;
});
}
/**
* Update the current user.
*/
$scope.edit = function() {
var promise = null;
if ($scope.isEdit()) {
promise = Restangular
.one('user', $stateParams.username)
.post('', $scope.user);
} else {
promise = Restangular
.one('user')
.put($scope.user);
}
promise.then(function() {
$scope.loadUsers();
$state.transitionTo('settings.user');
});
};
/**
* Delete the current user.
*/
$scope.remove = function () {
var title = 'Delete user';
var msg = 'Do you really want to delete this user? All associated documents, files and tags will be deleted';
var btns = [{result:'cancel', label: 'Cancel'}, {result:'ok', label: 'OK', cssClass: 'btn-primary'}];
$dialog.messageBox(title, msg, btns)
.open()
.then(function(result) {
if (result == 'ok') {
Restangular.one('user', $stateParams.username).remove().then(function() {
$scope.loadUsers();
$state.transitionTo('settings.user');
}, function () {
$state.transitionTo('settings.user');
});
}
});
};
});
@@ -0,0 +1,79 @@
'use strict';
/**
* Tag controller.
*/
App.controller('Tag', function($scope, $dialog, $state, Tag, Restangular) {
$scope.tag = { name: '', color: '#3a87ad' };
// Retrieve tags
Tag.tags().then(function(data) {
$scope.tags = data.tags;
});
// Retrieve tag stats
Restangular.one('tag/stats').get().then(function(data) {
$scope.stats = data.stats;
})
/**
* Returns total number of document from tag stats.
*/
$scope.getStatCount = function() {
return _.reduce($scope.stats, function(memo, stat) {
return memo + stat.count
}, 0);
};
/**
* Add a tag.
*/
$scope.addTag = function() {
// TODO Check if the tag don't already exists
Restangular.one('tag').put($scope.tag).then(function(data) {
$scope.tags.push({ id: data.id, name: $scope.tag.name, color: $scope.tag.color });
$scope.tag = { name: '', color: '#3a87ad' };
});
};
/**
* Delete a tag.
*/
$scope.deleteTag = function(tag) {
var title = 'Delete tag';
var msg = 'Do you really want to delete this tag?';
var btns = [
{result: 'cancel', label: 'Cancel'},
{result: 'ok', label: 'OK', cssClass: 'btn-primary'}
];
$dialog.messageBox(title, msg, btns)
.open()
.then(function(result) {
if (result == 'ok') {
Restangular.one('tag', tag.id).remove().then(function() {
$scope.tags = _.reject($scope.tags, function(t) {
return tag.id == t.id;
});
});
}
});
};
/**
* Update a tag.
*/
$scope.updateTag = function(tag) {
// Update the server
Restangular.one('tag', tag.id).post('', tag).then(function () {
// Update the stat object
var stat = _.find($scope.stats, function (t) {
return tag.id == t.id;
});
if (stat) {
_.extend(stat, tag);
}
});
};
});
@@ -0,0 +1,21 @@
'use strict';
/**
* File upload directive.
*/
App.directive('file', function() {
return {
restrict: 'E',
template: '<input type="file" />',
replace: true,
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
var listener = function() {
scope.$apply(function() {
attr.multiple ? ctrl.$setViewValue(element[0].files) : ctrl.$setViewValue(element[0].files[0]);
});
}
element.bind('change', listener);
}
}
});
@@ -0,0 +1,56 @@
'use strict';
/**
* Inline edition directive.
* Thanks to http://jsfiddle.net/joshdmiller/NDFHg/
*/
App.directive('inlineEdit', function() {
return {
restrict: 'E',
scope: {
value: '=',
editCallback: '&onEdit'
},
template: '<span ng-click="edit()" ng-bind="value"></span><input type="text" ng-model="value" />',
link: function (scope, element, attrs) {
// Let's get a reference to the input element, as we'll want to reference it.
var inputElement = angular.element(element.children()[1]);
var el = inputElement[0];
// This directive should have a set class so we can style it.
element.addClass('inline-edit');
// Initially, we're not editing.
scope.editing = false;
// ng-click handler to activate edit-in-place
scope.edit = function () {
scope.editing = true;
scope.oldValue = el.value;
// We control display through a class on the directive itself. See the CSS.
element.addClass('active');
// And we must focus the element.
// `angular.element()` provides a chainable array, like jQuery so to access a native DOM function,
// we have to reference the first element in the array.
el.focus();
el.selectionStart = 0;
el.selectionEnd = el.value.length;
};
// When we leave the input, we're done editing.
inputElement.on('blur', function() {
scope.editing = false;
element.removeClass('active');
// Invoke parent scope callback
if (scope.editCallback && scope.oldValue != el.value) {
scope.$apply(function() {
scope.editCallback();
});
}
});
}
};
});
@@ -0,0 +1,64 @@
'use strict';
/**
* Tag selection directive.
*/
App.directive('selectTag', function() {
return {
restrict: 'E',
templateUrl: 'partial/docs/directive.selecttag.html',
replace: true,
scope: {
tags: '=',
ref: '@'
},
controller: function($scope, Tag) {
// Retrieve tags
Tag.tags().then(function(data) {
$scope.allTags = data.tags;
});
/**
* Add a tag.
*/
$scope.addTag = function($event) {
// Does the new tag exists
var tag = _.find($scope.allTags, function(tag) {
if (tag.name == $scope.input) {
return tag;
}
});
// Does the new tag is already in the model
var duplicate = _.find($scope.tags, function(tag2) {
if (tag && tag2.id == tag.id) {
return tag2;
}
});
// Add the new tag
if (tag) {
if (!duplicate) {
$scope.tags.push(tag);
}
$scope.input = '';
}
if ($event) {
$event.preventDefault();
}
}
/**
* Remove a tag.
*/
$scope.deleteTag = function(deleteTag) {
$scope.tags = _.reject($scope.tags, function(tag) {
return tag.id == deleteTag.id;
})
};
},
link: function(scope, element, attr, ctrl) {
}
}
});
@@ -0,0 +1,13 @@
'use strict';
/**
* Filter converting new lines in <br />
*/
App.filter('newline', function() {
return function(text) {
if (!text) {
return '';
}
return text.replace(/\n/g, '<br/>');
}
})
@@ -0,0 +1,13 @@
'use strict';
/**
* Filter shortening text in one letter uppercase.
*/
App.filter('shorten', function() {
return function(text) {
if (!text) {
return '';
}
return text.substring(0, 1).toUpperCase();
}
})
@@ -0,0 +1,18 @@
'use strict';
/**
* Tag service.
*/
App.factory('Tag', function(Restangular) {
var tags = null;
return {
/**
* Returns tags.
* @param force If true, force reloading data
*/
tags: function(force) {
return Restangular.one('tag/list').get();
}
}
});
@@ -0,0 +1,35 @@
'use strict';
/**
* User service.
*/
App.factory('User', function(Restangular) {
var userInfo = null;
return {
/**
* Returns user info.
* @param force If true, force reloading data
*/
userInfo: function(force) {
if (userInfo == null || force) {
userInfo = Restangular.one('user').get();
}
return userInfo;
},
/**
* Login an user.
*/
login: function(user) {
return Restangular.one('user').post('login', user);
},
/**
* Logout the current user.
*/
logout: function() {
return Restangular.one('user').post('logout', {});
}
}
});