From b5b75f83a207c00d3582745bd2dc1866639028b6 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 18 Jan 2013 10:47:53 +0100 Subject: Rewrite of javascript engine to be more model-view-controller like. --- task.js | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 task.js (limited to 'task.js') diff --git a/task.js b/task.js new file mode 100644 index 0000000..ac9757f --- /dev/null +++ b/task.js @@ -0,0 +1,123 @@ +/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ + +var tasks = new Array(); + +function findTask(id, observeid) +{ + for(var i = 0; i < tasks.length; i++) { + var task = tasks[i]; + var child = task.findTask(id, observeid); + if(child != null) return child; + } + + return null; +} + +function Task(id, observeid) +{ + this.id = id; + this.observeid = observeid; + this.children = new Array(); + this.attributes = {}; + this.parent = null; + + // Elements: + this.element = document.createElement("div"); + this.div_id = document.createElement("span"); + this.div_title = document.createElement("span"); +} + +Task.prototype.dump = function() +{ + alert(this.id); +} + +Task.prototype.findTask = function(id, observeid) +{ + if(this.observeid != observeid) return null; + + if(this.id == id) return this; + + for(var i = 0; i < this.children.length; i++) { + var task = this.children[i]; + var child = task.findTask(id, observeid); + if(child != null) return child; + } + + return null; +} + +Task.prototype.addChild = function(task) +{ + if(task.parent != null) task.parent.removeChild(task); + this.children.push(task); + task.parent = this; + this.element.appendChild(task.element); +} + +Task.prototype.removeChild = function(task) +{ + this.children = this.children.filter( + function(e) { + return e.id != task.id; + }); + task.parent = null; + this.element.removeChild(task.element); +} + +Task.prototype.create = function() +{ + var task = this.element; + + task.name = "task"; + task.setAttribute("class", "task"); + task.setAttribute("ondblclick", "editTitle(this, event)"); + //task.setAttribute("onclick", "showHideChildren(this, event)"); + task.setAttribute("ondrop", "drop(this, event)"); + task.setAttribute("ondragover", "return false"); + task.setAttribute("draggable", true); + task.setAttribute("ondragstart", "drag(this, event)"); + task.setAttribute("title", this.id); + + task.id = createId(this.observeid, this.id); +/* + var observe_button = document.createElement("div"); + observe_button.name = "observe_button"; + observe_button.setAttribute("onclick", "observeMe(this, event)"); + observe_button.setAttribute("title", this.id); + observe_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid green 2px; cursor: pointer;"); + var txt_plus = document.createTextNode("+"); + observe_button.appendChild(txt_plus); + task.appendChild(observe_button); + + var unobserve_button = document.createElement("div"); + unobserve_button.name = "unobserve_button"; + unobserve_button.setAttribute("onclick", "unobserveMe(this, event)"); + unobserve_button.setAttribute("title", this.id); + unobserve_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid red 2px; cursor: pointer;"); + var txt_minus = document.createTextNode("-"); + unobserve_button.appendChild(txt_minus); + task.appendChild(unobserve_button); +*/ + this.element.appendChild(this.div_id); + var id_txt = document.createTextNode(this.id); + this.div_id.appendChild(id_txt); + + this.element.appendChild(this.div_title); + + this.setAttribute("title", "(missing title)"); +} + +Task.prototype.setAttribute = function(name, value) +{ + this.attributes[name] = value; + + if(name == "title") { + if(this.div_title.firstChild != null) { + this.div_title.removeChild(this.div_title.firstChild); + } + var title_txt = document.createTextNode(value); + this.div_title.appendChild(title_txt); + } +} \ No newline at end of file -- cgit v1.2.3