/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* JavaScript for common UI services */ (function() { 'use strict'; /** * @name Dialog * @constructor */ function Dialog() { } /** * @name alert * @desc Show an alert dialog box with a single "OK" button * @param {string} title * @param {string} content */ Dialog.alert = function(title, content) { this.$modal.open({ template: '
' + '' + '' + l('OK') + '' + '', windowClass: 'small', controller: function($scope, $modalInstance) { $scope.title = title; $scope.content = content; $scope.closeModal = function() { $modalInstance.close(); }; } }); }; /** * @name confirm * @desc Show a confirmation dialog box with buttons "Cancel" and "OK" * @param {string} title * @param {string} content * @returns a promise that always resolves, but returns true only if the user user has clicked on the * 'OK' button */ Dialog.confirm = function(title, content) { var d = this.$q.defer(), confirm = this.$modal.confirm() .title(title) .content(content) .ok(l('OK')) .cancel(l('Cancel')); this.$modal.show(confirm).then(function() { d.resolve(); }, function() { d.reject(); }); return d.promise; }; Dialog.prompt = function(title, inputPlaceholder, options) { var o = options || {}, d = this.$q.defer(); this.$modal.open({ template: '' + '' + '' + l('OK') + '' + '' + l('Cancel') + '' + '', windowClass: 'small', controller: function($scope, $modalInstance) { $scope.title = title; $scope.inputValue = o.inputValue || ''; $scope.closeModal = function() { $modalInstance.close(); d.resolve(false); }; $scope.confirm = function(value) { $modalInstance.close(); d.resolve(value); }; } }); return d.promise; }; /** * @memberof Dialog * @desc The factory we'll register as sgDialog in the Angular module SOGo.UIDesktop */ Dialog.$factory = ['$q', '$mdDialog', function($q, $mdDialog) { angular.extend(Dialog, { $q: $q , $modal: $mdDialog }); return Dialog; // return constructor }]; /* Angular module instanciation */ angular.module('SOGo.UIDesktop', ['ngMaterial', 'RecursionHelper']) /* Factory registration in Angular module */ .factory('sgDialog', Dialog.$factory) /** * sgEnter - A directive evaluated when the enter key is pressed * @memberof SOGo.UIDesktop * @example: */ .directive('sgEnter', function() { var ENTER_KEY = 13; return function(scope, element, attrs) { element.bind("keydown keypress", function(event) { if (event.which === ENTER_KEY) { scope.$apply(function() { scope.$eval(attrs.sgEnter); }); event.preventDefault(); } }); }; }) /** * sgEscape - A directive evaluated when the escape key is pressed * @memberof SOGo.UIDesktop * @example: */ .directive('sgEscape', function() { var ESCAPE_KEY = 27; return function(scope, elem, attrs) { elem.bind('keydown', function(event) { if (event.keyCode === ESCAPE_KEY) { scope.$apply(attrs.sgEscape); } }); }; }) /** * sgFocusOn - A directive that sets the focus on its element when the specified string is broadcasted * @memberof SOGo.UIDesktop * @see {@link SOGo.UIDesktop.sgFocus} * @example: */ .directive('sgFocusOn', function() { return function(scope, elem, attr) { scope.$on('sgFocusOn', function(e, name) { if (name === attr.sgFocusOn) { elem[0].focus(); elem[0].select(); } }); }; }) /** * sgFocus - A service to set the focus on the element associated to a specific string * @memberof SOGo.UIDesktop * @param {string} name - the string identifier of the element * @see {@link SOGo.UIDesktop.sgFocusOn} */ .factory('sgFocus', ['$rootScope', '$timeout', function($rootScope, $timeout) { return function(name) { $timeout(function() { $rootScope.$broadcast('sgFocusOn', name); }); } }]) /* * sgFolderTree - Provides hierarchical folders tree * @memberof SOGo.UIDesktop * @restrict element * @param {object} sgRoot * @param {object} sgFolder * @param {function} sgSetFolder * @see https://github.com/marklagendijk/angular-recursion * @example:World!