ws-forms.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. _ws.makeView('add-repo-form', {
  38. init: function() {
  39. this.$inputTitle = this.$elem.find('input[name="title"]');
  40. },
  41. render: function() {
  42. console.log('render form add repo', this);
  43. this.$elem.addClass('in');
  44. this.$btn.removeClass('in');
  45. this.$inputTitle.focus();
  46. },
  47. events: {
  48. 'click .icon-cross': function() {
  49. this.$elem.removeClass('in');
  50. this.$btn.addClass('in');
  51. },
  52. 'submit': function(e) {
  53. e.preventDefault();
  54. var title = this.$inputTitle.val();
  55. rp.post('/collections', { title: title })
  56. .then(function(c) {
  57. _ws.notify('success', 'Collection créée: ' + c.title);
  58. })
  59. .catch(function(err) {
  60. console.error(err);
  61. _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
  62. });
  63. }
  64. },
  65. $btn: $('#add-repo-btn')
  66. });
  67. console.log(_ws.ui);
  68. _ws.ui.addRepoForm.$btn.click(_ws.ui.addRepoForm.render);
  69. console.log($addExampleBtn);
  70. $addExampleBtn.click(toggleEditor);
  71. $exampleCancel.click(clearAndCloseEditor);
  72. $exampleForm.submit(addExample);
  73. });
  74. })(jQuery);