changeset 2422:d5a721a2c0ef

Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author www-data <www-data@sucuk.dcs.qmul.ac.uk>
date Fri, 27 May 2016 14:20:43 +0100
parents 4718cd881d3a (current diff) a41c985b20a3 (diff)
children d479fdc7221c
files
diffstat 1 files changed, 141 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/test_create/test_core.js	Fri May 27 12:20:51 2016 +0100
+++ b/test_create/test_core.js	Fri May 27 14:20:43 2016 +0100
@@ -1969,8 +1969,46 @@
                     this.parent.specification.options.push(obj.specification);
                     this.parent.childrenDOM.appendChild(obj.rootDOM);
                 }
-                
+                this.parent.children.forEach(function(obj,index){
+                    obj.moveButtons.disable(index);
+                });
             }
+            
+            this.moveButtons = {
+                root_up: document.createElement("button"),
+                root_down: document.createElement("button"),
+                parent: this,
+                handleEvent: function(event) {
+                    var index = this.parent.parent.children.indexOf(this.parent);
+                    if (event.currentTarget.getAttribute("direction") == "up") {
+                        index = Math.max(index-1,0);
+                    } else if (event.currentTarget.getAttribute("direction") == "down") {
+                        index = Math.min(index+1,this.parent.parent.children.length-1);
+                    }
+                    this.parent.moveToPosition(index);
+                    this.disable(index);
+                },
+                disable: function(index) {
+                    if (index == 0) {
+                        this.root_up.disabled = true;
+                    } else {
+                        this.root_up.disabled = false;
+                    }
+                    if (index == this.parent.parent.children.length-1) {
+                        this.root_down.disabled = true;
+                    } else {
+                        this.root_down.disabled = false;
+                    }
+                }
+            }
+            this.moveButtons.root_up.setAttribute("direction","up");
+            this.moveButtons.root_down.setAttribute("direction","down");
+            this.moveButtons.root_up.addEventListener("click",this.moveButtons,false);
+            this.moveButtons.root_down.addEventListener("click",this.moveButtons,false);
+            this.moveButtons.root_up.textContent = "Move Up";
+            this.moveButtons.root_down.textContent = "Move Down";
+            this.buttonDOM.appendChild(this.moveButtons.root_up);
+            this.buttonDOM.appendChild(this.moveButtons.root_down);
         }
         this.addNode = {
             root: document.createElement("button"),
@@ -1994,6 +2032,10 @@
             this.children.push(newNode);
             this.childrenDOM.appendChild(newNode.rootDOM);
         }
+        
+        this.children.forEach(function(obj,index){
+            obj.moveButtons.disable(index);
+        });
     }
     
     this.pageNode = function(parent,rootObject)
@@ -2074,6 +2116,23 @@
         
         this.interfaces = [];
         
+        this.getAudioElements = function() {
+            var array = [];
+            for (var i=0; i<this.children.length; i++) {
+                if (this.children[i].type == "audioElementNode") {
+                    array[array.length] = this.children[i];
+                }
+            }
+            return array;
+        }
+        
+        this.redrawChildren = function() {
+            this.childrenDOM.innerHTML = "";
+            for(var child of this.children) {
+                this.childrenDOM.appendChild(child.rootDOM);
+            }
+        }
+        
         this.audioElementNode = function(parent,rootObject)
         {
             this.type = "audioElementNode";
@@ -2154,6 +2213,82 @@
             this.deleteNode.root.textContent = "Delete Entry";
             this.deleteNode.root.addEventListener("click",this.deleteNode,false);
             this.buttonDOM.appendChild(this.deleteNode.root);
+            
+            this.moveButtons = {
+                root_up: document.createElement("button"),
+                root_down: document.createElement("button"),
+                parent: this,
+                handleEvent: function(event) {
+                    var index = this.parent.parent.getAudioElements().indexOf(this.parent);
+                    if (event.currentTarget.getAttribute("direction") == "up") {
+                        index = Math.max(index-1,0);
+                    } else if (event.currentTarget.getAttribute("direction") == "down") {
+                        index = Math.min(index+1,this.parent.parent.getAudioElements().length-1);
+                    }
+                    this.parent.moveToPosition(index);
+                    this.disable(index);
+                },
+                disable: function(index) {
+                    if (index == 0) {
+                        this.root_up.disabled = true;
+                    } else {
+                        this.root_up.disabled = false;
+                    }
+                    if (index == this.parent.parent.getAudioElements().length-1) {
+                        this.root_down.disabled = true;
+                    } else {
+                        this.root_down.disabled = false;
+                    }
+                }
+            }
+            this.moveButtons.root_up.setAttribute("direction","up");
+            this.moveButtons.root_down.setAttribute("direction","down");
+            this.moveButtons.root_up.addEventListener("click",this.moveButtons,false);
+            this.moveButtons.root_down.addEventListener("click",this.moveButtons,false);
+            this.moveButtons.root_up.textContent = "Move Up";
+            this.moveButtons.root_down.textContent = "Move Down";
+            this.buttonDOM.appendChild(this.moveButtons.root_up);
+            this.buttonDOM.appendChild(this.moveButtons.root_down);
+            
+            this.moveToPosition = function(new_index) {
+                
+                // Get the zero-th Object
+                var zero_object = this.parent.getAudioElements()[0];
+                var parent_children_root_index = this.parent.children.indexOf(zero_object);
+                // splice out the array for processing
+                var process_array = this.parent.children.splice(parent_children_root_index);
+                
+                
+                new_index = Math.min(new_index, process_array.length);
+                var curr_index = process_array.findIndex(function(elem){
+                    if (elem == this) {return true;} else {return false;}
+                },this);
+                
+                // Split at the current location to remove the node and shift all the children up
+                var tail = process_array.splice(curr_index+1);
+                process_array.pop();
+                process_array = process_array.concat(tail);
+                
+                //Split at the new location and insert the node
+                tail = process_array.splice(new_index);
+                process_array.push(this);
+                process_array = process_array.concat(tail);
+                
+                // Re-attach to the parent.children
+                this.parent.children = this.parent.children.concat(process_array);
+                
+                // Re-build the specification
+                this.parent.specification.audioElements = [];
+                for (var obj of process_array) {
+                    this.parent.specification.audioElements.push(obj.specification);
+                }
+                this.parent.redrawChildren();
+                
+                process_array.forEach(function(obj,index){
+                    obj.moveButtons.disable(index);
+                });
+                
+            }
         }
         
         this.commentQuestionNode = function(parent,rootObject)
@@ -2169,7 +2304,7 @@
             this.parent = parent;
             this.specification = rootObject;
             this.schema = specification.schema.getAllElementsByName("page")[0];
-            this.rootDOM.className = "node";
+            this.rootDOM.className = "node audio-element";
 
             var titleDiv = document.createElement('div');
             titleDiv.className = "node-title";
@@ -2207,6 +2342,10 @@
             this.childrenDOM.appendChild(audioElementDOM.rootDOM);
         }
         
+        this.getAudioElements().forEach(function(elem){
+            elem.moveButtons.disable();
+        });
+        
         this.addInterface = {
             root: document.createElement("button"),
             parent: this,