PageRenderTime 2960ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/app/assets/javascripts/issuable_form.js

https://gitlab.com/haleksandre/gitlab-ce
JavaScript | 150 lines | 131 code | 19 blank | 0 comment | 16 complexity | 3d2b5eb19dcb8775247ae6f5800ca697 MD5 | raw file
  1. (function() {
  2. var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  3. this.IssuableForm = (function() {
  4. IssuableForm.prototype.issueMoveConfirmMsg = 'Are you sure you want to move this issue to another project?';
  5. IssuableForm.prototype.wipRegex = /^\s*(\[WIP\]\s*|WIP:\s*|WIP\s+)+\s*/i;
  6. function IssuableForm(form) {
  7. var $issuableDueDate;
  8. this.form = form;
  9. this.toggleWip = bind(this.toggleWip, this);
  10. this.renderWipExplanation = bind(this.renderWipExplanation, this);
  11. this.resetAutosave = bind(this.resetAutosave, this);
  12. this.handleSubmit = bind(this.handleSubmit, this);
  13. GitLab.GfmAutoComplete.setup();
  14. new UsersSelect();
  15. new ZenMode();
  16. this.titleField = this.form.find("input[name*='[title]']");
  17. this.descriptionField = this.form.find("textarea[name*='[description]']");
  18. this.issueMoveField = this.form.find("#move_to_project_id");
  19. if (!(this.titleField.length && this.descriptionField.length)) {
  20. return;
  21. }
  22. this.initAutosave();
  23. this.form.on("submit", this.handleSubmit);
  24. this.form.on("click", ".btn-cancel", this.resetAutosave);
  25. this.initWip();
  26. this.initMoveDropdown();
  27. $issuableDueDate = $('#issuable-due-date');
  28. if ($issuableDueDate.length) {
  29. $('.datepicker').datepicker({
  30. dateFormat: 'yy-mm-dd',
  31. onSelect: function(dateText, inst) {
  32. return $issuableDueDate.val(dateText);
  33. }
  34. }).datepicker('setDate', $.datepicker.parseDate('yy-mm-dd', $issuableDueDate.val()));
  35. }
  36. }
  37. IssuableForm.prototype.initAutosave = function() {
  38. new Autosave(this.titleField, [document.location.pathname, document.location.search, "title"]);
  39. return new Autosave(this.descriptionField, [document.location.pathname, document.location.search, "description"]);
  40. };
  41. IssuableForm.prototype.handleSubmit = function() {
  42. var ref, ref1;
  43. if (((ref = parseInt((ref1 = this.issueMoveField) != null ? ref1.val() : void 0)) != null ? ref : 0) > 0) {
  44. if (!confirm(this.issueMoveConfirmMsg)) {
  45. return false;
  46. }
  47. }
  48. return this.resetAutosave();
  49. };
  50. IssuableForm.prototype.resetAutosave = function() {
  51. this.titleField.data("autosave").reset();
  52. return this.descriptionField.data("autosave").reset();
  53. };
  54. IssuableForm.prototype.initWip = function() {
  55. this.$wipExplanation = this.form.find(".js-wip-explanation");
  56. this.$noWipExplanation = this.form.find(".js-no-wip-explanation");
  57. if (!(this.$wipExplanation.length && this.$noWipExplanation.length)) {
  58. return;
  59. }
  60. this.form.on("click", ".js-toggle-wip", this.toggleWip);
  61. this.titleField.on("keyup blur", this.renderWipExplanation);
  62. return this.renderWipExplanation();
  63. };
  64. IssuableForm.prototype.workInProgress = function() {
  65. return this.wipRegex.test(this.titleField.val());
  66. };
  67. IssuableForm.prototype.renderWipExplanation = function() {
  68. if (this.workInProgress()) {
  69. this.$wipExplanation.show();
  70. return this.$noWipExplanation.hide();
  71. } else {
  72. this.$wipExplanation.hide();
  73. return this.$noWipExplanation.show();
  74. }
  75. };
  76. IssuableForm.prototype.toggleWip = function(event) {
  77. event.preventDefault();
  78. if (this.workInProgress()) {
  79. this.removeWip();
  80. } else {
  81. this.addWip();
  82. }
  83. return this.renderWipExplanation();
  84. };
  85. IssuableForm.prototype.removeWip = function() {
  86. return this.titleField.val(this.titleField.val().replace(this.wipRegex, ""));
  87. };
  88. IssuableForm.prototype.addWip = function() {
  89. return this.titleField.val("WIP: " + (this.titleField.val()));
  90. };
  91. IssuableForm.prototype.initMoveDropdown = function() {
  92. var $moveDropdown, pageSize;
  93. $moveDropdown = $('.js-move-dropdown');
  94. if ($moveDropdown.length) {
  95. pageSize = $moveDropdown.data('page-size');
  96. return $('.js-move-dropdown').select2({
  97. ajax: {
  98. url: $moveDropdown.data('projects-url'),
  99. quietMillis: 125,
  100. data: function(term, page, context) {
  101. return {
  102. search: term,
  103. offset_id: context
  104. };
  105. },
  106. results: function(data) {
  107. var context,
  108. more;
  109. if (data.length >= pageSize)
  110. more = true;
  111. if (data[data.length - 1])
  112. context = data[data.length - 1].id;
  113. return {
  114. results: data,
  115. more: more,
  116. context: context
  117. };
  118. }
  119. },
  120. formatResult: function(project) {
  121. return project.name_with_namespace;
  122. },
  123. formatSelection: function(project) {
  124. return project.name_with_namespace;
  125. }
  126. });
  127. }
  128. };
  129. return IssuableForm;
  130. })();
  131. }).call(this);