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. console.log('binding cb', cbName);
  64. self[cbName] = options[cbName].bind(self);
  65. });
  66. if(options.postInit) {
  67. (options.postInit.bind(this))();
  68. }
  69. console.log('init add repo form', this.$elem.prop('id'))
  70. },
  71. reset: function() {
  72. this.$btn.addClass('in');
  73. this.$form.removeClass('in');
  74. this.$input.val();
  75. },
  76. render: function() {
  77. console.log('render form add ', this.$elem.prop('id'));
  78. this.$btn.removeClass('in');
  79. this.$form.addClass('in');
  80. this.$input.focus();
  81. },
  82. events: {
  83. 'click .add-btn': function() {
  84. this.render();
  85. },
  86. 'click .icon-cross': function() {
  87. this.$form.removeClass('in');
  88. this.$btn.addClass('in');
  89. },
  90. 'submit form': function(e) {
  91. const self = this;
  92. e.preventDefault();
  93. if(this.cantSubmit) {
  94. this.$input
  95. .removeClass('input-success input-warning')
  96. .addClass('input-error');
  97. _ws.notify('error', this.cantSubmit);
  98. return;
  99. }
  100. var title = this.$input.val();
  101. // rp.post('/collections', { title: title })
  102. self.onSubmitAddPromise(title)
  103. .then(function(data) {
  104. self.onAddSuccess(data);
  105. self.reset();
  106. })
  107. .catch(function(err) {
  108. console.error(err);
  109. self.onAddError(err);
  110. self.reset();
  111. });
  112. }
  113. }
  114. });
  115. }
  116. makeFormView('add-repo', {
  117. onSubmitAddPromise: function(title) {
  118. return rp.post('/repos', { title: title });
  119. },
  120. onAddSuccess: function(repo) {
  121. console.log('****** onAddSuccess repo', repo);
  122. _ws.notify('success', 'Collection créée: ' + repo.title);
  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. console.log('****** onAddSuccess example', example);
  134. _ws.notify('success', 'Exemple crée: ' + example.title);
  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);