Please disable Adblockers and enable JavaScript for domain CEWebS.cs.univie.ac.at! We have NO ADS, but they may interfere with some of our course material.

Name: orgviz-js/lib/jsfiddle.js 
1:
var file = "http://cpee.org/~demo/orgviz/organisation_informatik.xml";                                     var xpath
2:
var subjectsin = "/o:organisation/o:subjects/o:subject";
3:
var nopts = null;
4:
var nodes2 = [];
5:
if (!Array.prototype.last){
6:
  Array.prototype.last = function(){
7:
    return this[this.length - 1];
8:
  };
9:
};
10:
 
11:
function objectEquals(x, y) {
12:
    'use strict';
13:
 
14:
    if (x === null || x === undefined || y === null || y === undefined) { return x === y; }
15:
    // after this just checking type of one would be enough
16:
    if (x.constructor !== y.constructor) { return false; }
17:
    // if they are functions, they should exactly refer to same one (because of closures)
18:
    if (x instanceof Function) { return x === y; }
19:
    // if they are regexps, they should exactly refer to same one (it is hard to better equality check on current ES)
20:
    if (x instanceof RegExp) { return x === y; }
21:
    if (x === y || x.valueOf() === y.valueOf()) { return true; }
22:
    if (Array.isArray(x) && x.length !== y.length) { return false; }
23:
 
24:
    // if they are dates, they must had equal valueOf
25:
    if (x instanceof Date) { return false; }
26:
 
27:
    // if they are strictly equal, they both need to be object at least
28:
    if (!(x instanceof Object)) { return false; }
29:
    if (!(y instanceof Object)) { return false; }
30:
 
31:
    // recursive object equality check
32:
    var p = Object.keys(x);
33:
    return Object.keys(y).every(function (i) { return p.indexOf(i) !== -1; }) &&
34:
        p.every(function (i) { return objectEquals(x[i], y[i]); });
35:
};
36:
 
37:
var Node = function(id,type,opts) {                                                                                  
38:
    this.type     = type;
39:
    this.id       = id;
40:
    this.rank     = 0;
41:
    this.parents  = [];
42:
    this.group    = 0;
43:
    this.numsubjects = 0;
44:
    this.subjects = [];
45:
    //this.twidth   = SVG.width_of(id);
46:
    //this.theight  = SVG.height_of(id);
47:
    //new instance variable for all elements of opts
48:
    for (var i in opts) {
49:
      if(opts.hasOwnProperty(i)) eval("this."+i+" = "+opts[i]+";");
50:
    }
51:
};
52:
 
53:
var Subject = function(shortid){                                                                                    
54:
  this.shortid    = shortid;
55:
  Subject.counter += 1;
56:
  this.id         = "s"+Subject.counter;
57:
  this.relations  = [];
58:
};      
59:
        
60:
Subject.counter = 0;
61:
 
62:
var Relation = function(unit, role){                                                                                
63:
    this.unit = unit;
64:
    this.role = role;
65:
};
66:
 
67:
function onlyUnique(value, index, self) {
68:
    return self.indexOf(value) === index;
69:
}
70:
 
