|
| 1 | +(function(){ |
| 2 | + |
| 3 | + function isButton(item) { |
| 4 | + return item.type === 'button'; |
| 5 | + } |
| 6 | + |
| 7 | + function isButtonTwoState(item) { |
| 8 | + return item.type === 'buttonTwoState' && item.id.indexOf('view') === -1; |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Private method for subscribing to rxSubject. |
| 13 | + * For success functuon @see ToolbarController#onRowSelect() |
| 14 | + */ |
| 15 | + function subscribeToSubject() { |
| 16 | + ManageIQ.angular.rxSubject.subscribe(function(event) { |
| 17 | + if (event.rowSelect) { |
| 18 | + this.onRowSelect(event.rowSelect); |
| 19 | + } else if (event.redrawToolbar) { |
| 20 | + this.onUpdateToolbar(event.redrawToolbar); |
| 21 | + } else if (event.update) { |
| 22 | + this.onUpdateItem(event); |
| 23 | + } |
| 24 | + }.bind(this), |
| 25 | + function (err) { |
| 26 | + console.error('Angular RxJs Error: ', err); |
| 27 | + }, |
| 28 | + function () { |
| 29 | + console.debug('Angular RxJs subject completed, no more events to catch.'); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Private method for setting rootPoint of MiQEndpointsService. |
| 35 | + * @param MiQEndpointsService service responsible for endpoits. |
| 36 | + */ |
| 37 | + function initEndpoints(MiQEndpointsService) { |
| 38 | + var urlPrefix = '/' + location.pathname.split('/')[1]; |
| 39 | + if (urlPrefix) { |
| 40 | + MiQEndpointsService.rootPoint = urlPrefix; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructor of angular's miqToolbarController. |
| 46 | + * @param MiQToolbarSettingsService toolbarSettings service from ui-components. |
| 47 | + * @param MiQEndpointsService endpoits service from ui-components. |
| 48 | + * @param $scope service for managing $scope (for apply and digest reasons). |
| 49 | + * @param $location service for managing browser's location. |
| 50 | + * this contructor will assign all params to `this`, it will init endpoits, set if toolbar is used on list page. |
| 51 | + */ |
| 52 | + var ToolbarController = function(MiQToolbarSettingsService, MiQEndpointsService, $scope, $location) { |
| 53 | + this.MiQToolbarSettingsService = MiQToolbarSettingsService; |
| 54 | + this.MiQEndpointsService = MiQEndpointsService; |
| 55 | + this.$scope = $scope; |
| 56 | + this.$location = $location; |
| 57 | + initEndpoints(this.MiQEndpointsService); |
| 58 | + this.isList = _.contains(location.pathname, 'show_list'); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Public method which is executed after row in gtl is selected. |
| 63 | + */ |
| 64 | + ToolbarController.prototype.onRowSelect = function(data) { |
| 65 | + this.MiQToolbarSettingsService.checkboxClicked(data.checked); |
| 66 | + if(!this.$scope.$$phase) { |
| 67 | + this.$scope.$digest(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Public method for setting up url of data views, based on last path param (e.g. /show_list). |
| 73 | + */ |
| 74 | + ToolbarController.prototype.defaultViewUrl = function() { |
| 75 | + this.dataViews.forEach(function(item) { |
| 76 | + if (item.url === "") { |
| 77 | + var lastSlash = location.pathname.lastIndexOf('/'); |
| 78 | + item.url = (lastSlash !== -1) ? location.pathname.substring(lastSlash): ""; |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Method which will retrieves toolbar settings from server. |
| 85 | + * @see MiQToolbarSettingsService#getSettings for more info. |
| 86 | + * Settings is called with this.isList and $location search object with value of `type`. |
| 87 | + * No need to worry about multiple search params and no complicated function for parsing is needed. |
| 88 | + */ |
| 89 | + ToolbarController.prototype.fetchData = function(getData) { |
| 90 | + return this.MiQToolbarSettingsService |
| 91 | + .getSettings(getData) |
| 92 | + .then(function(toolbarItems) { |
| 93 | + this.toolbarItems = toolbarItems.items; |
| 94 | + this.dataViews = toolbarItems.dataViews; |
| 95 | + }.bind(this)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * |
| 100 | + */ |
| 101 | + ToolbarController.prototype.setClickHandler = function() { |
| 102 | + var buttons = _ |
| 103 | + .chain(this.toolbarItems) |
| 104 | + .flatten() |
| 105 | + .map(function(item) { |
| 106 | + return (item && item.hasOwnProperty('items')) ? item.items : item; |
| 107 | + }) |
| 108 | + .flatten() |
| 109 | + .filter(function(item){ |
| 110 | + return item.type && |
| 111 | + (isButton(item) || isButtonTwoState(item)) |
| 112 | + }) |
| 113 | + .each(function(item) { |
| 114 | + item.eventFunction = function($event) { |
| 115 | + miqToolbarOnClick.bind($event.delegateTarget)($event); |
| 116 | + } |
| 117 | + }) |
| 118 | + .value(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Public method for changing view over data. |
| 123 | + */ |
| 124 | + ToolbarController.prototype.onViewClick = function(item, $event) { |
| 125 | + if (item.url.indexOf('/') === 0) { |
| 126 | + var tail = (ManageIQ.record.recordId) ? ManageIQ.record.recordId : ''; |
| 127 | + location.replace('/' + ManageIQ.controller + item.url + tail + item.url_parms); |
| 128 | + } else { |
| 129 | + miqToolbarOnClick.bind($event.delegateTarget)($event); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + ToolbarController.prototype.initObject = function(toolbarString) { |
| 134 | + subscribeToSubject.bind(this)(); |
| 135 | + this.updateToolbar(JSON.parse(toolbarString)); |
| 136 | + } |
| 137 | + |
| 138 | + ToolbarController.prototype.onUpdateToolbar = function(toolbarObject) { |
| 139 | + this.updateToolbar(toolbarObject); |
| 140 | + if(!this.$scope.$$phase) { |
| 141 | + this.$scope.$digest(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + ToolbarController.prototype.onUpdateItem = function(updateData) { |
| 146 | + _.find(_.flatten(this.toolbarItems), {id: updateData.update})[updateData.type] = updateData.value; |
| 147 | + if(!this.$scope.$$phase) { |
| 148 | + this.$scope.$digest(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + ToolbarController.prototype.updateToolbar = function(toolbarObject) { |
| 153 | + toolbarItems = this.MiQToolbarSettingsService.generateToolbarObject(toolbarObject); |
| 154 | + this.toolbarItems = toolbarItems.items; |
| 155 | + this.dataViews = toolbarItems.dataViews; |
| 156 | + this.defaultViewUrl(); |
| 157 | + this.setClickHandler(); |
| 158 | + } |
| 159 | + |
| 160 | + ToolbarController.$inject = ['MiQToolbarSettingsService', 'MiQEndpointsService', '$scope', '$location']; |
| 161 | + miqHttpInject(angular.module('ManageIQ.toolbar')) |
| 162 | + .controller('miqToolbarController', ToolbarController); |
| 163 | +})(); |
0 commit comments