ws-forms.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. (function($) {
  3. $(document).ready(function() {
  4. // var $addRepoBtn = $('#add-repo-btn');
  5. // var $addExampleBtn = $('#add-example-btn');
  6. // var $exampleForm = $('#add-example-form');
  7. // var $exampleFormIn = $exampleForm.find('input');
  8. // var $exampleSave = $('#add-example-save');
  9. // var $exampleCancel = $('#add-example-cancel');
  10. // /**
  11. // * Add new example
  12. // */
  13. // function addExample(e) {
  14. // e.preventDefault();
  15. // var repoSlug = $detailsRepo.data('slug');
  16. // var title = $(this).find('input[name="title"]').val();
  17. // rp.post('/' + repoSlug + '/examples', { title: title })
  18. // .then(function(example) {
  19. // clearAndCloseEditor();
  20. // // $fileSelect.append(makeFileSelectOption(example));
  21. // // $fileSelect.val(example.slug);
  22. // // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
  23. // // .appendTo( $('#cat-' + example.category) );
  24. // notify('success', "Exemple créé !");
  25. // // navigateTo('/'+ repoSlug + '/' + example.slug);
  26. // })
  27. // .catch(errText => notify('error', 'Erreur: ' + errText));
  28. // }
  29. // function clearAndCloseEditor() {
  30. // $exampleFormIn.val('');
  31. // toggleEditor();
  32. // }
  33. // function toggleEditor() {
  34. // $addExampleBtn.toggle();
  35. // $exampleForm.toggleClass("hidden");
  36. // }
  37. function makeFormView(elemId, onSubmitPromise) {
  38. _ws.makeView(elemId, {
  39. init: function() {
  40. this.$btn = this.$elem.find('.add-btn');
  41. this.$form = this.$elem.find('form');
  42. this.$input = this.$form.find('input[name="title"]');
  43. console.log('init add repo form', this.$elem.prop('id'))
  44. },
  45. render: function() {
  46. console.log('render form add ', this.$elem.prop('id'));
  47. this.$btn.removeClass('in');
  48. this.$form.addClass('in');
  49. this.$input.focus();
  50. },
  51. events: {
  52. 'click .add-btn': function() {
  53. this.render();
  54. },
  55. 'click .icon-cross': function() {
  56. this.$form.removeClass('in');
  57. this.$btn.addClass('in');
  58. },
  59. 'submit form': function(e) {
  60. e.preventDefault();
  61. var title = this.$input.val();
  62. // rp.post('/collections', { title: title })
  63. onSubmitPromise(title)
  64. .then(function(c) {
  65. _ws.notify('success', 'Collection créée: ' + c.title);
  66. })
  67. .catch(function(err) {
  68. console.error(err);
  69. _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
  70. });
  71. }
  72. }
  73. });
  74. }
  75. makeFormView('add-repo', function(title) {
  76. return rp.post('/repos', { title: title });
  77. });
  78. makeFormView('add-example', function(title) {
  79. return rp.post('/' + _ws.repo.path + '/examples', { title: title });
  80. });
  81. console.log(_ws.ui);
  82. // _ws.ui.addRepoForm.$btn.click(_ws.ui.addRepoForm.render);
  83. // console.log($addExampleBtn);
  84. // $addExampleBtn.click(toggleEditor);
  85. // $exampleCancel.click(clearAndCloseEditor);
  86. // $exampleForm.submit(addExample);
  87. });
  88. })(jQuery);