PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/balancer/static/js/accounting.js

https://gitlab.com/kammala/balancer
JavaScript | 266 lines | 236 code | 25 blank | 5 comment | 9 complexity | be11f3f681fa62fe17b3aa9d8477b613 MD5 | raw file
  1. var app = angular.module('accounting', ['ui.bootstrap'])
  2. .config(['$locationProvider', function($locationProvider) {
  3. $locationProvider.html5Mode(true);
  4. }]);
  5. angular.element(document).ready(function(){
  6. angular.bootstrap(angular.element('[ng-controller=donats_ctrl]'), ['accounting']);
  7. angular.element(window).on('popstate', function(e){
  8. console.log(e);
  9. })
  10. });
  11. var invite_modal_ctrl = function($scope, $modalInstance, $http, $filter) {
  12. $scope.invited_users = INVITED;
  13. $scope.ACCESS_TYPES = ACCESS_TYPES;
  14. $scope.get_users = function(val){
  15. return $http({method: 'GET', url: '/api/search_user/', params: {starts: val}})
  16. .then(function(data){
  17. var res = [],
  18. filtered = $filter('filter')(data.data, function(v) {
  19. return $filter('filter')(
  20. $scope.invited_users,
  21. function(u) { return u.id === v.id; }
  22. ).length === 0;
  23. });
  24. // should I use underscore.js?
  25. angular.forEach(filtered, function(v) {
  26. v.cancelable = true;
  27. v.modified = true;
  28. res.push(v);
  29. });
  30. return res;
  31. });
  32. };
  33. $scope.cancel_invite = function(user) {
  34. // should I use underscore.js?
  35. $scope.invited_users = $filter('filter')($scope.invited_users, function(v) {return v.id !== user.id});
  36. };
  37. $scope.disinvite = function(user) {
  38. $http.post('/api/accounting/'+ACCOUNTING+'/disinvite/', [user.id]);
  39. $scope.invited_users = $filter('filter')($scope.invited_users, function(v) {return v.id !== user.id});
  40. };
  41. $scope.ok = function() {
  42. var api_params = {};
  43. angular.forEach($filter('filter')($scope.invited_users, {modified: true}), function(val) {
  44. api_params[val.id] = val.access.id;
  45. });
  46. $http.post('/api/accounting/'+ACCOUNTING+'/invite/', api_params)
  47. .success(function(data){
  48. angular.forEach(data, function(user){
  49. $scope.invited_users = $filter('filter')($scope.invited_users, function(v) {return v.id !== user.id});
  50. });
  51. })
  52. .error(function(data, status){
  53. // todo
  54. console.log(status + ':' + data)
  55. });
  56. $modalInstance.close();
  57. };
  58. $scope.dismiss = function() {
  59. $modalInstance.dismiss('cancel');
  60. };
  61. };
  62. app.controller('donats_ctrl', function ($scope, $http, $filter, $modal, $location) {
  63. "use strict";
  64. $scope.to_unixtimestamp = function (dt) {
  65. if(typeof(dt) === 'undefined')
  66. return null;
  67. if(dt === null)
  68. return null;
  69. return dt.getTime() / 1000;
  70. };
  71. $scope.from_date_opened = false;
  72. $scope.to_date_opened = false;
  73. $scope.datepickerOptions = {
  74. 'show-weeks': false,
  75. 'starting-day': 1
  76. };
  77. // fixme: 4 identical functions
  78. $scope.open_from = function($event) {
  79. $event.preventDefault();
  80. $event.stopPropagation();
  81. $scope.to_date_opened = false;
  82. $scope.from_date_opened=!$scope.from_date_opened;
  83. };
  84. $scope.open_to = function($event) {
  85. $event.preventDefault();
  86. $event.stopPropagation();
  87. $scope.from_date_opened = false;
  88. $scope.to_date_opened=!$scope.to_date_opened;
  89. };
  90. $scope.open_date = function($event, donat) {
  91. $event.preventDefault();
  92. $event.stopPropagation();
  93. donat.until_opened = false;
  94. donat.date_opened = !donat.date_opened; }
  95. $scope.open_until = function($event, donat) {
  96. $event.preventDefault();
  97. $event.stopPropagation();
  98. donat.date_opened = false;
  99. donat.until_opened = !donat.until_opened;
  100. };
  101. $scope.acc_config = {
  102. list_id: ACCOUNTING,
  103. show_disabled: SHOW_DISABLED,
  104. start: START,
  105. stop: STOP,
  106. tags: TAGS,
  107. include_empty_tag: false
  108. };
  109. $scope.pagination = {
  110. max_size: 5,
  111. total: 0,
  112. page: 1,
  113. per_page: PER_PAGE
  114. };
  115. $scope.get_list = function () {
  116. var real_params = angular.copy($scope.acc_config);
  117. angular.extend(real_params, {
  118. start: $scope.to_unixtimestamp(real_params.start),
  119. stop: $scope.to_unixtimestamp(real_params.stop),
  120. limit: $scope.pagination.per_page,
  121. offset: ($scope.pagination.page - 1) * $scope.pagination.per_page
  122. });
  123. return $http({method: "GET", url: "/api/donation/", params: real_params})
  124. .then(function (promise_info) {
  125. var search = angular.copy($scope.acc_config);
  126. $scope.donats = promise_info.data.data;
  127. $scope.pagination.total = promise_info.data.count;
  128. angular.forEach($scope.donats, $scope.extend_donat);
  129. angular.extend(search, {
  130. page: $scope.pagination.page,
  131. start: $scope.acc_config.start === null ? '' : $filter('date')($scope.acc_config.start, 'yyyy-MM-dd'),
  132. stop: $scope.acc_config.stop === null ? '' : $filter('date')($scope.acc_config.stop, 'yyyy-MM-dd')
  133. });
  134. $location.search(search);
  135. });
  136. };
  137. $scope.get_list().then(function(){
  138. $scope.pagination.page = PAGE;
  139. });
  140. $scope.extend_donat = function (val, idx) {
  141. angular.extend(val, {
  142. js_date: new Date(val.date * 1000),
  143. edit_mode: false,
  144. edit_schedule: false,
  145. idx: idx,
  146. total: function () {
  147. var size = val.disabled ? 0 : val.size,
  148. is_last = val.idx + 1 === $scope.donats.length;
  149. return size + (is_last ? 0 : $scope.donats[val.idx + 1].total());
  150. },
  151. errors: null,
  152. old: null,
  153. date_opened: false,
  154. until_opened: false,
  155. js_until: val.until === null ? null : new Date(val.until * 1000)
  156. });
  157. };
  158. $scope.edit = function (d) {
  159. d.old = angular.copy(d);
  160. d.edit_schedule = false;
  161. d.edit_mode = true;
  162. };
  163. $scope.remove = function (d) {
  164. var need_to_delete = window.confirm('Really delete?');
  165. if (!need_to_delete)
  166. return;
  167. $http.delete("/api/donation/" + d.id + '/').success(function () {
  168. $scope.donats.splice($scope.donats.indexOf(d), 1);
  169. angular.forEach($scope.donats, function(val, idx) {
  170. val.idx = idx;
  171. });
  172. }).error(function(data, status){
  173. if (status === 400)
  174. $scope.errors = data;
  175. });
  176. };
  177. $scope.commit = function(d) {
  178. var sender = null;
  179. if (d.id === null)
  180. sender = function(params) {
  181. params.list_id = ACCOUNTING;
  182. return $http.post("/api/donation/", params);
  183. };
  184. else
  185. sender = function(params) {
  186. params.list_id = ACCOUNTING;
  187. return $http.put("/api/donation/"+ d.id+'/', params);
  188. };
  189. sender(d).success(function(data){
  190. angular.extend(d, data);
  191. if(d.old.date !== d.date || d.old.period !== d.period )
  192. $scope.get_list();
  193. d.old = null;
  194. d.errors = null;
  195. d.edit_mode = false;
  196. })
  197. .error(function(data, status) {
  198. if (status === 400)
  199. d.errors = data;
  200. else
  201. $scope.errors = data;
  202. });
  203. };
  204. $scope.rollback = function(d) {
  205. if (d.id === null)
  206. {
  207. $scope.donats.splice(d.idx, 1);
  208. }
  209. else
  210. {
  211. angular.extend(d, d.old);
  212. d.old = null;
  213. d.errors = null;
  214. }
  215. };
  216. $scope.add_row = function() {
  217. var new_donat = angular.copy(DONATION_STRUCTURE);
  218. $scope.donats.push(new_donat);
  219. $scope.extend_donat(new_donat, $scope.donats.length-1);
  220. new_donat.edit_mode = true;
  221. };
  222. $scope.remove_tag = function(tags, tag) {
  223. tags.splice(tags.indexOf(tag), 1);
  224. };
  225. $scope.get_tags = function(val){
  226. return $http({method: 'GET', url: '/api/search_tag/', params: {starts: val}})
  227. .then(function(data){
  228. var res = [];
  229. angular.forEach(data.data, function(item){res.push(item.name);});
  230. return res;
  231. });
  232. };
  233. // invite
  234. $scope.open_invite_modal = function(){
  235. $modal.open({
  236. templateUrl: 'invite_modal',
  237. controller: invite_modal_ctrl,
  238. size: 'lg'
  239. });
  240. };
  241. });