Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. angular.module('cbuControllers').controller 'AccountListCtrl',
  2. [
  3. '$scope'
  4. '$q'
  5. '$state'
  6. '$stateParams'
  7. '$i18next'
  8. 'AccountService'
  9. 'cbServices'
  10. 'ErrorHandlerService'
  11. 'cbDefaultValues'
  12. ($scope, $q, $state, $stateParams, $i18n, accountService, services,
  13. ErrorHandlerService, cbDefaults) ->
  14.  
  15. $scope.showScreen = false
  16. Auth = services.get 'Auth'
  17. $scope.accounts = []
  18. $scope.search = {}
  19. $scope.start = 0
  20. $scope.limit = cbDefaults.recordLimit
  21. $scope.totalCount = 0
  22. $scope.sortBy = 'name'
  23. $scope.sortOrder = 'asc'
  24. $scope.table = false
  25. tableTpl =
  26. headers: ['name', 'account.linked_websites']
  27. columns: ['name', 'websites']
  28. order : 'name'
  29. rows : []
  30. meta : {
  31. sortBy : $scope.sortBy
  32. sortOrder : $scope.sortOrder
  33. limit : $scope.limit
  34. }
  35. active : false
  36.  
  37. # Validate if we have the appropriate mask to create an Account to
  38. # enable or not the Add Account button.
  39. if Auth.authorize 'account', Auth.masks.CREATE
  40. $scope.addAccount = () ->
  41. $state.go 'root.account.edit.new',
  42. us_hash: '_new_'
  43. else
  44. $scope.addAccount = false
  45.  
  46. $scope.message = ''
  47. if $stateParams.account != ''
  48. $scope.message = $stateParams.account
  49.  
  50. ###*
  51. * Displays error messages in the screen for users.
  52. * @param { Object } error
  53. ###
  54. handleErrors = (error) ->
  55. $scope.errorMessages = [ErrorHandlerService.parseAPIError(error)]
  56.  
  57. ###*
  58. * Perfoms the backend call for getting the list of accounts.
  59. ###
  60. reloadList = ()->
  61. unless $scope.start > $scope.totalCount
  62. query = {}
  63. query['search[name]'] = $scope.searchtext
  64. query['search[websites]'] = $scope.searchtext
  65. query['sort['+$scope.sortBy+']'] = $scope.sortOrder
  66. query.start = $scope.start
  67. query.limit = $scope.limit
  68.  
  69. accountService.getBy(query)
  70. .then (accounts) ->
  71.  
  72. if accounts._metadata
  73. $scope.totalCount = accounts._metadata.total_count
  74. tableTpl.meta = _.assign({}, tableTpl.meta,
  75. accounts._metadata,
  76. {sortBy: $scope.sortBy, sortOrder: $scope.sortOrder}
  77. )
  78. delete accounts._metadata
  79.  
  80. _.forEach accounts, (account) ->
  81. account.websites = ''
  82. _.forEach account.sources, (source) ->
  83. if source.source
  84. if account.websites.length
  85. account.websites += ', ' + source.source
  86. else
  87. account.websites += source.source
  88.  
  89. # start from 0 on new search or sortby
  90. if $scope.start is 0
  91. $scope.accounts = _.clone(accounts)
  92. else
  93. # load more... records at the bottom of the table
  94. [].push.apply $scope.accounts, accounts
  95.  
  96. # notify the component
  97. $scope.table = _.assign({}, tableTpl, {rows: $scope.accounts})
  98.  
  99. .catch (error) ->
  100. handleErrors(error)
  101.  
  102. ###*
  103. * Loads accounts into dropdown for filtering and preselects the default
  104. * account if you only have one.
  105. * @return { Promise } with accounts.
  106. ###
  107. loadAccounts = () ->
  108. deferred = $q.defer()
  109. accountService.ensureLoaded (accounts) ->
  110. deferred.resolve accounts
  111. deferred.promise
  112.  
  113. ###*
  114. * Loads everything necessary to get the screen correctly and
  115. * makes the first backend call for listing the websites.
  116. ###
  117. init = () ->
  118. loadAccounts()
  119. .then (accounts) ->
  120. if accounts.length is 1
  121. $state.go 'root.account.edit.viewone',
  122. us_hash: accounts[0].hash
  123. return
  124. $scope.showScreen = true
  125. $scope.title = $i18n 'account.list_accounts'
  126. $scope.search()
  127.  
  128. .catch (error) ->
  129. handleErrors(error)
  130.  
  131. ###*
  132. * Method fired on user clicks the search button.
  133. ###
  134. $scope.search = () ->
  135. $scope.start = 0
  136. reloadList()
  137.  
  138. ###*
  139. * Method fired on user sorting results.
  140. * @param { String } column
  141. ###
  142. $scope.sort = (column, order) ->
  143. $scope.sortBy = column
  144. $scope.sortOrder = order
  145. $scope.search()
  146.  
  147. ###*
  148. * Method fired on loading more records in the table paging component.
  149. ###
  150. $scope.nextPage = () ->
  151. $scope.start = $scope.start + $scope.limit
  152. reloadList()
  153.  
  154. ###*
  155. * Method fired on user clicking an account to view its details.
  156. * It redirects to account edit view.
  157. * @param { Object } account
  158. ###
  159. $scope.view = (account) ->
  160. $state.go 'root.account.edit.view',
  161. us_hash: account.hash
  162.  
  163. init()
  164. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement