ws-forms.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. _ws.navigateTo('/' + repo.path);
  124. },
  125. onAddError: function(err) {
  126. _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
  127. }
  128. });
  129. makeFormView('add-example', {
  130. onSubmitAddPromise: function(title) {
  131. return rp.post('/' + _ws.repo.path + '/examples', { title: title });
  132. },
  133. onAddSuccess: function(example) {
  134. console.log('****** onAddSuccess example', example);
  135. _ws.notify('success', 'Exemple crée: ' + example.title);
  136. _ws.navigateTo('/' + _ws.repo.path + '/' + example.slug);
  137. },
  138. onAddError: function(err) {
  139. _ws.notify('error', "Impossible de créer l'exemple: " + err.message);
  140. }
  141. });
  142. makeFormView(
  143. 'add-file',
  144. {
  145. onSubmitAddPromise: function(name) {
  146. return rp.post('/' + _ws.repo.path + '/examples/' + _ws.example.slug + '/files', { name: name });
  147. },
  148. onAddSuccess: function(file) {
  149. _ws.notify('success', 'Fichier crée: ' + file.name);
  150. console.log('****** onAddSuccess file', file, _ws.files);
  151. _ws.files.push(file);
  152. _ws.ui.tabs.render({ files: _ws.files });
  153. },
  154. onAddError: function(err) {
  155. _ws.notify('error', "Impossible de créer le fichier: " + err.message);
  156. },
  157. postInit: function() {
  158. console.log('### exec postInit', this);
  159. function onKeyup(e) {
  160. var filename = this.$input.val();
  161. var bits = filename.split('.');
  162. var lastIdx = bits.length - 1;
  163. this.cantSubmit = '';
  164. // show warning if no extension has been provided
  165. if(bits.length < 2) {
  166. this.$input
  167. .removeClass('input-success input-error')
  168. .addClass('input-warning');
  169. this.cantSubmit = 'Impossible de valider: extension (.html/.js/.css) manquante';
  170. return;
  171. }
  172. // show error if provided extension is invalid
  173. else {
  174. var ext = (bits[lastIdx]).toLowerCase();
  175. console.log(bits);
  176. if( ['html', 'js', 'css'].indexOf( ext ) === -1 ) {
  177. this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
  178. }
  179. else if(bits[0] === '') {
  180. this.cantSubmit = "Le premier caractère du nom de fichier doit être autre chose qu'un point";
  181. }
  182. if( this.cantSubmit ) {
  183. this.$input
  184. .removeClass('input-success input-warning')
  185. .addClass('input-error');
  186. return;
  187. }
  188. }
  189. this.$input.addClass('input-success');
  190. this.cantSubmit = undefined;
  191. }
  192. this.$input.on('keyup', onKeyup.bind(this));
  193. }
  194. }
  195. );
  196. console.log(_ws.ui);
  197. // _ws.ui.addRepoForm.$btn.click(_ws.ui.addRepoForm.render);
  198. // console.log($addExampleBtn);
  199. // $addExampleBtn.click(toggleEditor);
  200. // $exampleCancel.click(clearAndCloseEditor);
  201. // $exampleForm.submit(addExample);
  202. });
  203. })(jQuery);