71:
var GraphWorker = function(file,xpath,subjects,nopts){
72:
  this.nodes = [];
73:
  this.subjects = [];
74:
  this.maxsubjects = 0;
75:
  this.paths = [];
76:
 
77:
  this.nsResolver = function(prefix) {
78:
    return prefix == 'o' ? "http://cpee.org/ns/organisation/1.0" : null ;
79:
  };
80:
 
81:
  this.processData = function(data) {
82:
    var evalue = data.evaluate('/o:organisation/o:units/o:unit|/o:organisation/o:roles/o:role',
83:
                               data,
84:
                               nsResolver,
85:
                               XPathResult.ORDERED_NODE_ITERATOR_TYPE,
86:
                               null);
87:
    var node = evalue.iterateNext();
88:
    for(; node && !evalue.invalidIteratorState; ) {
89:
        var type = node.localName;
90:
        var id = node.id;
91:
        var curr = new Node(id, type, nopts);
92:
        var numsubjects = data.evaluate('count(' + subjects.replace(/\/*$/,'') + '[o:relation[@' + type +
93:
                                         '="' + id + '"]])',
94:
                                        data,
95:
                                        nsResolver,
96:
                                        XPathResult.NUMBER_TYPE,
97:
                                        null);
98:
        curr.numsubjects = numsubjects.numberValue;
99:
        if(curr.numsubjects > this.maxsubjects) this.maxsubjects = curr.numsubjects;
100:
 
101:
        nodes2.push(node);
102:
        for(var i = 0; i < node.childNodes.length; ++i) {
103:
          var child = node.childNodes[i];
104:
            if(child.nodeName == "parent") {
105:
                var pa = child.textContent;
106:
                //console.log(node.parentNode.childNodes.length);
107:
                for(var j = 0; j < node.parentNode.childNodes.length; ++j) {
108:
                    var pid = node.parentNode.childNodes[j];
109:
                    if(pid.id == pa) {
110:
                        curr.parents.push(pid);
111:
                    }
112:
                }
113:
            }
114:
        }
115:
        nodes.push(curr);
116:
        node = evalue.iterateNext();
117:
    }
118:
 
119:
    var subjectIterator = data.evaluate(subjects.replace(/\/*$/,''),
120:
                                        data,
121:
                                        nsResolver,
122:
                                        XPathResult.ORDERED_NODE_ITERATOR_TYPE,
123:
                                        null);
124:
    var subject = subjectIterator.iterateNext();
125:
    for( ; subject && !subjectIterator.invalidIteratorState; ) {
126:
      var s = new Subject(subject.id);
127:
      for(var i = 0; i < subject.childNodes.length; ++i) {
128:
        var child = subject.childNodes[i];
129:
        if(child.nodeName == "relation") {
130:
          var unit = nodes.filter(function(e) { return e.id == child.attributes["unit"].nodeValue });
131:
          var role = nodes.filter(function(e) { return e.id == child.attributes["role"].nodeValue });
132:
          unit.subjects = [];
133:
          unit.subjects.push(s);
134:
          unit = unit.filter( onlyUnique );
135:
          role.subjects = [];
136:
          role.subjects.push(s);
137:
          role = role.filter( onlyUnique );
138:
          if(unit && role ) {
139:
            s.relations.push( new Relation(unit,role) );
140:
          }
141:
        }
142:
      }
143:
      this.subjects.push(s);
144:
      subject = subjectIterator.iterateNext();
145:
    }
146:
 
147:
    console.log(this.subjects);
148:
    console.log(nodes);
149:
 
150:
    for(var node of nodes) {
151:
      console.log(node);
152:
      this.paths.push([node]);
153:
      calculate_path(this.paths, this.paths.last());
154:
    }
155:
  };
156:
 
157:
  var calculate_path = function(paths, path) {
158:
    var parents = path.last().parents
159:
    console.log(paths, path);
160:
    if(parents !== undefined && (parents.length == 0 || parents.length == 1) ) {
161:
        console.log(parents[0]);
162:
      if( parents[0] === undefined || path.includes(parents[0]) ) {
163:
          return;
164:
        }
165:
        path.push(parents[0]);
166:
        calculate_path(paths, path);
167:
    } else {
168:
        var tpath = $.extend( true, {}, path );
169:
      for(var p of parents){
170:
            if(tpath.includes(p)){
171:
                continue;
172:
            }
173:
            if(objectEquals(p, parents[0])){
174:
                path.push(p);
175:
                calculate_path(paths, path);
176:
            } else {
177:
                paths.push( ($.extend(true, {}, tpath)).push(p) );
178:
                calculate_path(paths, paths.last());
179:
            }
180:
        }
181:
    }
182:
  };
183:
 
184:
  var client = new XMLHttpRequest();
185:
  client.onload = function() {
186:
    if(this.status == 200 )
187:
    {
188:
      //console.log(this.responseXML);
189:
      processData(this.responseXML);
190:
    } else {
191:
      console.log(this);
192:
    }
193:
  };
194:
  client.open("GET", file, true);
195:
  client.send(null);
196:
};
197:
 
198:
GraphWorker(file,xpath,subjectsin,nopts);