From ade21b2f3b22bc064bd35421fd6b66fc0d082457 Mon Sep 17 00:00:00 2001 From: Anon Ray Date: Thu, 23 Jan 2014 15:10:24 +0530 Subject: [PATCH] Fix: JS/CSS plugin getting executed when loading up for editing - Currently implemented is a hacky workaround around this problem. See models.js > Plugin Model > getCode function. - Small fix in editor to not take empty strings as valid tags. --- mouchak/server.py | 2 +- mouchak/static/js/editor.js | 23 ++++++++++++++--------- mouchak/static/js/models.js | 17 ++++++++++++++++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/mouchak/server.py b/mouchak/server.py index 1cccfda..3ad576b 100644 --- a/mouchak/server.py +++ b/mouchak/server.py @@ -113,7 +113,6 @@ def index(): def edit(): if "logged_in" in flask.session: flask.session['key'] = conf.SECRET_KEY - #print getContent() return flask.render_template('editor.html', content=getContent(), title=conf.SITE_TITLE) else: @@ -142,6 +141,7 @@ def listPages(): return flask.make_response(json.dumps(content), '200 OK', {'Content-Type': 'application/json'}) + @app.route('/pages/<_id>', methods=['GET']) def listPage(_id): try: diff --git a/mouchak/static/js/editor.js b/mouchak/static/js/editor.js index 824f709..8649a23 100644 --- a/mouchak/static/js/editor.js +++ b/mouchak/static/js/editor.js @@ -204,7 +204,7 @@ split('-')[1]; var content = this.model.get('content')[idx]; content = new M.types.model[content.type](content); - console.log('model inited ', content); + //console.log('model inited ', content); this.editing = true; this.edit_idx = idx; var contentview = new ContentView({model: content}); @@ -256,11 +256,13 @@ this.render(); return false; }, - updatePage: function() { + updatePage: function(event) { + event.preventDefault(); var name = $('#name').val(); var title = $('#title').val(); - var children = $('#children').val(); - children = (children === '') ? [] : children.split(','); + var children = []; + //var children = $('#children').val(); + //children = (children === '') ? [] : children.split(','); this.model.set({'name': name, 'title': title, 'children': children}); if($('#showNav').is(':checked')) { @@ -374,7 +376,9 @@ if(this.model.get('src')) { var plugin_type = this.model.get('plugin_type'); plugin_type = (plugin_type === 'js') ? 'javascript' : 'css'; + //console.log('getting code..'); this.model.getCode(function(data) { + //console.log('got code..'); $('#plugin-edit').html(escapeHtml(data)); M.editor.code.init('plugin-edit', plugin_type); }); @@ -417,7 +421,7 @@ $('#contentview [m-data-target]').each(function(idx, elem) { prop = $(elem).attr('m-data-target'); if(prop === 'tags') { - val = $(elem).val().split(','); + val = ($(elem).val()) ? $(elem).val().split(',') : []; } else { val = $(elem).val(); @@ -438,10 +442,11 @@ else if(this.$select.val() === 'plugin') { var data = M.editor.code.save('plugin-edit'); this.model.saveCode(data, function(resp) { - console.log('plugin saved..'); + //console.log('plugin saved..'); }); } this.model.set(new_attrs); + //console.log('content updated'); M.editor.pageview.updateContent(this.model.toJSON()); }, cleanUp: function() { @@ -462,9 +467,9 @@ var self = this; M.editor.showOverlay(); var $form = $('#plugin-upload-form')[0]; - console.log($form); + //console.log($form); var formdata = new FormData($form); - console.log(formdata); + //console.log(formdata); $.ajax({ type: 'POST', url: M.PluginUploadURL(), @@ -475,7 +480,7 @@ self.model.set({'src': response.path}) self.render(); M.editor.hideOverlay(); - console.log(self.model.toJSON()); + //console.log(self.model.toJSON()); } }); } diff --git a/mouchak/static/js/models.js b/mouchak/static/js/models.js index 8984920..0e5a0b8 100644 --- a/mouchak/static/js/models.js +++ b/mouchak/static/js/models.js @@ -115,12 +115,27 @@ // get the source code of the plugin from the src path getCode: function(cb) { var self = this; + //NOTE: jQuery executes the code(css or js) as soon as it is returned by + //the server. Apparently, there is no way to tell jQuery not to execute + //the piece of code retrieved. + //Hecnce, right now its a HACK to workaround this problem. + // We use dataFilter which is called by jQuery to process the raw xhr + // response sent, and jQuery expects back a filtered/sanitized data so + // that it can call the success callback with that data. We use + // dataFilter to do our processing there, and return an empty string; + // this apparently prevents jQuery from executing the code. + // TODO: find a better way to workaround this. + // TODO: find out how cloud-based IDEs load up code files for editing. $.ajax({ type: 'GET', url: self.get('src'), + dataFilter: function(data, type) { + cb(data); + return ''; + }, cache: false, success: function(data) { - cb(data); + //cb(data); } }); }, -- 1.7.10.4