ws-forms.js 2.6 KB

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