(function() { 'use strict'; angular .module('equis') .directive('ngCallback', ChooserCallback) .controller('GenericChooser', GenericChooser) .controller('WidgetChooser', WidgetChooser) .controller('DashboardChooser', DashboardChooser) .controller('FacilityChooser', FacilityChooser) .controller('ReportChooser', ReportChooser); function ChooserCallback() { return function(scope, element, attrs) { if (scope.$parent.generic) { scope.$parent.generic.callback = function(object) { scope.$eval(attrs.ngCallback, object); } } } } GenericChooser.$inject = ['$http', '$scope', '$attrs', 'favoriteService', 'recentService']; FacilityChooser.$inject = ['$scope', '$attrs', '$http']; ReportChooser.$inject = ['$scope', '$attrs', '$http']; DashboardChooser.$inject = ['$scope', '$attrs', '$http']; function GenericChooser($http, $scope, $attrs, favoriteService, recentService) { var vm = this; vm.browseUrl = 'explorer/browse'; vm.searchUrl = 'explorer/search'; vm.favoriteUrl = 'explorer/favorite'; vm.searchString = ''; vm.objects = []; vm.breadcrumbs = [{ Code: '', Name: '..', thumbUrl: 'assets/images/folder-light.svg', getThumb: function () { return 'assets/images/folder-light.svg'; }, isFavorite: false, selected: false, Type: 'group_code' }]; vm.showModal = false; vm.tileView = true; vm.browseView = true; vm.detailsView = false; vm.recentView = false; vm.favoriteView = false; vm.objectFilter = { Type: '!group_code' }; vm.browseFilter = {}; vm.browseStartFolder = null; vm.folderIcon = 'assets/images/folder-grey.svg'; vm.folderIconLight = 'assets/images/folder-light.svg'; vm.$scope = $scope; if ($attrs.userid) { vm.userId = $attrs.userid; } vm.toggleFavorite = function(object) { favoriteService.toggleFavorite(object); }; vm.addRecent = function (object) { recentService.addRecent(object); } vm.toggleModal = function () { vm.showModal = !vm.showModal; if (vm.showModal) { $('body').addClass('overflow-hidden'); if (vm.objects.length === 0) vm.loadTiles(); } else { $('body').removeClass('overflow-hidden'); } }; vm.loadTiles = function(search, parent) { var filters = { ItemType: vm.item_type }; var url = vm.browseUrl; if (vm.recentView || vm.favoriteView) { filters.IsRecent = vm.recentView; filters.IsFavorite = vm.favoriteView; if (typeof(search) == 'undefined' || search === '') search = "%%%"; } if (typeof (search) != 'undefined' && search !== '') { filters.Query = search; url = vm.searchUrl; } if (typeof (parent) != 'undefined') { if (parent.Code !== '') filters.Group = { Code: parent.Code, Id: parent.Id }; } else if (vm.browseStartFolder != null) // if no parent passed in (root folder), but start folder has been defined. filters.Group = vm.browseStartFolder; if (typeof(vm.userId) != 'undefined' && vm.userId != '') { filters.UserId = vm.userId; } var promise = $http.post(url, filters); promise.success(function(data, status, headers, config) { //clear objects vm.objects = []; for (var i = 0; i < data.length; i++) { var o = data[i]; o.selected = o.Id === vm.selectedValue; o.isFavorite = function() { return this.IsFavorite; }; o.select = function ($event) { //FB.117435 if (parent != null) { if (parent.Code === '#byfacility') { vm.breadcrumbs.push(this); vm.loadTiles('facility_id:' + this.Id); return; } else if (parent.Code === '#byuser') { vm.breadcrumbs.push(this); vm.loadTiles('user_id:' + this.Id); return; } else if (parent.Code === '#byreport') { vm.breadcrumbs.push(this); vm.loadTiles('report_id:' + this.Id); return; } } //here this refers to the object vm.selectedValue = this.Id; vm.selectedObject = this; if (this.Type !== 'group_code') { if (!$event.ctrlKey && vm.item_type !== 'dashboard_id') vm.toggleModal(); } else { //if group vm.breadcrumbs.push(this); vm.loadTiles('', this); return; } if (typeof (vm.item_action) == 'function') { vm.item_action(this, $event); } if (typeof(vm.callback) === 'function') { vm.callback(this, $event); } }; o.selectGroupAsItem = function($event) { if (vm.item_type == 'facility_id' && typeof(vm.group_action) == 'function' && this.FacilityId) { vm.toggleModal(); vm.selectedValue = this.FacilityId; vm.selectedObject = this; vm.group_action(this, $event); if (typeof (vm.callback) === 'function') { vm.callback(this); } } }; //FB.110661: create tooltip for object o.tooltip = getObjectTooltip(o); vm.objects.push(o); } }); promise.error(function(data, status, headers, config) { vm.error = 'AJAX failed'; }); }; vm.showBrowseView = function() { vm.browseView = true; vm.tileView = false; vm.recentView = false; vm.favoriteView = false; vm.detailsView = true; delete vm.objectFilter.IsFavorite; delete vm.browseFilter.IsFavorite; vm.loadTiles(); }; vm.showTileView = function() { vm.detailsView = false; vm.tileView = true; }; vm.showGridView = function() { vm.detailsView = true; vm.tileView = false; }; vm.showRecentView = function() { vm.showTileView(); vm.recentView = true; vm.favoriteView = false; vm.browseView = false; delete vm.objectFilter.IsFavorite; delete vm.browseFilter.IsFavorite; vm.loadTiles(); }; vm.showFavoritesView = function() { vm.showTileView(); vm.favoriteView = true; vm.recentView = false; vm.browseView = false; vm.objectFilter.IsFavorite = true; vm.browseFilter.IsFavorite = true; vm.loadTiles(); }; vm.showDetailsView = function() { vm.tileView = false; vm.detailsView = true; delete vm.objectFilter.IsFavorite; }; vm.search = function() { vm.loadTiles(vm.searchString); }; vm.navigate = function (el) { var i = el.$index; if (i >= 0 && i < vm.breadcrumbs.length) { vm.breadcrumbs.splice(i + 1, vm.breadcrumbs.length - (i + 1)); vm.loadTiles('', vm.breadcrumbs[vm.breadcrumbs.length - 1]); } }; vm.clearSelection = function($event) { vm.selectedObject = {}; vm.selectedValue = -1; if (vm.item_type === 'facility_id') { vm.item_action({ Id: -1 }, $event); } if (typeof (vm.callback) === 'function') { vm.callback({}, $event); } if ($event) vm.toggleModal(); }; } function WidgetChooser($scope) { var vm = this; vm.title = 'Widgets'; vm.item_type = 'widget_type_id'; vm.chooser_type = 'Widget'; vm.buttonName = 'Add Widget'; vm.headerImage = 'assets/images/widget_light.svg'; vm.headerImageAlt = 'assets/images/widget_light.svg'; vm.buttonImage = 'App_Themes/Blue/Images/add_widget.png'; vm.buttonImageAlt = 'App_Themes/Blue/Images/add_widget.png'; vm.showSelection = false; $scope.generic.thumbUrl = 'assets/images/widget.svg'; $scope.generic.item_type = 'widget_type_id'; $scope.generic.item_action = function (item) { __doPostBack('addWidget', item.Id); } } function DashboardChooser($scope, $attrs, $http) { var vm = this; vm.title = 'Dashboards'; vm.item_type = 'dashboard_id'; vm.chooserType = 'Dashboard'; vm.defaultButtonName = 'Dashboards'; vm.buttonName = vm.defaultButtonName; vm.headerImage = 'assets/images/dashboard_light.svg'; vm.buttonImage = 'assets/images/dashboard_dark.svg'; vm.buttonImageAlt = 'assets/images/dashboard_dark.svg'; vm.showSelection = false; $scope.generic.thumbUrl = 'assets/images/dashboard_light.svg'; $scope.generic.item_type = 'dashboard_id'; if ($attrs.value && $attrs.value !== "0" && $attrs.value !== "") { $scope.generic.selectedValue = $attrs.value; var p = $http.get('explorer/item/' + $attrs.value); p.success(function(object) { $scope.generic.selectedObject = object; vm.buttonName = object.Name; }); } if ($attrs.showclearbutton === 'True') { vm.showClearButton = true; } $scope.generic.item_action = function (item, $event) { if ($attrs.navigate !== 'False') { var url = "default.aspx?d=" + item.Id; if ($event.ctrlKey) { window.open(url); } else { //FB.116023: clear filters before switching dashboards EQuIS.clearFilters({ facility_id: false }); window.location = url; } } else { vm.buttonName = item.Name; vm.selectedValue = item.Id; vm.selectedObject = item; $scope.generic.toggleModal(); } } } function FacilityChooser($scope, $attrs, $http) { var vm = this; vm.title = 'Facilities'; vm.item_type = 'facility_id'; vm.displayColumn = $attrs.displaycolumn; vm.chooserType = 'Facility'; vm.defaultButtonName = 'Select Facility'; vm.buttonName = vm.defaultButtonName; vm.headerImage = 'assets/images/facility_light.svg'; vm.headerImageAlt = 'assets/images/facility_light.svg'; vm.buttonImage = 'assets/images/facility_dark.svg'; vm.buttonImageAlt = 'assets/images/facility_dark.svg'; vm.showSelection = true; $scope.generic.item_type = 'facility_id'; $scope.generic.set_value = function (value) { try { $scope.generic.selectedValue = value; var id = parseInt(value); if (id === -1 || id === 0) { $scope.generic.clearSelection(null); return; } var url = "facility/" + value; if (id < -1) url = "facility/group/" + value; var promise = $http.get(url); promise.success(function (item) { $scope.generic.selectedObject = item; if (item == null || item.length == 0 || item.Id == -1) { $scope.generic.clearSelection(null); } else { vm.buttonName = item[vm.displayColumn]; if (item.Type == 'group_code') { $scope.generic.group_action(item); } else { $scope.generic.item_action(item); } if (typeof ($scope.generic.callback) === 'function') { $scope.generic.callback(item); } } }); } catch (ex) { console.error(ex.message); } }; $scope.generic.item_action = function (item, $event) { try { if (item == null || Object.getOwnPropertyNames(item).length === 0 || item.Id == -1) $.extend(item, { Code: vm.defaultButtonName, Name: vm.defaultButtonName }); vm.buttonName = item[vm.displayColumn]; // item.Name; vm.selectedValue = item.Id; vm.selectedObject = item; vm.buttonImage = 'assets/images/facility_dark.svg'; if ($event && item.Id !== -1) $scope.generic.addRecent(item); } catch (ex) { console.error(ex.message); } }; // FB.110661: this method is for selecting facility groups $scope.generic.group_action = function (group, $event) { try { vm.buttonName = group.Name; vm.selectedValue = group.FacilityId; vm.selectedObject = group; vm.buttonImage = 'assets/images/facility_group_dark.svg'; //FB.116023: settings groups as "recent" does not quite work //if ($event && group.Id !== -1) $scope.generic.addRecent(group); } catch (ex) { console.error(ex.message); } }; // event listers for updating chooser (either facility selection or starting group in browse) // this event listener will change the chooser selection when triggered. $scope.$on('facilityChooserUpdateSelection', function (event, facility) { if (facility && typeof(facility.Id) != 'undefined' && typeof(facility.Name) != 'undefined') { if (facility.Id == null || facility.Id == '' || facility.Id.toString() == "-1") { // unsetting facility. facility.Id = ''; facility.Name = 'Select Facility'; } $scope.generic.item_action({ Id: facility.Id, Name: facility.Name }); $scope.$apply(); } }); // this event listener will set the browse view start folder when triggered. $scope.$on('facilityChooserSetStartFolder', function (event, folderItem) { $scope.$parent.generic.objects = []; // need to have the chooser reload fresh. $scope.$parent.generic.breadcrumbs.splice(1, $scope.$parent.generic.breadcrumbs.length -1); // reset breadcrumbs to root folder. if (folderItem) { $scope.$parent.generic.browseStartFolder = folderItem; // set starting folder $scope.$parent.generic.breadcrumbs.push(folderItem); // append start folder to breadcrumbs. } else // back to root folder $scope.$parent.generic.browseStartFolder = null; }); // look for pre-selected value if ($attrs.value && $attrs.value !== "0" && $attrs.value !== "") { var facId = $attrs.value; if ($attrs.valuepriority === "session") { // if there is a session value, use it var filters = EQuIS.getFilters(); var facilityId = parseInt(filters.facility_id); if (!isNaN(facilityId) && facilityId !== 0) facId = facilityId.toString(); } $scope.generic.set_value(facId); } } function ReportChooser($scope, $attrs, $http) { var vm = this; vm.title = 'Reports'; vm.item_type = 'report_id'; vm.chooserType = 'Report'; vm.defaultButtonName = 'Select Report'; vm.buttonName = vm.defaultButtonName; vm.headerImage = 'assets/images/report_light.svg'; vm.headerImageAlt = 'assets/images/userreport_light.svg'; vm.buttonImage = 'assets/images/report_dark.svg'; vm.buttonImageAlt = 'assets/images/userreport_dark.svg'; vm.showSelection = true; $scope.generic.item_type = 'report_id'; if ($attrs.value && $attrs.value !== "0" && $attrs.value !== "") { $scope.generic.selectedValue = $attrs.value; var promise = $http.get('report/' + $attrs.value); promise.success(function (report) { $scope.generic.selectedObject = report; vm.buttonName = report.Name; }); } $scope.generic.item_action = function(item) { vm.buttonName = item.Name; vm.selectedValue = item.Id; vm.selectedObject = item; $scope.generic.addRecent(item); }; vm.saveDialog = function (event, output) { if (output.hasOwnProperty('reportId')) { var item = { Name: output.reportName, Id: output.reportId }; $scope.generic.selectedObject = item; vm.buttonName = output.reportName; $scope.$apply(); $scope.generic.callback(item); } }; vm.openEditor = function() { EQuIS.openDialog({ title: $scope.generic.selectedObject.Name, dialog: 'Controls/ReportParametersEditor/ReportParametersEditor.ascx', params: { report_id: $scope.generic.selectedObject.Id }, onsave: function(event, output) { vm.saveDialog(event, output); } }); }; } /* Create the tooltip for a given object */ function getObjectTooltip (ob) { var tip = ""; try { tip = tip + 'ID: ' + Math.abs(ob.Id).toString() + "\r"; if (ob.Code && ob.Code.length > 0 && ob.Code != ob.Name) { tip = tip + 'Code: ' + ob.Code.toString() + "\r"; } tip = tip + 'Name: ' + ob.Name.toString() + "\r"; if (ob.Remark && ob.Remark.length > 0) { tip = tip + 'Remark: ' + ob.Remark.toString() + "\r"; } if (ob.Owner && ob.Owner.length > 0) { tip = tip + 'Owner: ' + ob.Owner.toString() + "\r"; } } catch (ex) { console.warn(ex.message); } return tip; } })();