|
|
@@ -43,57 +43,51 @@
|
|
|
// $addExampleBtn.toggle();
|
|
|
// $exampleForm.toggleClass("hidden");
|
|
|
// }
|
|
|
+function checkPropsExist(obj, props) {
|
|
|
+ if(typeof obj !== 'object' || ! props) {
|
|
|
+ throw new Error('checkPropsExist was called with wrong arguments');
|
|
|
+ }
|
|
|
+ props = typeof props === 'string' ? [props] : props;
|
|
|
+ for(let p = 0 ; p < props.length ; p++) {
|
|
|
+ const prop = props[p];
|
|
|
+ if(! obj[prop]) {
|
|
|
+ throw new Error('obj does not have a `' + prop + '` parameter: please provide it.');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
- function makeFormView(elemId, onSubmitPromise, options) {
|
|
|
-
|
|
|
+ function makeFormView(elemId, options) {
|
|
|
+ var callbacks = ['onSubmitAddPromise', 'onAddSuccess', 'onAddError'];
|
|
|
// Fallback options & options.events
|
|
|
- options = options || {};
|
|
|
+ checkPropsExist(options, callbacks)
|
|
|
options.events = options.events || {};
|
|
|
|
|
|
// Define events and optionally extend them with those
|
|
|
// provided in options.events
|
|
|
- var events = Object.assign({
|
|
|
- 'click .add-btn': function() {
|
|
|
- this.render();
|
|
|
- },
|
|
|
-
|
|
|
- 'click .icon-cross': function() {
|
|
|
- this.$form.removeClass('in');
|
|
|
- this.$btn.addClass('in');
|
|
|
- },
|
|
|
-
|
|
|
- 'submit form': function(e) {
|
|
|
- e.preventDefault();
|
|
|
- if(this.cantSubmit) {
|
|
|
- this.$input
|
|
|
- .removeClass('input-success input-warning')
|
|
|
- .addClass('input-error');
|
|
|
- _ws.notify('error', this.cantSubmit);
|
|
|
- return;
|
|
|
- }
|
|
|
- var title = this.$input.val();
|
|
|
- // rp.post('/collections', { title: title })
|
|
|
- onSubmitPromise(title)
|
|
|
- .then(function(c) {
|
|
|
- _ws.notify('success', 'Collection créée: ' + c.title);
|
|
|
- })
|
|
|
- .catch(function(err) {
|
|
|
- console.error(err);
|
|
|
- _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
|
|
|
- });
|
|
|
- }
|
|
|
- }, options.events);
|
|
|
|
|
|
_ws.makeView(elemId, {
|
|
|
|
|
|
init: function() {
|
|
|
+ const self = this;
|
|
|
this.$btn = this.$elem.find('.add-btn');
|
|
|
this.$form = this.$elem.find('form');
|
|
|
this.$input = this.$form.find('input[name="title"]');
|
|
|
+ callbacks.forEach(function(cbName) {
|
|
|
+ console.log('binding cb', cbName);
|
|
|
+ self[cbName] = options[cbName].bind(self);
|
|
|
+ });
|
|
|
+ if(options.postInit) {
|
|
|
+ (options.postInit.bind(this))();
|
|
|
+ }
|
|
|
console.log('init add repo form', this.$elem.prop('id'))
|
|
|
},
|
|
|
|
|
|
+ reset: function() {
|
|
|
+ this.$btn.addClass('in');
|
|
|
+ this.$form.removeClass('in');
|
|
|
+ this.$input.val();
|
|
|
+ },
|
|
|
+
|
|
|
render: function() {
|
|
|
console.log('render form add ', this.$elem.prop('id'));
|
|
|
this.$btn.removeClass('in');
|
|
|
@@ -101,28 +95,91 @@
|
|
|
this.$input.focus();
|
|
|
},
|
|
|
|
|
|
- events: events
|
|
|
+ events: {
|
|
|
+ 'click .add-btn': function() {
|
|
|
+ this.render();
|
|
|
+ },
|
|
|
+
|
|
|
+ 'click .icon-cross': function() {
|
|
|
+ this.$form.removeClass('in');
|
|
|
+ this.$btn.addClass('in');
|
|
|
+ },
|
|
|
+
|
|
|
+ 'submit form': function(e) {
|
|
|
+ const self = this;
|
|
|
+ e.preventDefault();
|
|
|
+ if(this.cantSubmit) {
|
|
|
+ this.$input
|
|
|
+ .removeClass('input-success input-warning')
|
|
|
+ .addClass('input-error');
|
|
|
+ _ws.notify('error', this.cantSubmit);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var title = this.$input.val();
|
|
|
+ // rp.post('/collections', { title: title })
|
|
|
+ self.onSubmitAddPromise(title)
|
|
|
+ .then(function(data) {
|
|
|
+ self.onAddSuccess(data);
|
|
|
+ self.reset();
|
|
|
+ })
|
|
|
+ .catch(function(err) {
|
|
|
+ console.error(err);
|
|
|
+ self.onAddError(err);
|
|
|
+ self.reset();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- makeFormView('add-repo', function(title) {
|
|
|
- return rp.post('/repos', { title: title });
|
|
|
+ makeFormView('add-repo', {
|
|
|
+ onSubmitAddPromise: function(title) {
|
|
|
+ return rp.post('/repos', { title: title });
|
|
|
+ },
|
|
|
+ onAddSuccess: function(repo) {
|
|
|
+ console.log('****** onAddSuccess repo', repo);
|
|
|
+ _ws.notify('success', 'Collection créée: ' + repo.title);
|
|
|
+ },
|
|
|
+ onAddError: function(err) {
|
|
|
+ _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
- makeFormView('add-example', function(title) {
|
|
|
- return rp.post('/' + _ws.repo.path + '/examples', { title: title });
|
|
|
+ makeFormView('add-example', {
|
|
|
+ onSubmitAddPromise: function(title) {
|
|
|
+ return rp.post('/' + _ws.repo.path + '/examples', { title: title });
|
|
|
+ },
|
|
|
+ onAddSuccess: function(example) {
|
|
|
+ console.log('****** onAddSuccess example', example);
|
|
|
+ _ws.notify('success', 'Exemple crée: ' + example.title);
|
|
|
+ },
|
|
|
+ onAddError: function(err) {
|
|
|
+ _ws.notify('error', "Impossible de créer l'exemple: " + err.message);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
|
|
|
makeFormView(
|
|
|
'add-file',
|
|
|
- function(name) {
|
|
|
- return rp.post('/' + _ws.repo.path + '/examples/' + _ws.example.slug + '/file', { name: name });
|
|
|
- },
|
|
|
{
|
|
|
- events: {
|
|
|
- 'keyup input[name="title"]': function(e) {
|
|
|
+ onSubmitAddPromise: function(name) {
|
|
|
+ return rp.post('/' + _ws.repo.path + '/examples/' + _ws.example.slug + '/files', { name: name });
|
|
|
+ },
|
|
|
+
|
|
|
+ onAddSuccess: function(file) {
|
|
|
+ _ws.notify('success', 'Fichier crée: ' + file.name);
|
|
|
+ console.log('****** onAddSuccess file', file, _ws.files);
|
|
|
+ _ws.files.push(file);
|
|
|
+ _ws.ui.tabs.render({ files: _ws.files });
|
|
|
+ },
|
|
|
+ onAddError: function(err) {
|
|
|
+ _ws.notify('error', "Impossible de créer le fichier: " + err.message);
|
|
|
+ },
|
|
|
+
|
|
|
+ postInit: function() {
|
|
|
+ console.log('### exec postInit', this);
|
|
|
+ function onKeyup(e) {
|
|
|
var filename = this.$input.val();
|
|
|
var bits = filename.split('.');
|
|
|
var lastIdx = bits.length - 1;
|
|
|
@@ -157,6 +214,7 @@
|
|
|
this.$input.addClass('input-success');
|
|
|
this.cantSubmit = undefined;
|
|
|
}
|
|
|
+ this.$input.on('keyup', onKeyup.bind(this));
|
|
|
}
|
|
|
}
|
|
|
);
|