ws-forms.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 checkPropsExist(obj, props) {
  38. if(typeof obj !== 'object' || ! props) {
  39. throw new Error('checkPropsExist was called with wrong arguments');
  40. }
  41. props = typeof props === 'string' ? [props] : props;
  42. for(let p = 0 ; p < props.length ; p++) {
  43. const prop = props[p];
  44. if(! obj[prop]) {
  45. throw new Error('obj does not have a `' + prop + '` parameter: please provide it.');
  46. }
  47. }
  48. }
  49. function makeFormView(elemId, options) {
  50. var callbacks = ['onSubmitAddPromise', 'onAddSuccess', 'onAddError'];
  51. // Fallback options & options.events
  52. checkPropsExist(options, callbacks)
  53. options.events = options.events || {};
  54. // Define events and optionally extend them with those
  55. // provided in options.events
  56. _ws.makeView(elemId, {
  57. init: function() {
  58. const self = this;
  59. this.$btn = this.$elem.find('.add-btn');
  60. this.$form = this.$elem.find('form');
  61. this.$input = this.$form.find('input[name="title"]');
  62. callbacks.forEach(function(cbName) {
  63. self[cbName] = options[cbName].bind(self);
  64. });
  65. if(options.postInit) {
  66. (options.postInit.bind(this))();
  67. }
  68. },
  69. reset: function() {
  70. console.log('reset', this);
  71. this.$btn.addClass('in');
  72. this.$form.removeClass('in');
  73. this.$input.val('');
  74. },
  75. render: function() {
  76. this.$btn.removeClass('in');
  77. this.$form.addClass('in');
  78. this.$input.focus();
  79. },
  80. events: {
  81. 'click .add-btn': function() {
  82. this.render();
  83. },
  84. 'click .icon-cross': function() {
  85. this.$form.removeClass('in');
  86. this.$btn.addClass('in');
  87. },
  88. 'submit form': function(e) {
  89. const self = this;
  90. e.preventDefault();
  91. if(this.cantSubmit) {
  92. this.$input
  93. .removeClass('input-success input-warning')
  94. .addClass('input-error');
  95. _ws.notify('error', this.cantSubmit);
  96. return;
  97. }
  98. var title = this.$input.val();
  99. // rp.post('/collections', { title: title })
  100. self.onSubmitAddPromise(title)
  101. .then(function(data) {
  102. self.onAddSuccess(data);
  103. self.reset();
  104. })
  105. .catch(function(err) {
  106. console.error(err);
  107. self.onAddError(err);
  108. self.reset();
  109. });
  110. }
  111. }
  112. });
  113. }
  114. makeFormView('add-repo', {
  115. onSubmitAddPromise: function(title) {
  116. return rp.post('/repos', { title: title });
  117. },
  118. onAddSuccess: function(repo) {
  119. _ws.repos.push(repo);
  120. _ws.ui.menuRepo.render({ repos: _ws.repos });
  121. _ws.notify('success', 'Collection créée: ' + repo.title);
  122. _ws.navigateTo('/' + repo.path);
  123. },
  124. onAddError: function(err) {
  125. _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
  126. }
  127. });
  128. makeFormView('add-example', {
  129. onSubmitAddPromise: function(title) {
  130. return rp.post('/' + _ws.repo.path + '/examples', { title: title });
  131. },
  132. onAddSuccess: function(example) {
  133. _ws.notify('success', 'Exemple crée: ' + example.title);
  134. _ws.navigateTo('/' + _ws.repo.path + '/' + example.slug);
  135. },
  136. onAddError: function(err) {
  137. _ws.notify('error', "Impossible de créer l'exemple: " + err.message);
  138. }
  139. });
  140. makeFormView(
  141. 'add-file',
  142. {
  143. onSubmitAddPromise: function(name) {
  144. return rp.post('/' + _ws.repo.path + '/examples/' + _ws.example.slug + '/files', { name: name });
  145. },
  146. onAddSuccess: function(file) {
  147. _ws.notify('success', 'Fichier crée: ' + file.name);
  148. console.log('****** onAddSuccess file', file, _ws.files);
  149. _ws.files.push(file);
  150. _ws.ui.tabs.render({ files: _ws.files });
  151. },
  152. onAddError: function(err) {
  153. _ws.notify('error', "Impossible de créer le fichier: " + err.message);
  154. },
  155. postInit: function() {
  156. console.log('### exec postInit', this);
  157. function onKeyup(e) {
  158. var filename = this.$input.val();
  159. var bits = filename.split('.');
  160. var lastIdx = bits.length - 1;
  161. this.cantSubmit = '';
  162. // show warning if no extension has been provided
  163. if(bits.length < 2) {
  164. this.$input
  165. .removeClass('input-success input-error')
  166. .addClass('input-warning');
  167. this.cantSubmit = 'Impossible de valider: extension (.html/.js/.css) manquante';
  168. return;
  169. }
  170. // show error if provided extension is invalid
  171. else {
  172. var ext = (bits[lastIdx]).toLowerCase();
  173. console.log(bits);
  174. if( ['html', 'js', 'css'].indexOf( ext ) === -1 ) {
  175. this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
  176. }
  177. else if(bits[0] === '') {
  178. this.cantSubmit = "Le premier caractère du nom de fichier doit être autre chose qu'un point";
  179. }
  180. if( this.cantSubmit ) {
  181. this.$input
  182. .removeClass('input-success input-warning')
  183. .addClass('input-error');
  184. return;
  185. }
  186. }
  187. this.$input.addClass('input-success');
  188. this.cantSubmit = undefined;
  189. }
  190. this.$input.on('keyup', onKeyup.bind(this));
  191. }
  192. }
  193. );
  194. console.log(_ws.ui);
  195. // _ws.ui.addRepoForm.$btn.click(_ws.ui.addRepoForm.render);
  196. // console.log($addExampleBtn);
  197. // $addExampleBtn.click(toggleEditor);
  198. // $exampleCancel.click(clearAndCloseEditor);
  199. // $exampleForm.submit(addExample);
  200. });
  201. })(jQuery);