PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/public/views/admin/users/details.js

https://gitlab.com/konforti/people
JavaScript | 539 lines | 491 code | 47 blank | 1 comment | 32 complexity | 8dcd428050852d7427b904e45fcb924e MD5 | raw file
  1. /* global app:true */
  2. (function() {
  3. 'use strict';
  4. app = app || {};
  5. app.User = Backbone.Model.extend({
  6. idAttribute: '_id',
  7. url: function() {
  8. return '/admin/users/'+ this.id +'/';
  9. }
  10. });
  11. app.Delete = Backbone.Model.extend({
  12. idAttribute: '_id',
  13. defaults: {
  14. success: false,
  15. errors: [],
  16. errfor: {}
  17. },
  18. url: function() {
  19. return '/admin/users/'+ app.mainView.model.id +'/';
  20. }
  21. });
  22. app.Identity = Backbone.Model.extend({
  23. idAttribute: '_id',
  24. defaults: {
  25. success: false,
  26. errors: [],
  27. errfor: {},
  28. isActive: '',
  29. username: '',
  30. email: ''
  31. },
  32. url: function() {
  33. return '/admin/users/'+ app.mainView.model.id +'/';
  34. },
  35. parse: function(response) {
  36. if (response.user) {
  37. app.mainView.model.set(response.user);
  38. delete response.user;
  39. }
  40. return response;
  41. }
  42. });
  43. app.Roles = Backbone.Model.extend({
  44. idAttribute: '_id',
  45. defaults: {
  46. success: false,
  47. errors: [],
  48. errfor: {},
  49. roles: [],
  50. newRole: ''
  51. },
  52. url: function() {
  53. return '/admin/users/'+ app.mainView.model.id +'/roles/';
  54. },
  55. parse: function(response) {
  56. if (response.user) {
  57. app.mainView.model.set(response.user);
  58. delete response.user;
  59. }
  60. return response;
  61. }
  62. });
  63. app.Password = Backbone.Model.extend({
  64. idAttribute: '_id',
  65. defaults: {
  66. success: false,
  67. errors: [],
  68. errfor: {},
  69. newPassword: '',
  70. confirm: ''
  71. },
  72. url: function() {
  73. return '/admin/users/'+ app.mainView.model.id +'/password/';
  74. },
  75. parse: function(response) {
  76. if (response.user) {
  77. app.mainView.model.set(response.user);
  78. delete response.user;
  79. }
  80. return response;
  81. }
  82. });
  83. app.Note = Backbone.Model.extend({
  84. idAttribute: '_id',
  85. defaults: {
  86. success: false,
  87. errors: [],
  88. data: '',
  89. userCreated: {}
  90. },
  91. url: function() {
  92. return '/admin/users/'+ app.mainView.model.id +'/notes/'+ (this.isNew() ? '' : this.id +'/');
  93. },
  94. parse: function(response) {
  95. if (response.account) {
  96. app.mainView.model.set(response.account);
  97. delete response.account;
  98. }
  99. return response;
  100. }
  101. });
  102. app.NoteCollection = Backbone.Collection.extend({
  103. model: app.Note
  104. });
  105. app.Status = Backbone.Model.extend({
  106. idAttribute: '_id',
  107. defaults: {
  108. success: false,
  109. errors: [],
  110. status: '',
  111. name: ''
  112. },
  113. url: function() {
  114. return '/admin/users/'+ app.mainView.model.id +'/status/'+ (this.isNew() ? '' : this.id +'/');
  115. },
  116. parse: function(response) {
  117. if (response.account) {
  118. app.mainView.model.set(response.account);
  119. delete response.account;
  120. }
  121. return response;
  122. }
  123. });
  124. app.StatusCollection = Backbone.Collection.extend({
  125. model: app.Status
  126. });
  127. app.HeaderView = Backbone.View.extend({
  128. el: '#header',
  129. template: _.template( $('#tmpl-header').html() ),
  130. initialize: function() {
  131. this.model = app.mainView.model;
  132. this.listenTo(this.model, 'change', this.render);
  133. this.render();
  134. },
  135. render: function() {
  136. this.$el.html(this.template( this.model.attributes ));
  137. }
  138. });
  139. app.IdentityView = Backbone.View.extend({
  140. el: '#identity',
  141. template: _.template( $('#tmpl-identity').html() ),
  142. events: {
  143. 'click .btn-update': 'update'
  144. },
  145. initialize: function() {
  146. this.model = new app.Identity();
  147. this.syncUp();
  148. this.listenTo(app.mainView.model, 'change', this.syncUp);
  149. this.listenTo(this.model, 'sync', this.render);
  150. this.render();
  151. },
  152. syncUp: function() {
  153. this.model.set({
  154. _id: app.mainView.model.id,
  155. isActive: app.mainView.model.get('isActive'),
  156. username: app.mainView.model.get('username'),
  157. email: app.mainView.model.get('email')
  158. });
  159. },
  160. render: function() {
  161. this.$el.html(this.template( this.model.attributes ));
  162. for (var key in this.model.attributes) {
  163. if (this.model.attributes.hasOwnProperty(key)) {
  164. this.$el.find('[name="'+ key +'"]').val(this.model.attributes[key]);
  165. }
  166. }
  167. },
  168. update: function() {
  169. var toSave = {};
  170. this.$el.find('.form-role input').each(function(i, el) {
  171. var key = el.name;
  172. var val = el.value;
  173. toSave[key] = val;
  174. });
  175. this.$el.find('.form-role select').each(function(i, el) {
  176. var key = el.name;
  177. var val = el.value;
  178. toSave[key] = val;
  179. });
  180. this.model.save(toSave);
  181. }
  182. });
  183. app.RolesView = Backbone.View.extend({
  184. el: '#roles',
  185. template: _.template( $('#tmpl-roles').html() ),
  186. events: {
  187. 'click .btn-add': 'add',
  188. 'click .btn-delete': 'delete',
  189. 'click .btn-save': 'saveRoles'
  190. },
  191. initialize: function() {
  192. this.model = new app.Roles();
  193. this.syncUp();
  194. this.listenTo(app.mainView.model, 'change', this.syncUp);
  195. this.listenTo(this.model, 'sync', this.render);
  196. this.render();
  197. },
  198. syncUp: function() {
  199. this.model.set({
  200. _id: app.mainView.model.id,
  201. roles: app.mainView.model.get('roles')
  202. });
  203. },
  204. render: function() {
  205. this.$el.html(this.template( this.model.attributes ));
  206. for (var key in this.model.attributes) {
  207. if (this.model.attributes.hasOwnProperty(key)) {
  208. this.$el.find('[name="'+ key +'"]').val(this.model.attributes[key]);
  209. }
  210. }
  211. },
  212. add: function() {
  213. var newRole = this.$el.find('[name="newRole"]').val();
  214. var newRoleName = this.$el.find('[name="newRole"] option:selected').text();
  215. if (!newRole) {
  216. alert('Please select a role.');
  217. return;
  218. }
  219. else {
  220. var alreadyAdded = false;
  221. _.each(this.model.get('roles'), function(role) {
  222. if (newRole === role._id) {
  223. alreadyAdded = true;
  224. }
  225. });
  226. if (alreadyAdded) {
  227. alert('That role already exists.');
  228. return;
  229. }
  230. }
  231. this.model.get('roles').push({ _id: newRole, name: newRoleName });
  232. var sorted = this.model.get('roles');
  233. sorted.sort(function(a, b) {
  234. return a.name.toLowerCase() > b.name.toLowerCase();
  235. });
  236. this.model.set('roles', sorted);
  237. this.render();
  238. },
  239. delete: function(event) {
  240. if (confirm('Are you sure?')) {
  241. var idx = this.$el.find('.btn-delete').index(event.currentTarget);
  242. this.model.get('roles').splice(idx, 1);
  243. this.render();
  244. }
  245. },
  246. saveRoles: function() {
  247. this.model.save();
  248. }
  249. });
  250. app.PasswordView = Backbone.View.extend({
  251. el: '#password',
  252. template: _.template( $('#tmpl-password').html() ),
  253. events: {
  254. 'click .btn-password': 'password'
  255. },
  256. initialize: function() {
  257. this.model = new app.Password({ _id: app.mainView.model.id });
  258. this.listenTo(this.model, 'sync', this.render);
  259. this.render();
  260. },
  261. render: function() {
  262. this.$el.html(this.template( this.model.attributes ));
  263. for (var key in this.model.attributes) {
  264. if (this.model.attributes.hasOwnProperty(key)) {
  265. this.$el.find('[name="'+ key +'"]').val(this.model.attributes[key]);
  266. }
  267. }
  268. },
  269. password: function() {
  270. this.model.save({
  271. newPassword: this.$el.find('[name="newPassword"]').val(),
  272. confirm: this.$el.find('[name="confirm"]').val()
  273. });
  274. }
  275. });
  276. app.DeleteView = Backbone.View.extend({
  277. el: '#delete',
  278. template: _.template( $('#tmpl-delete').html() ),
  279. events: {
  280. 'click .btn-delete': 'delete',
  281. },
  282. initialize: function() {
  283. this.model = new app.Delete({ _id: app.mainView.model.id });
  284. this.listenTo(this.model, 'sync', this.render);
  285. this.render();
  286. },
  287. render: function() {
  288. this.$el.html(this.template( this.model.attributes ));
  289. },
  290. delete: function() {
  291. if (confirm('Are you sure?')) {
  292. this.model.destroy({
  293. success: function(model, response) {
  294. if (response.success) {
  295. location.href = '/admin/users/';
  296. }
  297. else {
  298. app.deleteView.model.set(response);
  299. }
  300. }
  301. });
  302. }
  303. }
  304. });
  305. app.NewNoteView = Backbone.View.extend({
  306. el: '#notes-new',
  307. template: _.template( $('#tmpl-notes-new').html() ),
  308. events: {
  309. 'click .btn-add': 'addNew'
  310. },
  311. initialize: function() {
  312. this.model = new app.Note();
  313. this.listenTo(this.model, 'change', this.render);
  314. this.render();
  315. },
  316. render: function() {
  317. this.$el.html( this.template(this.model.attributes) );
  318. },
  319. validates: function() {
  320. var errors = [];
  321. if (this.$el.find('[name="data"]').val() === '') {
  322. errors.push('Please enter some notes.');
  323. }
  324. if (errors.length > 0) {
  325. this.model.set({ errors: errors });
  326. return false;
  327. }
  328. return true;
  329. },
  330. addNew: function() {
  331. if (this.validates()) {
  332. this.model.save({
  333. data: this.$el.find('[name="data"]').val()
  334. });
  335. }
  336. }
  337. });
  338. app.NoteCollectionView = Backbone.View.extend({
  339. el: '#notes-collection',
  340. template: _.template( $('#tmpl-notes-collection').html() ),
  341. initialize: function() {
  342. this.collection = new app.NoteCollection();
  343. this.syncUp();
  344. this.listenTo(app.mainView.model, 'change', this.syncUp);
  345. this.listenTo(this.collection, 'reset', this.render);
  346. this.render();
  347. },
  348. syncUp: function() {
  349. this.collection.reset(app.mainView.model.get('notes'));
  350. },
  351. render: function() {
  352. this.$el.html(this.template());
  353. var frag = document.createDocumentFragment();
  354. var last = document.createTextNode('');
  355. frag.appendChild(last);
  356. this.collection.each(function(model) {
  357. var view = new app.NotesItemView({ model: model });
  358. var newEl = view.render().el;
  359. frag.insertBefore(newEl, last);
  360. last = newEl;
  361. }, this);
  362. $('#notes-items').append(frag);
  363. if (this.collection.length === 0) {
  364. $('#notes-items').append( $('#tmpl-notes-none').html() );
  365. }
  366. }
  367. });
  368. app.NotesItemView = Backbone.View.extend({
  369. tagName: 'div',
  370. className: 'note',
  371. template: _.template( $('#tmpl-notes-item').html() ),
  372. render: function() {
  373. this.$el.html( this.template(this.model.attributes) );
  374. this.$el.find('.timeago').each(function(index, indexValue) {
  375. if (indexValue.innerText) {
  376. var myMoment = moment(indexValue.innerText);
  377. indexValue.innerText = myMoment.from();
  378. }
  379. });
  380. return this;
  381. }
  382. });
  383. app.NewStatusView = Backbone.View.extend({
  384. el: '#status-new',
  385. template: _.template( $('#tmpl-status-new').html() ),
  386. events: {
  387. 'click .btn-add': 'addNew'
  388. },
  389. initialize: function() {
  390. this.model = new app.Status();
  391. this.syncUp();
  392. this.listenTo(app.mainView.model, 'change', this.syncUp);
  393. this.listenTo(this.model, 'change', this.render);
  394. this.render();
  395. },
  396. syncUp: function() {
  397. this.model.set({
  398. id: app.mainView.model.get('status').id,
  399. name: app.mainView.model.get('status').name
  400. });
  401. },
  402. render: function() {
  403. this.$el.html( this.template(this.model.attributes) );
  404. if (app.mainView.model.get('status') && app.mainView.model.get('status').id) {
  405. this.$el.find('[name="status"]').val(app.mainView.model.get('status').id);
  406. }
  407. },
  408. validates: function() {
  409. var errors = [];
  410. if (this.$el.find('[name="status"]').val() === '') {
  411. errors.push('Please choose a status.');
  412. }
  413. if (this.$el.find('[name="status"]').val() === app.mainView.model.get('status').id) {
  414. errors.push('That is the current status.');
  415. }
  416. if (errors.length > 0) {
  417. this.model.set({ errors: errors });
  418. return false;
  419. }
  420. return true;
  421. },
  422. addNew: function() {
  423. if (this.validates()) {
  424. this.model.save({
  425. id: this.$el.find('[name="status"]').val(),
  426. name: this.$el.find('[name="status"] option:selected').text()
  427. });
  428. }
  429. }
  430. });
  431. app.StatusCollectionView = Backbone.View.extend({
  432. el: '#status-collection',
  433. template: _.template( $('#tmpl-status-collection').html() ),
  434. initialize: function() {
  435. this.collection = new app.StatusCollection();
  436. this.syncUp();
  437. this.listenTo(app.mainView.model, 'change', this.syncUp);
  438. this.listenTo(this.collection, 'reset', this.render);
  439. this.render();
  440. },
  441. syncUp: function() {
  442. this.collection.reset(app.mainView.model.get('statusLog'));
  443. },
  444. render: function() {
  445. this.$el.html( this.template() );
  446. var frag = document.createDocumentFragment();
  447. var last = document.createTextNode('');
  448. frag.appendChild(last);
  449. this.collection.each(function(model) {
  450. var view = new app.StatusItemView({ model: model });
  451. var newEl = view.render().el;
  452. frag.insertBefore(newEl, last);
  453. last = newEl;
  454. }, this);
  455. $('#status-items').append(frag);
  456. }
  457. });
  458. app.StatusItemView = Backbone.View.extend({
  459. tagName: 'div',
  460. className: 'status',
  461. template: _.template( $('#tmpl-status-item').html() ),
  462. render: function() {
  463. this.$el.html( this.template(this.model.attributes) );
  464. this.$el.find('.timeago').each(function(index, indexValue) {
  465. if (indexValue.innerText) {
  466. var myMoment = moment(indexValue.innerText);
  467. indexValue.innerText = myMoment.from();
  468. }
  469. });
  470. return this;
  471. }
  472. });
  473. app.MainView = Backbone.View.extend({
  474. el: '.page .container',
  475. initialize: function() {
  476. app.mainView = this;
  477. this.model = new app.User( JSON.parse( unescape($('#data-record').html())) );
  478. app.headerView = new app.HeaderView();
  479. app.identityView = new app.IdentityView();
  480. app.passwordView = new app.PasswordView();
  481. app.rolesView = new app.RolesView();
  482. app.newNoteView = new app.NewNoteView();
  483. app.notesCollectionView = new app.NoteCollectionView();
  484. app.newStatusView = new app.NewStatusView();
  485. app.statusCollectionView = new app.StatusCollectionView();
  486. app.deleteView = new app.DeleteView();
  487. }
  488. });
  489. $(document).ready(function() {
  490. app.mainView = new app.MainView();
  491. });
  492. }());