ws-forms.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, options) {
  38. // Fallback options & options.events
  39. options = options || {};
  40. options.events = options.events || {};
  41. // Define events and optionally extend them with those
  42. // provided in options.events
  43. var events = Object.assign({
  44. 'click .add-btn': function() {
  45. this.render();
  46. },
  47. 'click .icon-cross': function() {
  48. this.$form.removeClass('in');
  49. this.$btn.addClass('in');
  50. },
  51. 'submit form': function(e) {
  52. e.preventDefault();
  53. if(this.cantSubmit) {
  54. this.$input
  55. .removeClass('input-success input-warning')
  56. .addClass('input-error');
  57. _ws.notify('error', this.cantSubmit);
  58. return;
  59. }
  60. var title = this.$input.val();
  61. // rp.post('/collections', { title: title })
  62. onSubmitPromise(title)
  63. .then(function(c) {
  64. _ws.notify('success', 'Collection créée: ' + c.title);
  65. })
  66. .catch(function(err) {
  67. console.error(err);
  68. _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
  69. });
  70. }
  71. }, options.events);
  72. _ws.makeView(elemId, {
  73. init: function() {
  74. this.$btn = this.$elem.find('.add-btn');
  75. this.$form = this.$elem.find('form');
  76. this.$input = this.$form.find('input[name="title"]');
  77. console.log('init add repo form', this.$elem.prop('id'))
  78. },
  79. render: function() {
  80. console.log('render form add ', this.$elem.prop('id'));
  81. this.$btn.removeClass('in');
  82. this.$form.addClass('in');
  83. this.$input.focus();
  84. },
  85. events: events
  86. });
  87. }
  88. makeFormView('add-repo', function(title) {
  89. return rp.post('/repos', { title: title });
  90. });
  91. makeFormView('add-example', function(title) {
  92. return rp.post('/' + _ws.repo.path + '/examples', { title: title });
  93. });
  94. makeFormView(
  95. 'add-file',
  96. function(name) {
  97. return rp.post('/' + _ws.repo.path + '/examples/file', { name: name });
  98. },
  99. {
  100. events: {
  101. 'keyup input[name="title"]': function(e) {
  102. var filename = this.$input.val();
  103. // console.log('filename', filename);
  104. var bits = filename.split('.');
  105. var lastIdx = bits.length - 1;
  106. console.log(bits, bits.length, lastIdx, bits[lastIdx], typeof bits[lastIdx]);
  107. if(bits.length < 2) {
  108. this.$input
  109. .removeClass('input-success input-error')
  110. .addClass('input-warning');
  111. this.cantSubmit = 'Impossible de valider: extension (.html/.js/.css) manquante';
  112. return;
  113. }
  114. else {
  115. var ext = (bits[lastIdx]).toLowerCase();
  116. console.log(ext);
  117. if( ['html', 'js', 'css'].indexOf( ext ) === -1 ) {
  118. this.$input
  119. .removeClass('input-success input-warning')
  120. .addClass('input-error');
  121. this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
  122. return;
  123. }
  124. }
  125. this.$input.addClass('input-success');
  126. this.cantSubmit = undefined;
  127. }
  128. }
  129. }
  130. );
  131. console.log(_ws.ui);
  132. // _ws.ui.addRepoForm.$btn.click(_ws.ui.addRepoForm.render);
  133. // console.log($addExampleBtn);
  134. // $addExampleBtn.click(toggleEditor);
  135. // $exampleCancel.click(clearAndCloseEditor);
  136. // $exampleForm.submit(addExample);
  137. });
  138. })(jQuery);