ws-forms.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. (function($) {
  3. $(document).ready(function() {
  4. function checkPropsExist(obj, props) {
  5. if(typeof obj !== 'object' || ! props) {
  6. throw new Error('checkPropsExist was called with wrong arguments');
  7. }
  8. props = typeof props === 'string' ? [props] : props;
  9. for(let p = 0 ; p < props.length ; p++) {
  10. const prop = props[p];
  11. if(! obj[prop]) {
  12. throw new Error('obj does not have a `' + prop + '` parameter: please provide it.');
  13. }
  14. }
  15. }
  16. function makeFormView(elemId, options) {
  17. var callbacks = ['onSubmitAddPromise', 'onAddSuccess', 'onAddError'];
  18. // Fallback options & options.events
  19. checkPropsExist(options, callbacks)
  20. options.events = options.events || {};
  21. // Define events and optionally extend them with those
  22. // provided in options.events
  23. _ws.makeView(elemId, {
  24. init: function() {
  25. const self = this;
  26. this.$btn = this.$elem.find('.add-btn');
  27. this.$form = this.$elem.find('form');
  28. this.$input = this.$form.find('input[name="title"]');
  29. callbacks.forEach(function(cbName) {
  30. self[cbName] = options[cbName].bind(self);
  31. });
  32. if(options.postInit) {
  33. (options.postInit.bind(this))();
  34. }
  35. },
  36. reset: function() {
  37. console.log('reset', this);
  38. this.$btn.addClass('in');
  39. this.$form.removeClass('in');
  40. this.$input.val('');
  41. },
  42. render: function() {
  43. this.$btn.removeClass('in');
  44. this.$form.addClass('in');
  45. this.$input.focus();
  46. },
  47. events: {
  48. 'click .add-btn': function() {
  49. this.render();
  50. },
  51. 'click .icon-cross': function() {
  52. this.$form.removeClass('in');
  53. this.$btn.addClass('in');
  54. },
  55. 'submit form': function(e) {
  56. const self = this;
  57. e.preventDefault();
  58. if(this.cantSubmit) {
  59. this.$input
  60. .removeClass('input-success input-warning')
  61. .addClass('input-error');
  62. _ws.notify('error', this.cantSubmit);
  63. return;
  64. }
  65. var title = this.$input.val();
  66. // rp.post('/collections', { title: title })
  67. self.onSubmitAddPromise(title)
  68. .then(function(data) {
  69. self.onAddSuccess(data);
  70. self.reset();
  71. })
  72. .catch(function(err) {
  73. console.error(err);
  74. self.onAddError(err);
  75. self.reset();
  76. });
  77. }
  78. }
  79. });
  80. }
  81. makeFormView('add-repo', {
  82. onSubmitAddPromise: function(title) {
  83. return rp.post('/repos', { title: title });
  84. },
  85. onAddSuccess: function(repo) {
  86. _ws.repos.push(repo);
  87. _ws.ui.menuRepo.render({ repos: _ws.repos });
  88. _ws.notify('success', 'Collection créée: ' + repo.title);
  89. _ws.navigateTo('/' + repo.path);
  90. },
  91. onAddError: function(err) {
  92. _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
  93. }
  94. });
  95. makeFormView('add-example', {
  96. onSubmitAddPromise: function(title) {
  97. return rp.post('/' + _ws.repo.path + '/examples', { title: title });
  98. },
  99. onAddSuccess: function(example) {
  100. _ws.notify('success', 'Exemple crée: ' + example.title);
  101. _ws.navigateTo('/' + _ws.repo.path + '/' + example.slug);
  102. // console.log('##### addExample success', _ws, _ws.ui.menuExample.$elem.find('ul[data-id="' + example.category + '"]') )
  103. _ws.ui.menuExample.addExampleLink(example, '/' + _ws.repo.path + '/' + example.slug);
  104. _ws.ui.shortcutExample.addExampleLink(example, '/' + _ws.repo.path + '/' + example.slug);
  105. },
  106. onAddError: function(err) {
  107. _ws.notify('error', "Impossible de créer l'exemple: " + err.message);
  108. }
  109. });
  110. makeFormView(
  111. 'add-file',
  112. {
  113. onSubmitAddPromise: function(name) {
  114. return rp.post('/' + _ws.repo.path + '/examples/' + _ws.example.slug + '/files', { name: name });
  115. },
  116. onAddSuccess: function(file) {
  117. _ws.notify('success', 'Fichier crée: ' + file.name);
  118. console.log('****** onAddSuccess file', file, _ws.files);
  119. _ws.files.push(file);
  120. _ws.ui.tabs.render({ files: _ws.files });
  121. },
  122. onAddError: function(err) {
  123. _ws.notify('error', "Impossible de créer le fichier: " + err.message);
  124. },
  125. postInit: function() {
  126. console.log('### exec postInit', this);
  127. function onKeyup(e) {
  128. var filename = this.$input.val();
  129. var bits = filename.split('.');
  130. var lastIdx = bits.length - 1;
  131. this.cantSubmit = '';
  132. // show warning if no extension has been provided
  133. if(bits.length < 2) {
  134. this.$input
  135. .removeClass('input-success input-error')
  136. .addClass('input-warning');
  137. this.cantSubmit = 'Impossible de valider: extension (.html/.js/.css) manquante';
  138. return;
  139. }
  140. // show error if provided extension is invalid
  141. else {
  142. var ext = (bits[lastIdx]).toLowerCase();
  143. console.log(bits);
  144. if( ['html', 'js', 'css'].indexOf( ext ) === -1 ) {
  145. this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
  146. }
  147. else if(bits[0] === '') {
  148. this.cantSubmit = "Le premier caractère du nom de fichier doit être autre chose qu'un point";
  149. }
  150. if( this.cantSubmit ) {
  151. this.$input
  152. .removeClass('input-success input-warning')
  153. .addClass('input-error');
  154. return;
  155. }
  156. }
  157. this.$input.addClass('input-success');
  158. this.cantSubmit = undefined;
  159. }
  160. this.$input.on('keyup', onKeyup.bind(this));
  161. }
  162. }
  163. );
  164. console.log(_ws.ui);
  165. });
  166. })(jQuery);