| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- "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 makeFormView(elemId, onSubmitPromise, options) {
- // Fallback options & options.events
- options = options || {};
- 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() {
- this.$btn = this.$elem.find('.add-btn');
- this.$form = this.$elem.find('form');
- this.$input = this.$form.find('input[name="title"]');
- console.log('init add repo form', this.$elem.prop('id'))
- },
- render: function() {
- console.log('render form add ', this.$elem.prop('id'));
- this.$btn.removeClass('in');
- this.$form.addClass('in');
- this.$input.focus();
- },
- events: events
- });
- }
- makeFormView('add-repo', function(title) {
- return rp.post('/repos', { title: title });
- });
- makeFormView('add-example', function(title) {
- return rp.post('/' + _ws.repo.path + '/examples', { title: title });
- });
- makeFormView(
- 'add-file',
- function(name) {
- return rp.post('/' + _ws.repo.path + '/examples/file', { name: name });
- },
- {
- events: {
- 'keyup input[name="title"]': function(e) {
- var filename = this.$input.val();
- // console.log('filename', filename);
- var bits = filename.split('.');
- var lastIdx = bits.length - 1;
- console.log(bits, bits.length, lastIdx, bits[lastIdx], typeof bits[lastIdx]);
- 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;
- }
- else {
- var ext = (bits[lastIdx]).toLowerCase();
- console.log(ext);
- if( ['html', 'js', 'css'].indexOf( ext ) === -1 ) {
- this.$input
- .removeClass('input-success input-warning')
- .addClass('input-error');
- this.cantSubmit = 'Impossible de valider: extension ' + ext + ' invalide (autorisées: .html/.js/.css)';
- return;
- }
- }
- this.$input.addClass('input-success');
- this.cantSubmit = undefined;
- }
- }
- }
- );
- 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);
|