/js/seepy.js
JavaScript | 510 lines | 413 code | 74 blank | 23 comment | 73 complexity | afe56a5670fd91a2102e5c345a7d1261 MD5 | raw file
- (function() {
- //*************
- //***HELPERS***
- //*************
- $.event.props = $.event.props.join('|').replace('layerX|layerY|', '').split('|');
- //Global identifier
- var global_id = 0;
- //String helper functions for parseCode
- if (typeof String.prototype.startsWith != 'function') {
- String.prototype.startsWith = function (str){
- return this.indexOf(str) == 0;
- };
- String.prototype.after = function (str){
- return this.replace(str, "");
- };
- String.prototype.between = function (start, end){
- var startI = this.indexOf(start)+start.length;
- var endI = this.indexOf(end);
- return this.substring(startI, endI);
- };
- String.prototype.until = function (end){
- return this.substr();
- };
- String.prototype.contains = function (str){
- if(this.indexOf(str) != -1) {
- return true;
- }
- else {
- return false;
- }
- };
- }
- //************
- //***MODELS***
- //************
- Class = Backbone.Model.extend({
- defaults: function() {
- return {
- name: "New Class",
- code: "",
- rendered: false
- }
- },
- renameClass : function(new_name) {
- var code = this.get('code');
- var myData = code.split('\n');
- for(var i = 0; i < myData.length; i++) {
- if(myData[i].startsWith("class ")) {
- myData[i] = myData[i].replace(this.get('name'), new_name);
- break;
- }
- }
- var newCode = myData.join("\n");
- this.set('name', new_name);
- this.set('code', newCode);
- editor.setValue(this.get('code'));
- currViewing = this.cid;
- },
- newVariable : function(new_model) {
- var code = this.get('code');
- var myData = code.split('\n');
- var initLine = -1;
- for(var i = 0; i < myData.length; i++) {
- if(myData[i].startsWith("\tdef __init__")) initLine = i++;
- }
- initLine++;
- console.log(initLine);
- while(myData[initLine].startsWith("\t\t")) {
- initLine++;
- }
- var newLine = new Array();
- newLine.push("\t\tself."+new_model.get('name')+" = "+new_model.get('name'));
- myData = myData.slice(0, initLine).concat(newLine).concat(myData.slice(initLine, myData.length+1));
- var newCode = myData.join("\n");
- this.parseCode(newCode);
- editor.setValue(this.get('code'));
- currViewing = this.cid;
- },
- renameVariable : function(model, new_name) {
- var code = this.get('code');
- var myData = code.split('\n');
- var line = myData[model.get('line')];
- myData[model.get('line')] = line.replace(model.get('name'), new_name);
- var newCode = myData.join("\n");
- model.set('name', new_name);
- this.set('code', newCode);
- editor.setValue(this.get('code'));
- currViewing = this.cid;
- },
- deleteVariable : function(model) {
- //console.log("Removing variable '"+model.get('name')+"' on line "+model.get('line'));
- var code = this.get('code');
- var myData = code.split('\n');
- myData = myData.slice(0, model.get('line')).concat(myData.slice(model.get('line')+1, myData.length+1));
- //myData[model.get('line')-1] = myData[model.get('line')+1].replace("\n", "");
- var newCode = myData.join("\n");
- this.parseCode(newCode);
- editor.setValue(this.get('code'));
- currViewing = this.cid;
- },
- newFunction : function(new_model) {
- var code = this.get('code');
- var myData = code.split('\n');
- myData.push("\n\tdef "+new_model.get('name')+"("+new_model.get('parameters').join(", ")+"):\n");
- var newCode = myData.join("\n");
- this.parseCode(newCode);
- editor.setValue(this.get('code'));
- currViewing = this.cid;
- },
- editFunction : function(old_model, new_model) {
- var code = this.get('code');
- var myData = code.split('\n');
- var line = myData[old_model.get('line')];
- myData[old_model.get('line')] = line.replace(old_model.get('name'), new_model.get('name'));
- line = myData[old_model.get('line')];
- myData[old_model.get('line')] = line.replace("("+old_model.get('parameters').join(", ")+")", "("+new_model.get('parameters').join(", ")+")");
- var newCode = myData.join("\n");
- old_model.set('name', new_model.get('name'));
- this.set('code', newCode);
- editor.setValue(this.get('code'));
- currViewing = this.cid;
- },
- deleteFunction : function(model) {
- //console.log("Removing function '"+model.get('name')+"' on line "+model.get('line'));
- var code = this.get('code');
- var myData = code.split('\n');
- var funcLines = 1;
- for(var i = model.get('line')+1; i < myData.length; i++) {
- if(myData[i].startsWith("\t\t")) {
- funcLines++;
- }
- else {
- break;
- }
- }
- myData = myData.slice(0, model.get('line')).concat(myData.slice(model.get('line')+funcLines+1, myData.length+1));
- var newCode = myData.join("\n");
- this.parseCode(newCode);
- editor.setValue(this.get('code'));
- currViewing = this.cid;
- },
- parseCode : function(data) {
- if(this.get('code') == data) return;
- var i = 0;
- var myData = data.split('\n');
- var className = "New Class";
- var functions = new Array;
- var variables = new Array;
- for(i = 0; i < myData.length; i++) {
- var line = myData[i];
- if(line.startsWith("class") && line.contains(":")) {
- className = line.between("class ", ":");
- }
- else if(line.startsWith("\tdef") && line.contains("(") && line.contains(")") && line.contains(":")) {
- var funcName = line.between("\tdef ", "(");
- var perm = "+";
- if(funcName.startsWith("_")) {
- perm = "-";
- }
- var funcParams = line.between("(", ")").replace(" ", "").split(",");
- functions.push({name: funcName, parameters: funcParams, permission: perm, line: i++});
- if(funcName == "__init__") {
- line = myData[i];
- while(!line.startsWith("\tdef")) {
- if(line.startsWith("\t\tself.") && line.contains("=")) {
- var varName = line.between("\t\tself.", " =");
- variables.push({name: varName, line: i});
- }
- i++;
- line = myData[i];
- }
- i--;
- }
- }
- }
- this.functions = new Functions(functions);
- this.variables = new Variables(variables);
- this.inheritances = new Inheritances;
- this.set({name: className, code: data});
- return this;
- }
- });
- Variable = Backbone.Model.extend({
- defaults : function() {
- return {
- name : "newVar"
- }
- },
- initialize: function() {
- this.set('id', this.cid);
- //console.log("New Variable: "+this.get('name')+" "+this.get("id"));
- }
- });
- FunctionM = Backbone.Model.extend({
- defaults : function() {
- return {
- name: "newFunc",
- permission: "+"
- }
- },
- initialize: function() {
- this.set('id', this.cid);
- //console.log("New Function: "+this.get('name')+" "+this.get("id"));
- this.parameters = new Array;
- }
- });
- Inheritance = Backbone.Model.extend({
- defaults : function() {
- return {
- }
- }
- });
- //*****************
- //***COLLECTIONS***
- //*****************
- Classes = Backbone.Collection.extend({
- model : Class
- });
- Variables = Backbone.Collection.extend({
- model : Variable
- });
- Functions = Backbone.Collection.extend({
- model : FunctionM
- });
- Parameters = Backbone.Collection.extend({
- model : Variable
- });
- Inheritances = Backbone.Collection.extend({
- model : Inheritance
- });
- //***********
- //***VIEWS***
- //***********
- ClassesView = Backbone.View.extend({
- initialize: function() {
- this.collection.bind('reset', this.render, this);
- this.collection.bind('add', this.render, this);
- this.collection.bind('remove', this.render, this);
- this.render();
- },
- render: function() {
- this.collection.each(function(aClass) {
- if(!aClass.get('rendered')) {
- this.renderClass(aClass);
- aClass.set({rendered: true});
- }
- }, this);
- },
- renderClass: function(aClass) {
- var classView = new ClassView({model: aClass});
- this.$el.append(classView.render().el);
- }
- });
- ClassView = Backbone.View.extend({
- tagName: "div",
- className: "class",
- initialize: function() {
- this.id = this.model.cid;
- this.$el.attr('id', this.id);
- this.model.bind('change', this.render, this);
- },
- render: function() {
- var _this = this;
- var template = _.template( $("#classTemplate").html(), this.model.toJSON());
- this.el.innerHTML = template;
- this.$el
- .draggable({stack: ".class", containment: "#class_container"})
- .click(function() {
- var cid = $(this).attr('id');
- var bClass = classes.getByCid(cid);
- if(!(currViewing == cid)) {
- editor.setValue(bClass.get('code'));
- currViewing = bClass.cid;
- }
- })
- .contextMenu({menu: 'classMenu'}, function(action, el, pos) {
- var id = $(el).attr('id');
- var model = _this.model;
- if(action == "rename_class") {
- var answer = prompt("Rename class", model.get('name'));
- if(answer) _this.model.renameClass(answer);
- }
- else if(action == "delete_class") {
- var answer = confirm("Are you sure you want to delete class '"+model.get('name')+"'?");
- if(answer) {
- $("#"+_this.model.cid).remove();
- classes.remove(_this.model);
- editor.setValue("");
- }
- }
- else if(action == "new_variable") {
- var answer = prompt("New variable", "");
- if(answer) _this.model.newVariable(new Variable({name: answer}));
- }
- else if(action == "new_function") {
- var name = prompt("Function name", "");
- if(name) {
- var parameters = prompt("Function parameters", "")
- parameters = parameters.replace("(", "").replace(")", "").replace(" ", "").split(",");
- }
- if(name) _this.model.newFunction(new FunctionM({name: name, parameters: parameters}));
- }
- });
- //Variables
- var varsView = new VariablesView({collection: this.model.variables, el: this.$el.find(".var_list")});
- varsView.render();
- //Context Menu
- _this.$el.find(".variable").contextMenu({menu: 'variableMenu'}, function(action, el, pos) {
- var id = $(el).attr('id');
- var model = _this.model.variables.get(id);
- if(action == "rename_variable") {
- var answer = prompt("Rename variable", model.get('name'));
- if(answer) _this.model.renameVariable(model, answer);
- }
- else if(action == "delete_variable") {
- var answer = confirm("Are you sure you want to delete variable '"+model.get('name')+"'?");
- if(answer) _this.model.deleteVariable(model);;
- }
- else if(action == "new_variable") {
- var answer = prompt("New variable", "");
- if(answer) _this.model.newVariable(new Variable({name: answer}));
- }
- });
- //Functions
- var funcsView = new FunctionsView({collection: this.model.functions, el: this.$el.find(".func_list")});
- funcsView.render();
- //Context Menu
- _this.$el.find(".function").contextMenu({menu: 'functionMenu'}, function(action, el, pos) {
- var id = $(el).attr('id');
- var model = _this.model.functions.get(id);
- if(action == "edit_function") {
- var name = prompt("Function name", model.get('name'));
- if(name) {
- var parameters = prompt("Function parameters", model.get('parameters').join(", "));
- parameters = parameters.replace("(", "").replace(")", "").replace(" ", "").split(",");
- }
- if(name) _this.model.editFunction(model, new FunctionM({name: name, parameters: parameters}));
- }
- else if(action == "delete_function") {
- var model = _this.model.functions.get(id);
- var answer = confirm("Are you sure you want to delete function '"+model.get('name')+"'?");
- if(answer) _this.model.deleteFunction(model);
- }
- else if(action == "new_function") {
- var name = prompt("Function name", "");
- if(name) {
- var parameters = prompt("Function parameters", "")
- parameters = parameters.replace("(", "").replace(")", "").replace(" ", "").split(",");
- }
- if(name) _this.model.newFunction(new FunctionM({name: name, parameters: parameters}));
- }
- });
- return this;
- }
- });
- VariablesView = Backbone.View.extend({
- initialize: function() {
- this.collection.bind('reset', this.render, this);
- this.collection.bind('add', this.render, this);
- this.collection.bind('remove', this.render, this);
- },
- render: function() {
- var _this = this;
- _this.$el.empty();
- if(_this.collection.size() <= 0) {
- this.el.innerHTML += "<div class='plain'>No Variables</div>";
- }
- _this.collection.each(function(variable) {
- var v = new VariableView({model: variable, el: _this.el});
- v.render();
- });
- }
- });
- VariableView = Backbone.View.extend({
- initialize: function() {
- this.model.bind('change', this.render, this);
- },
- render: function() {
- var template = _.template( $("#variableTemplate").html(), this.model.toJSON());
- this.el.innerHTML += template;
- return this;
- }
- });
- FunctionsView = Backbone.View.extend({
- initialize: function() {
- this.collection.bind('reset', this.render, this);
- this.collection.bind('add', this.render, this);
- this.collection.bind('remove', this.render, this);
- },
- render: function() {
- var _this = this;
- _this.$el.empty();
- if(_this.collection.size() <= 0) {
- this.el.innerHTML += "<div class='plain'>No Functions</div>";
- }
- _this.collection.each(function(func) {
- var f = new FunctionView({model: func, el: _this.el});
- f.render();
- });
- }
- });
- FunctionView = Backbone.View.extend({
- initialize: function() {
- this.model.bind('change', this.render, this);
- },
- render: function() {
- var template = _.template( $("#functionTemplate").html(), this.model.toJSON());
- this.el.innerHTML += template;
- return this;
- }
- });
- ParametersView = Backbone.View.extend({
- initialize: function() {
- this.collection.bind('reset', this.render, this);
- this.collection.bind('add', this.render, this);
- var that = this;
- },
- render: function() {
- var that = this;
- this.collection.each(function(param) {
- var p = new ParameterView({model: param, el: $(".param_list") });
- p.render();
- });
- }
- });
- ParameterView = Backbone.View.extend({
- initialize: function() {
- this.model.bind('change', this.render, this);
- },
- render: function() {
- var template = _.template( $("#parameterTemplate").html(), this.model.toJSON());
- this.el.innerHTML += template;
- return this;
- }
- });
- }).call(this);