| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- "use strict";
- (function($) {
- $(document).ready(function() {
- // var $addRepoBtn = $('#add-repo-btn');
- // var $addExampleBtn = $('#add-example-btn');
- // var $exampleForm = $('#add-example-form');
- // var $exampleFormIn = $exampleForm.find('input');
- // var $exampleSave = $('#add-example-save');
- // var $exampleCancel = $('#add-example-cancel');
- // /**
- // * Add new example
- // */
- // function addExample(e) {
- // e.preventDefault();
- // var repoSlug = $detailsRepo.data('slug');
- // var title = $(this).find('input[name="title"]').val();
- // rp.post('/' + repoSlug + '/examples', { title: title })
- // .then(function(example) {
- // clearAndCloseEditor();
- // // $fileSelect.append(makeFileSelectOption(example));
- // // $fileSelect.val(example.slug);
-
- // // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
- // // .appendTo( $('#cat-' + example.category) );
- // notify('success', "Exemple créé !");
- // // navigateTo('/'+ repoSlug + '/' + example.slug);
- // })
- // .catch(errText => notify('error', 'Erreur: ' + errText));
- // }
- // function clearAndCloseEditor() {
- // $exampleFormIn.val('');
- // toggleEditor();
- // }
- // function toggleEditor() {
- // $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, options) {
- var callbacks = ['onSubmitAddPromise', 'onAddSuccess', 'onAddError'];
- // Fallback options & options.events
- checkPropsExist(options, callbacks)
- options.events = options.events || {};
- // Define events and optionally extend them with those
- // provided in 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) {
- self[cbName] = options[cbName].bind(self);
- });
- if(options.postInit) {
- (options.postInit.bind(this))();
- }
- },
- reset: function() {
- console.log('reset', this);
- this.$btn.addClass('in');
- this.$form.removeClass('in');
- this.$input.val('');
- },
- render: function() {
- this.$btn.removeClass('in');
- this.$form.addClass('in');
- this.$input.focus();
- },
- 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', {
- onSubmitAddPromise: function(title) {
- return rp.post('/repos', { title: title });
- },
- onAddSuccess: function(repo) {
- _ws.repos.push(repo);
- _ws.ui.menuRepo.render({ repos: _ws.repos });
- _ws.notify('success', 'Collection créée: ' + repo.title);
- _ws.navigateTo('/' + repo.path);
- },
- onAddError: function(err) {
- _ws.notify('error', 'Impossible de créer la collection: ' + err.message);
- }
- });
- makeFormView('add-example', {
- onSubmitAddPromise: function(title) {
- return rp.post('/' + _ws.repo.path + '/examples', { title: title });
- },
- onAddSuccess: function(example) {
- _ws.notify('success', 'Exemple crée: ' + example.title);
- _ws.navigateTo('/' + _ws.repo.path + '/' + example.slug);
- },
- onAddError: function(err) {
- _ws.notify('error', "Impossible de créer l'exemple: " + err.message);
- }
- });
- makeFormView(
- 'add-file',
- {
- 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;
- this.cantSubmit = '';
- // show warning if no extension has been provided
- if(bits.length < 2) {
- this.$input
- .removeClass('input-success input-error')
- .addClass('input-warning');
- this.cantSubmit = 'Impossible de valider: extension (.html/.js/.css) manquante';
- return;
- }
- // show error if provided extension is invalid
- else {
- var ext = (bits[lastIdx]).toLowerCase();
- console.log(bits);
- if( ['html', 'js', 'css'].indexOf( ext ) === -1 ) {
- this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
- }
- else if(bits[0] === '') {
- this.cantSubmit = "Le premier caractère du nom de fichier doit être autre chose qu'un point";
- }
- if( this.cantSubmit ) {
- this.$input
- .removeClass('input-success input-warning')
- .addClass('input-error');
- return;
- }
- }
- this.$input.addClass('input-success');
- this.cantSubmit = undefined;
- }
- this.$input.on('keyup', onKeyup.bind(this));
- }
- }
- );
- console.log(_ws.ui);
- // _ws.ui.addRepoForm.$btn.click(_ws.ui.addRepoForm.render);
- // console.log($addExampleBtn);
- // $addExampleBtn.click(toggleEditor);
- // $exampleCancel.click(clearAndCloseEditor);
- // $exampleForm.submit(addExample);
- });
- })(jQuery);
|