(function() { 'use strict'; var app = angular.module('app'); //directive to show frameworks for the user app.directive('frameworks', ['frameworks', 'common', 'user', function (frameworks, common, user) { var directive = { restrict: 'E', scope: { item: '=item', showOnlyActive: '=', active: '=' }, templateUrl: 'app/frameworks/frameworks.html?version=270122', controller: controller }; return directive; function controller($scope, $element) { var controllerId = 'frameworkCtrl'; var getLogFn = common.logger.getLogFn; var log = getLogFn(controllerId); $scope.$on('resetframeworks', function () { $scope.chooseropen = false; $scope.frameworks = null; $scope.competencies = []; }); if ($scope.active) { getCompetencies($scope.active.id) } $scope.currentFramework = {}; if (!$scope.item.competencies) $scope.item.competencies = []; function getFrameworks() { //get the frameworks (ids) for the current user user.getAllMyFrameworks().then(function (theFrameworks) { var trimmedFrameworks = []; //trim decativated frameworks if required if ($scope.showOnlyActive) { for (var i = 0; i < theFrameworks.length; i++) { if (!theFrameworks[i].deactivated) trimmedFrameworks.push(theFrameworks[i]); } theFrameworks=trimmedFrameworks; } if (theFrameworks.length > 0) { //get the actual framework details for the framework id list frameworks.getFrameworkList(theFrameworks).then(function (data) { return setFrameworkData(data); }); } }); } $scope.getData = function () { if ($scope.frameworks || $scope.active) return; return getFrameworks(); } //set up the framework data function setFrameworkData(data) { if (data) { frameworks.formatFrameworkList(data).then(function(data) { $scope.frameworks = data; if ($scope.frameworks && $scope.frameworks.length > 0) { $scope.currentFramework.id = $scope.frameworks[0].id; $scope.currentFramework.deactivated = $scope.frameworks[0].deactivated; getCompetencies($scope.currentFramework.id); } }); } } //get the competencies for the framework function getCompetencies(frameworkId) { return frameworks.getCompetencies(false, frameworkId, false).then(function (data) { if (!data) { $scope.competencies = []; return; } $scope.competencies = [data]; associateCompetencies($scope.competencies); }); } //manage the competency for the item $scope.manageCompetency = function (competencyId) { var index = $scope.item.competencies.indexOf(competencyId); if (index > -1) $scope.item.competencies.splice(index, 1); else if(index==-1 && !$scope.currentFramework.deactivated) $scope.item.competencies.push(competencyId); associateCompetencies($scope.competencies); } //change to a new framework $scope.changeFramework = function () { for (var i = 0; i < $scope.frameworks.length; i++) { if ($scope.frameworks[i].id === $scope.currentFramework.id) { $scope.currentFramework.deactivated = $scope.frameworks[i].deactivated;unique break; } } getCompetencies($scope.currentFramework.id); } //work out which competencies are linked to the current item (so we can highlight em in the UI) function associateCompetencies(competencies) { if (!competencies) return; for (var i = 0; i < competencies.length; i++) { if (!competencies[i].linked) competencies[i].linked = false; competencies[i].linked = $scope.item.competencies.indexOf(competencies[i].id) > -1; if (competencies[i].childCompetencies && competencies[i].childCompetencies.length > 0) associateCompetencies(competencies[i].childCompetencies); } } } }]); app.directive('evernoteNoteChooser', ['evernote', function (evernote) { var directive = { restrict: 'E', scope: { item:'=' }, templateUrl: 'app/items/templates/evernotenotechooser.html?version=270122', controller: controller, link:link }; return directive; function controller($scope, $element) { $scope.getNotes=function(notebook) { evernote.getAllNotesForNotebook(notebook.id).then(function (notes) { $scope.notes = notes; }); } $scope.getFullNote=function(note) { evernote.getNote(note.id).then(function (fullNote) { $scope.fullNote = fullNote; $scope.item.title = fullNote.title; $scope.item.details = fullNote.textContent; }); } } function link($scope, element, $attrs) { evernote.getAllNotebooks().then(function (notebooks) { $scope.notebooks = notebooks; }); } }]); //download a file from Dropbox app.directive('dropboxDownload', function() { return { restrict: 'E', template: 'DownloadLog in to Dropbox first', scope: true, controller: [ '$scope', '$attrs', 'Dropbox', function ($scope, $attrs, Dropbox) { $scope.isAuthenticatedWithDropbox = Dropbox.isAuthenticated(); $scope.downloadDropboxFile = function () { var params = { arrayBuffer: true }; Dropbox.readFile($attrs.fileurl, params).then(function (response) { var blob = new Blob([response], { type: $attrs.filetype }); saveAs(blob, $attrs.filename); }); }; //auth with dropbox if the user isn't already $scope.authWithDropbox =function () { Dropbox.getAccessToken().then(function() { $scope.isAuthenticatedWithDropbox = true; }); }; } ] }; }); //embed a preview of the showcase app.directive('embedShowcase', ["$sce", "$timeout", function ($sce, $timeout) { "use strict"; //uses iframe to host the showcase in preview mode return { restrict: "E", template: '', scope: { height: "@", width: "@", scale:"@", scrolling:"@" }, link: function ($scope, element, $attrs) { $attrs.$observe('width', function (w) { $scope.width = w; }); $attrs.$observe('scrolling', function (sc) { $scope.scrolling = sc; }); $scope.transform = "scale("+$scope.scale+")"; $attrs.$observe('height', function (h) { $scope.height = h; }); $attrs.$observe('scale', function (s) { $scope.scale = s; }); //handle the use of both ng-href and href $attrs.$observe('href', function(url) { if (url === undefined) return; $scope.trustedSrc = $sce.trustAsResourceUrl(url); }); } } }]); //directive to handle the registration process app.directive('register', ['common', 'config', 'auth', 'validation', 'datacontext', 'fullScreenLoaderContext', function ( common, config, authService, validation, datacontext, fsl) { return { restrict: 'E', // transclude: true, templateUrl: "app/register/registerdirective.html?version=270122", scope: { emailDomains: '@', organisationId: '@' }, link: function ($scope, element, $attrs) { if (!$scope.emailDomains) { $scope.emailDomains = ''; } var splitEmailDomains = []; $scope.registerFailed = false; $scope.confirmed = false; $scope.showUserExists = false; $scope.user = { iAmRobot: false }; if ($scope.emailDomains) $scope.splitEmailDomains = $scope.emailDomains.split(","); $scope.validation = validation.userValidationConfig(); //get the relevant validation logic $scope.doesEmailExist = function (username) { //HTML encode the username so that e.g. '+' is handled correctly username = encodeURIComponent(username); datacontext.isEmailAvailable(username).then(function (data) { $scope.showUserExists = data; //Decode the email back again username = decodeURIComponent(username); }); } //is the email domain the user is trying to register with valid (e.g. a mmu user cannot register without an mmu address) $scope.isValidEmailDomain = function (email) { if (!$scope.emailDomains || $scope.emailDomains.length === 0) return true; if (!email) return true; if (email.indexOf("@") === -1) return true; if (email) { for (var i = 0; i < $scope.splitEmailDomains.length; i++) { if (email.endsWith($scope.splitEmailDomains[i])) return true; } } return false; } $scope.register = function () { fsl.show('Registering'); //robots tend to complete all form fields - this is a hidden field hence no real user would complete it hence if its complete it must be a robot //this is a good way to do this without impacting users, unlike captcha if ($scope.user.iAmRobot) return; $scope.user.email = $scope.user.email.toLowerCase(); $scope.user.emailConfirm = $scope.user.emailConfirm.toLowerCase(); //HTML encode the email so that symbols e.g. '+' are handled correctly var email = encodeURIComponent($scope.user.email); datacontext.isEmailAvailable(email).then(function (data) { $scope.showUserExists = data; if ($scope.registration.$invalid || $scope.showUserExists === true || $scope.showUserExists === undefined) return; if ($scope.organisationId) $scope.user.organisation = $scope.organisationId; $scope.registerFailed = false; authService.registerPersonal($scope.user) .then(function () { $scope.registerSucceeded = true; fsl.hide(); }) .catch(function(data, status) { //problem with registration so tell user if (data.data.errorCode === "0") $scope.showUserExists = true; else $scope.registerFailed = true; fsl.hide(); }); }); }; } }; }]); app.directive('preventEnterFireing', function () { return { link: function (scope, element, attrs) { element.keypress(function (e) { if (e.keyCode == 13) { e.preventDefault(); return; } }); } } }); app.directive('ngEnter', function() { return function(scope, element, attrs) { element.bind("keydown keypress", function(event) { if(event.which === 13) { scope.$apply(function(){ scope.$eval(attrs.ngEnter, {'event': event}); }); event.preventDefault(); } }); }; }); app.directive('httpPrefix', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, controller) { function ensureHttpPrefix(value) { // Need to add prefix if we don't have http:// prefix already AND we don't have part of it if (value && !/^(https?):\/\//i.test(value) && 'http://'.indexOf(value) !== 0 && 'https://'.indexOf(value) !== 0) { controller.$setViewValue('http://' + value); controller.$render(); return 'http://' + value; } else return value; } controller.$formatters.push(ensureHttpPrefix); controller.$parsers.splice(0, 0, ensureHttpPrefix); } }; }); //directive to display and handle social logins app.directive('socialLoginRegister', ['$sce', '$location', '$window', 'config', function ($sce, $location, $window, config) { return { restrict: 'E', transclude: true, templateUrl: "app/register/socialloginregister.html?version=270122", scope: { register: "@" }, link: function ($scope, element, $attrs) { //define the social logins $scope.loginbuttons = [ { LoginUrl: $sce.trustAsResourceUrl(config.realm + 'Account/ExternalLogin' + '?provider=Facebook&ReturnUrl=' + config.applicationId + '&state=' + $scope.state), CssClass: "facebook", Icon: "facebook", Name: "Facebook", Color: '#3b5998', }, { LoginUrl: $sce.trustAsResourceUrl(config.realm + 'Account/ExternalLogin' + '?provider=Google&ReturnUrl=' + config.applicationId + '&state=' + $scope.state), CssClass: "google", Icon: "google", Name: "Google", Color: '#db3236', } ]; //redirect the user to the social login $scope.redirect = function (button) { console.log($scope.state); $window.location.href = button.LoginUrl; } } }; }]); // Help Search Directive app.directive('helpsearch', ['helpCentreSearch', 'config', function (helpCentreSearch, config) { return { restrict: 'EA', scope: { helpquery: '@' }, templateUrl: 'app/help/helpsearch.html?version=270122', link: function ($scope, element, $attrs) { $scope.videoName = 'Adding stuff to Myshowcase.me'; $scope.settingsUrl = helpCentreSearch.settings.umbracoUrl; $scope.settingsUserType = helpCentreSearch.settings.userType; $scope.settingsArticleId = helpCentreSearch.settings.articleId; $scope.settingsProduct = config.productName; $scope.settingsVersion = config.productVersion; $scope.settingsShowResults = helpCentreSearch.settings.showResults; var timeTaken; $scope.search = function (query) { var d = new Date(); $scope.timeSearchTookStart = d.getTime(); helpCentreSearch.search(query, timeTaken).then(function (results, timeTaken) { $scope.results = results; $scope.currentArticle = $scope.results[0]; $scope.selectedIndex = 0; $scope.currentQuery = query; $scope.isBodyOpen = false; $scope.setResult = function (index) { $scope.currentArticle = $scope.results[index]; $scope.selectedIndex = index; } var d = new Date(); $scope.timeSearchTookEnd = d.getTime(); $scope.timeSearchTookEnd = $scope.timeSearchTookEnd - $scope.timeSearchTookStart }); }; $scope.resetResults = function (results) { $scope.results = null; }; $scope.changeSettings = function () { helpCentreSearch.settings.umbracoUrl = $scope.settingsUrl; helpCentreSearch.settings.userType = $scope.settingsUserType; helpCentreSearch.settings.articleId = $scope.settingsArticleId; helpCentreSearch.settings.product = product; helpCentreSearch.settings.version = version; helpCentreSearch.settings.showResults = $scope.settingsShowResults; } } }; }]); // Dock toolbar app.directive("dockToolbar", function ($window) { return function(scope, element, attrs) { angular.element($window).bind("scroll", function() { if (this.pageYOffset >= 38) element.addClass('toolbar-docked'); else element.removeClass('toolbar-docked'); }); }; }); // Dock Mystuff toolbar app.directive("dockMyStuffToolbar", function ($window) { return { restrict: 'A', scope: { isShowcaseView: '=', }, link: function (scope, element, attrs) { scope.$watch('isShowcaseView', function (isShowcaseView) { if (isShowcaseView) { angular.element($window).bind("scroll", function () { element.removeClass('toolbar-mystuff-docked'); element.removeClass('toolbar-mystuff-docked-showcase'); if (this.pageYOffset >= 38) { element.addClass('toolbar-mystuff-docked'); } else { element.removeClass('toolbar-mystuff-docked'); } }); } else { angular.element($window).bind("scroll", function () { if (this.pageYOffset >= 20) { element.addClass('toolbar-mystuff-docked-showcase'); } else { element.removeClass('toolbar-mystuff-docked-showcase'); } }); } }); } }; return function(scope, element, attrs) { angular.element($window).bind("scroll", function() { if (this.pageYOffset >= 110) element.addClass('toolbar-mystuff-docked'); else element.removeClass('toolbar-mystuff-docked'); }); }; }); app.directive("myStuffTransform", function ($window) { return { restrict: 'A', scope: { isShowcaseView: '=' }, link: function (scope, element, attrs) { scope.$watch('isShowcaseView', function (isShowcaseView) { if (isShowcaseView) { angular.element($window).bind("scroll", function () { element.removeClass('mystuff-margin'); element.removeClass('mystuff-margin-showcase-docked'); if (this.pageYOffset >= 38) { element.addClass('mystuff-margin'); } else { element.removeClass('mystuff-margin'); } }); } else { angular.element($window).bind("scroll", function () { if (this.pageYOffset >= 20) { element.addClass('mystuff-margin-showcase-docked'); } else { element.removeClass('mystuff-margin-showcase-docked'); } }); } }); } }; }); // sets the background image of an element to the url and covers, you can also specify height and width of the element //
app.directive('coverImg', function () { return function (scope, element, attrs) { attrs.$observe('coverImg', function (source) { element.css({ 'background-image': 'url(' + source + ')', 'background-size': 'cover' }); }); attrs.$observe('coverImgWidth', function (width) { //if attr is just number use px if (!isNaN(width)) { element.css({ 'width': width + 'px' }); } else { //all others have to be exact e.g. 50%, 5em, etc. element.css({ 'width': width }); } }); attrs.$observe('coverImgHeight', function (height) { if (!isNaN(height)) { element.css({ 'height': height + 'px' }); } else { element.css({ 'height': height }); } }); }; }); app.directive('removeTags', function () { return { restrict: 'AE', scope: { removeTags: '=' }, link: function (scope) { if (scope.removeTags == null) scope.removeTags = ''; scope.$watch('removeTags', function (newValue, oldValue) { scope.removeTags = String(scope.removeTags).replace(//gm, ''); }); } }; }); app.directive('mySrc', function() { return { restrict: 'A', scope: { mySrc:'@' }, link: function (scope, iElement, iAttrs) { scope.$watch('mySrc',function(){ var iFrame = iElement; if (!(iFrame && iFrame.length > 0)) { iFrame = $(''); iElement.append(iFrame); } iFrame.attr("src", scope.mySrc); }); } }; }); app.filter('Filesize', function () { return function (size) { if (isNaN(size)) size = 0; if (size < 1024) return size + ' Bytes'; size /= 1024; if (size < 1024) return size.toFixed(2) + ' KB'; size /= 1024; if (size < 1024) return size.toFixed(2) + ' MB'; size /= 1024; if (size < 1024) return size.toFixed(2) + ' GB'; size /= 1024; return size.toFixed(2) + ' TB'; }; }); app.directive('hoverPopover', function ($compile, $templateCache, $timeout, $rootScope) { return { restrict: 'A', link: function (scope, element, attrs) { scope.template = attrs.template; scope.getTemplate = function (contentType) { return $templateCache.get(scope.template); }; $rootScope.insidePopover = false; scope.content = scope.getTemplate(); scope.content = $compile(scope.content)(scope); var removed = false; $(element).popover({ content: scope.content, title: null, container: 'body', placement: 'right', trigger: 'manual', html: true }); $(element).bind('mouseenter', function (e) { scope.content = scope.getTemplate(); scope.content = $compile(scope.content)(scope); $(element).popover({ content: scope.content, title: null, container: 'body', placement: 'right', trigger: 'manual', html: true }); $(element).popover('show'); scope.attachEvents(element); }); $(element).bind('mouseleave', function (e) { $timeout(function () { if (!$rootScope.insidePopover) { $('.popover').remove(); removed = true; $templateCache.remove(scope.content) } }, 1); }); }, controller: function ($scope, $element) { $scope.attachEvents = function (element) { $('.popover').on('mouseenter', function () { $rootScope.insidePopover = true; }); $('.popover').on('mouseleave', function () { $rootScope.insidePopover = false; $('.popover').remove(); }); } } }; }); })();