loleaflet: Split admin JS files into multiple

And then combine into one during build for distributing. We would
also want to minify the combined JS file but that is for later.

This come up rather early than anticipated, but good for us.

Change-Id: I74feb4955c5d47f0e9e817b410726ad219add485
This commit is contained in:
Pranav Kant 2016-03-05 19:26:03 +05:30
parent f8a21a783e
commit ee54f2abbb
6 changed files with 233 additions and 189 deletions

View file

@ -0,0 +1,28 @@
var adminDeps = {
Base: {
src: ['admin/Base.js'],
desc: 'Base.js used for JS Inheritence.'
},
Util: {
src: ['admin/Util.js'],
desc: 'Utility class',
deps: ['Base']
},
AdminSocketBase: {
src: ['admin/AdminSocketBase.js'],
desc: 'Abstract base class for Admin sockets',
deps: ['Base']
},
AdminSocketOverview: {
src: ['admin/AdminSocketOverview.js'],
desc: 'Socket to handle messages in overview page.',
deps: ['AdminSocketBase']
}
};
if (typeof exports !== 'undefined') {
exports.adminDeps = adminDeps;
}

View file

@ -3,6 +3,7 @@ var fs = require('fs'),
zlib = require('zlib'),
deps = require('./deps.js').deps;
adminDeps = require('./adminDeps.js').adminDeps;
function getFiles(compsBase32) {
var memo = {},
@ -43,6 +44,18 @@ function getFiles(compsBase32) {
return files;
}
function getAdminFiles() {
var files = [];
for (var i in adminDeps) {
for (var j = 0, len = adminDeps[i].src.length; j < len; j++) {
files.push('src/' + adminDeps[i].src[j]);
}
}
return files;
}
exports.getFiles = getFiles;
function getSizeDelta(newContent, oldContent, fixCRLF) {
@ -105,10 +118,9 @@ exports.build = function (callback, version, compsBase32, buildName) {
console.log('\tSaved to ' + srcPath);
}
// Also copy the admin JS files
// For the time being, just copy the file, since there is only one
var adminCopy = fs.readFileSync('src/admin.js', 'utf-8'),
adminNewSrc = adminCopy,
// Also combine and copy the admin JS files
// TODO: Also minify if admin complexity increases in future
var adminNewSrc = combineFiles(getAdminFiles()),
adminPath = 'dist/admin-src.js',
adminOldSrc = loadSilently(adminPath);

View file

@ -0,0 +1,43 @@
/*
Abstract class
*/
var AdminSocketBase = Base.extend({
socket: null,
constructor: function(host) {
// because i am abstract
if (this.constructor === AdminSocketBase) {
throw new Error('Cannot instantiate abstract class');
}
// We do not allow such child class to instantiate websocket that do not implement
// onSocketMessage and onSocketOpen.
if (typeof this.onSocketMessage === 'function' && typeof this.onSocketOpen === 'function') {
this.socket = new WebSocket(host);
this.socket.onopen = this.onSocketOpen.bind(this);
this.socket.onclose = this.onSocketClose.bind(this);
this.socket.onmessage = this.onSocketMessage.bind(this);
this.socket.onerror = this.onSocketError.bind(this);
this.socket.binaryType = 'arraybuffer';
}
},
onSocketOpen: function() {
/* Implemented by child */
},
onSocketMessage: function() {
/* Implemented by child */
},
onSocketClose: function() {
this.socket.onerror = function() {};
this.socket.onclose = function() {};
this.socket.onmessage = function() {};
this.socket.close();
},
onSocketError: function() {
vex.dialog.alert('Connection error');
}
});

View file

@ -1,188 +1,3 @@
/*
Base.js, version 1.1a
Copyright 2006-2010, Dean Edwards
License: http://www.opensource.org/licenses/mit-license.php
*/
var Base = function() {
// dummy
};
Base.extend = function(_instance, _static) { // subclass
var extend = Base.prototype.extend;
// build the prototype
Base._prototyping = true;
var proto = new this;
extend.call(proto, _instance);
proto.base = function() {
// call this method from any other method to invoke that method's ancestor
};
delete Base._prototyping;
// create the wrapper for the constructor function
//var constructor = proto.constructor.valueOf(); //-dean
var constructor = proto.constructor;
var klass = proto.constructor = function() {
if (!Base._prototyping) {
if (this._constructing || this.constructor == klass) { // instantiation
this._constructing = true;
constructor.apply(this, arguments);
delete this._constructing;
} else if (arguments[0] != null) { // casting
return (arguments[0].extend || extend).call(arguments[0], proto);
}
}
};
// build the class interface
klass.ancestor = this;
klass.extend = this.extend;
klass.forEach = this.forEach;
klass.implement = this.implement;
klass.prototype = proto;
klass.toString = this.toString;
klass.valueOf = function(type) {
//return (type == "object") ? klass : constructor; //-dean
return (type == "object") ? klass : constructor.valueOf();
};
extend.call(klass, _static);
// class initialisation
if (typeof klass.init == "function") klass.init();
return klass;
};
Base.prototype = {
extend: function(source, value) {
if (arguments.length > 1) { // extending with a name/value pair
var ancestor = this[source];
if (ancestor && (typeof value == "function") && // overriding a method?
// the valueOf() comparison is to avoid circular references
(!ancestor.valueOf || ancestor.valueOf() != value.valueOf()) &&
/\bbase\b/.test(value)) {
// get the underlying method
var method = value.valueOf();
// override
value = function() {
var previous = this.base || Base.prototype.base;
this.base = ancestor;
var returnValue = method.apply(this, arguments);
this.base = previous;
return returnValue;
};
// point to the underlying method
value.valueOf = function(type) {
return (type == "object") ? value : method;
};
value.toString = Base.toString;
}
this[source] = value;
} else if (source) { // extending with an object literal
var extend = Base.prototype.extend;
// if this object has a customised extend method then use it
if (!Base._prototyping && typeof this != "function") {
extend = this.extend || extend;
}
var proto = {toSource: null};
// do the "toString" and other methods manually
var hidden = ["constructor", "toString", "valueOf"];
// if we are prototyping then include the constructor
var i = Base._prototyping ? 0 : 1;
while (key = hidden[i++]) {
if (source[key] != proto[key]) {
extend.call(this, key, source[key]);
}
}
// copy each of the source object's properties to this object
for (var key in source) {
if (!proto[key]) extend.call(this, key, source[key]);
}
}
return this;
}
};
// initialise
Base = Base.extend({
constructor: function() {
this.extend(arguments[0]);
}
}, {
ancestor: Object,
version: "1.1",
forEach: function(object, block, context) {
for (var key in object) {
if (this.prototype[key] === undefined) {
block.call(context, object[key], key, object);
}
}
},
implement: function() {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == "function") {
// if it's a function, call it
arguments[i](this.prototype);
} else {
// add the interface using the extend method
this.prototype.extend(arguments[i]);
}
}
return this;
},
toString: function() {
return String(this.valueOf());
}
});
/*
Abstract class
*/
var AdminSocketBase = Base.extend({
socket: null,
constructor: function(host) {
// because i am abstract
if (this.constructor === AdminSocketBase) {
throw new Error('Cannot instantiate abstract class');
}
// We do not allow such child class to instantiate websocket that do not implement
// onSocketMessage and onSocketOpen.
if (typeof this.onSocketMessage === 'function' && typeof this.onSocketOpen === 'function') {
this.socket = new WebSocket(host);
this.socket.onopen = this.onSocketOpen.bind(this);
this.socket.onclose = this.onSocketClose.bind(this);
this.socket.onmessage = this.onSocketMessage.bind(this);
this.socket.onerror = this.onSocketError.bind(this);
this.socket.binaryType = 'arraybuffer';
}
},
onSocketOpen: function() {
/* Implemented by child */
},
onSocketMessage: function() {
/* Implemented by child */
},
onSocketClose: function() {
this.socket.onerror = function() {};
this.socket.onclose = function() {};
this.socket.onmessage = function() {};
this.socket.close();
},
onSocketError: function() {
vex.dialog.alert('Connection error');
}
});
/*
Socket to be intialized on opening the overview page in Admin console
*/

