Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('cbuControllers').controller 'AccountListCtrl',
- [
- '$scope'
- '$q'
- '$state'
- '$stateParams'
- '$i18next'
- 'AccountService'
- 'cbServices'
- 'ErrorHandlerService'
- 'cbDefaultValues'
- ($scope, $q, $state, $stateParams, $i18n, accountService, services,
- ErrorHandlerService, cbDefaults) ->
- $scope.showScreen = false
- Auth = services.get 'Auth'
- $scope.accounts = []
- $scope.search = {}
- $scope.start = 0
- $scope.limit = cbDefaults.recordLimit
- $scope.totalCount = 0
- $scope.sortBy = 'name'
- $scope.sortOrder = 'asc'
- $scope.table = false
- tableTpl =
- headers: ['name', 'account.linked_websites']
- columns: ['name', 'websites']
- order : 'name'
- rows : []
- meta : {
- sortBy : $scope.sortBy
- sortOrder : $scope.sortOrder
- limit : $scope.limit
- }
- active : false
- # Validate if we have the appropriate mask to create an Account to
- # enable or not the Add Account button.
- if Auth.authorize 'account', Auth.masks.CREATE
- $scope.addAccount = () ->
- $state.go 'root.account.edit.new',
- us_hash: '_new_'
- else
- $scope.addAccount = false
- $scope.message = ''
- if $stateParams.account != ''
- $scope.message = $stateParams.account
- ###*
- * Displays error messages in the screen for users.
- * @param { Object } error
- ###
- handleErrors = (error) ->
- $scope.errorMessages = [ErrorHandlerService.parseAPIError(error)]
- ###*
- * Perfoms the backend call for getting the list of accounts.
- ###
- reloadList = ()->
- unless $scope.start > $scope.totalCount
- query = {}
- query['search[name]'] = $scope.searchtext
- query['search[websites]'] = $scope.searchtext
- query['sort['+$scope.sortBy+']'] = $scope.sortOrder
- query.start = $scope.start
- query.limit = $scope.limit
- accountService.getBy(query)
- .then (accounts) ->
- if accounts._metadata
- $scope.totalCount = accounts._metadata.total_count
- tableTpl.meta = _.assign({}, tableTpl.meta,
- accounts._metadata,
- {sortBy: $scope.sortBy, sortOrder: $scope.sortOrder}
- )
- delete accounts._metadata
- _.forEach accounts, (account) ->
- account.websites = ''
- _.forEach account.sources, (source) ->
- if source.source
- if account.websites.length
- account.websites += ', ' + source.source
- else
- account.websites += source.source
- # start from 0 on new search or sortby
- if $scope.start is 0
- $scope.accounts = _.clone(accounts)
- else
- # load more... records at the bottom of the table
- [].push.apply $scope.accounts, accounts
- # notify the component
- $scope.table = _.assign({}, tableTpl, {rows: $scope.accounts})
- .catch (error) ->
- handleErrors(error)
- ###*
- * Loads accounts into dropdown for filtering and preselects the default
- * account if you only have one.
- * @return { Promise } with accounts.
- ###
- loadAccounts = () ->
- deferred = $q.defer()
- accountService.ensureLoaded (accounts) ->
- deferred.resolve accounts
- deferred.promise
- ###*
- * Loads everything necessary to get the screen correctly and
- * makes the first backend call for listing the websites.
- ###
- init = () ->
- loadAccounts()
- .then (accounts) ->
- if accounts.length is 1
- $state.go 'root.account.edit.viewone',
- us_hash: accounts[0].hash
- return
- $scope.showScreen = true
- $scope.title = $i18n 'account.list_accounts'
- $scope.search()
- .catch (error) ->
- handleErrors(error)
- ###*
- * Method fired on user clicks the search button.
- ###
- $scope.search = () ->
- $scope.start = 0
- reloadList()
- ###*
- * Method fired on user sorting results.
- * @param { String } column
- ###
- $scope.sort = (column, order) ->
- $scope.sortBy = column
- $scope.sortOrder = order
- $scope.search()
- ###*
- * Method fired on loading more records in the table paging component.
- ###
- $scope.nextPage = () ->
- $scope.start = $scope.start + $scope.limit
- reloadList()
- ###*
- * Method fired on user clicking an account to view its details.
- * It redirects to account edit view.
- * @param { Object } account
- ###
- $scope.view = (account) ->
- $state.go 'root.account.edit.view',
- us_hash: account.hash
- init()
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement