projects.controller.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. Template.projects.helpers({
  2. projects: function () {
  3. return ProjectService.list();
  4. }
  5. });
  6. Template.projectForm.onRendered(function() {
  7. new Clipboard('.btn.clipboard');
  8. Session.set('vars', []);
  9. $('.variables input').val('');
  10. });
  11. Template.projectForm.events({
  12. 'submit .new-project': function (event) {
  13. event.preventDefault();
  14. var form = event.target,
  15. variables = Session.get('vars'),
  16. callback = function(errors, result) {
  17. if( errors ) {
  18. } else {
  19. Session.set('projectToEdit', undefined);
  20. Session.set('vars', []);
  21. $('.new-project input').val('');
  22. $('.new-project textarea').val('');
  23. }
  24. };
  25. if( form.id.value ) {
  26. Meteor.call('editProject',
  27. form.id.value,
  28. form.label.value,
  29. form.git_url.value,
  30. form.public_url.value,
  31. form.commands.value,
  32. form.run.value,
  33. variables,
  34. callback);
  35. } else {
  36. Meteor.call('addProject',
  37. form.label.value,
  38. form.git_url.value,
  39. form.public_url.value,
  40. form.commands.value,
  41. form.run.value,
  42. variables,
  43. callback);
  44. }
  45. },
  46. 'click .cancel': function(event) {
  47. event.preventDefault();
  48. Session.set('projectToEdit', undefined);
  49. Session.set('vars', []);
  50. $('.variables input').val('');
  51. },
  52. 'click .trash': function(event) {
  53. event.preventDefault();
  54. Meteor.call('deleteProject', Session.get('projectToEdit')._id);
  55. Session.set('projectToEdit', undefined);
  56. Session.set('vars', []);
  57. }
  58. });
  59. Template.projectForm.helpers({
  60. project: function() {
  61. return Session.get('projectToEdit');
  62. },
  63. editionMode: function() {
  64. return Session.get('projectToEdit') ? '' : 'hidden';
  65. },
  66. deployLink: function() {
  67. return Meteor.absoluteUrl('deploy?token=XXXX&project_id=' + Session.get('projectToEdit')._id);
  68. },
  69. vars: function() {
  70. return Session.get('vars');
  71. }
  72. });
  73. Template.variables.events({
  74. 'click .remove': function(event) {
  75. event.preventDefault();
  76. var variable = Template.currentData().name
  77. vars = Session.get('vars');
  78. vars = _.filter(vars, function(object) {
  79. return object.name !== variable;
  80. });
  81. Session.set('vars', vars);
  82. },
  83. 'click .add': function(event) {
  84. event.preventDefault();
  85. var instance = Template.instance(),
  86. name = instance.$('.name').val(),
  87. value = instance.$('.value').val();
  88. var variables = Session.get('vars');
  89. variables.push({name: name, value: value});
  90. Session.set('vars', variables);
  91. Session.set('variables.active', undefined);
  92. instance.$('.name').val(undefined);
  93. instance.$('.value').val(undefined);
  94. }
  95. });
  96. Template.variables.helpers({
  97. action: function() {
  98. return Template.currentData() ? 'remove' : 'add';
  99. },
  100. logo: function() {
  101. return Template.currentData() ? 'minus' : 'plus';
  102. },
  103. active: function() {
  104. var data = Template.currentData(),
  105. active = data && data.name && data.value;
  106. return active || Session.get('variables.active') ? '' : 'disabled';
  107. },
  108. readonly: function() {
  109. return Template.currentData() ? 'readonly' : '';
  110. }
  111. });
  112. Template.variables.events({
  113. 'keyup .name, keyup .value': function(event) {
  114. var instance = Template.instance(),
  115. name = instance.$('.name').val(),
  116. value = instance.$('.value').val();
  117. Session.set('variables.active', name && value);
  118. }
  119. });
  120. Template.project.events({
  121. 'click .edit': function(event) {
  122. event.preventDefault();
  123. return Meteor.call('getProject', this._id, function(error, result) {
  124. Session.set('projectToEdit', result);
  125. Session.set('vars', result.variables || []);
  126. });
  127. },
  128. });