140
loleaflet/src/admin/Base.js Normal file
View file

@ -0,0 +1,140 @@
/*
Base.js, version 1.1a
Copyright 2006-2010, Dean Edwards
License: http://www.opensource.org/licenses/mit-license.php
*/
var Base = function() {
// dummy
};
Base.extend = function(_instance, _static) { // subclass
var extend = Base.prototype.extend;
// build the prototype
Base._prototyping = true;
var proto = new this;
extend.call(proto, _instance);
proto.base = function() {
// call this method from any other method to invoke that method's ancestor
};
delete Base._prototyping;
// create the wrapper for the constructor function
//var constructor = proto.constructor.valueOf(); //-dean
var constructor = proto.constructor;
var klass = proto.constructor = function() {
if (!Base._prototyping) {
if (this._constructing || this.constructor == klass) { // instantiation
this._constructing = true;
constructor.apply(this, arguments);
delete this._constructing;
} else if (arguments[0] != null) { // casting
return (arguments[0].extend || extend).call(arguments[0], proto);
}
}
};
// build the class interface
klass.ancestor = this;
klass.extend = this.extend;
klass.forEach = this.forEach;
klass.implement = this.implement;
klass.prototype = proto;
klass.toString = this.toString;
klass.valueOf = function(type) {
//return (type == "object") ? klass : constructor; //-dean
return (type == "object") ? klass : constructor.valueOf();
};
extend.call(klass, _static);
// class initialisation
if (typeof klass.init == "function") klass.init();
return klass;
};
Base.prototype = {
extend: function(source, value) {
if (arguments.length > 1) { // extending with a name/value pair
var ancestor = this[source];
if (ancestor && (typeof value == "function") && // overriding a method?
// the valueOf() comparison is to avoid circular references
(!ancestor.valueOf || ancestor.valueOf() != value.valueOf()) &&
/\bbase\b/.test(value)) {
// get the underlying method
var method = value.valueOf();
// override
value = function() {
var previous = this.base || Base.prototype.base;
this.base = ancestor;
var returnValue = method.apply(this, arguments);
this.base = previous;
return returnValue;
};
// point to the underlying method
value.valueOf = function(type) {
return (type == "object") ? value : method;
};
value.toString = Base.toString;
}
this[source] = value;
} else if (source) { // extending with an object literal
var extend = Base.prototype.extend;
// if this object has a customised extend method then use it
if (!Base._prototyping && typeof this != "function") {
extend = this.extend || extend;
}
var proto = {toSource: null};
// do the "toString" and other methods manually
var hidden = ["constructor", "toString", "valueOf"];
// if we are prototyping then include the constructor
var i = Base._prototyping ? 0 : 1;
while (key = hidden[i++]) {
if (source[key] != proto[key]) {
extend.call(this, key, source[key]);
}
}
// copy each of the source object's properties to this object
for (var key in source) {
if (!proto[key]) extend.call(this, key, source[key]);
}
}
return this;
}
};
// initialise
Base = Base.extend({
constructor: function() {
this.extend(arguments[0]);
}
}, {
ancestor: Object,
version: "1.1",
forEach: function(object, block, context) {
for (var key in object) {
if (this.prototype[key] === undefined) {
block.call(context, object[key], key, object);
}
}
},
implement: function() {
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == "function") {
// if it's a function, call it
arguments[i](this.prototype);
} else {
// add the interface using the extend method
this.prototype.extend(arguments[i]);
}
}
return this;
},
toString: function() {
return String(this.valueOf());
}
});

View file

@ -0,0 +1,6 @@
/*
Utility class
*/
var Util = Base.extend({
constructor: null
});