Skip to content

Commit

Permalink
Refactor middleware handling (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Dec 31, 2016
1 parent 11112e7 commit 87f0553
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 64 deletions.
3 changes: 2 additions & 1 deletion htdocs/frontend/index.html
Expand Up @@ -60,6 +60,7 @@
<script type="text/javascript" src="vendor/autobahn.min.js"></script>

<script type="text/javascript" src="javascripts/init.js"></script>
<script type="text/javascript" src="javascripts/middleware.js"></script>
<script type="text/javascript" src="javascripts/functions.js"></script>
<script type="text/javascript" src="javascripts/entities.js"></script>
<script type="text/javascript" src="javascripts/wui.js"></script>
Expand Down Expand Up @@ -195,7 +196,7 @@ <h3><img src="images/blank.png" class="icon-wrench" alt="" /> Optionen</h3>
<form method="get" target="_blank">
<table>
<tr class="property"><th>Eigenschaft</th><th>Wert</th></tr>
<tr class="property"><td>Middleware:</td><td><input type="text" id="entity-create-middleware" /></td></tr>
<tr class="property"><td>Middleware:</td><td><select id="entity-create-middleware"></select></td></tr>
<tr class="property"><td>Typ:</td><td><select class="icons" name="type" size="1"></select></td></tr>
</table>
<p><input type="submit" value="Erstellen" /> <label for="entity-create-cookie">Cookie:</label> <input id="entity-create-cookie" type="checkbox" checked="checked" /></p>
Expand Down
2 changes: 1 addition & 1 deletion htdocs/frontend/javascripts/entities.js
Expand Up @@ -79,7 +79,7 @@ vz.entities.loadDetails = function() {
return $.Deferred().resolveWith(this, [xhr.responseJSON]);
}
vz.wui.dialogs.middlewareException(xhr);
return $.Deferred().rejectWith(this, [xhr]);
return vz.load.errorHandler(xhr);
}
));
}, true); // recursive
Expand Down
6 changes: 3 additions & 3 deletions htdocs/frontend/javascripts/entity.js
Expand Up @@ -188,7 +188,7 @@ Entity.prototype.updateAxisScale = function() {
* WAMP session subscription and handler
*/
Entity.prototype.subscribe = function(session) {
var mw = vz.getMiddleware(this.middleware);
var mw = vz.middleware.find(this.middleware);
if (mw && mw.session) {
session = session || mw.session;
}
Expand Down Expand Up @@ -247,7 +247,7 @@ Entity.prototype.subscribe = function(session) {
* Cancel live update subscription from WAMP server
*/
Entity.prototype.unsubscribe = function() {
var mw = vz.getMiddleware(this.middleware);
var mw = vz.middleware.find(this.middleware);
if (mw.session) {
mw.session.unsubscribe(this.uuid);
}
Expand Down Expand Up @@ -759,7 +759,7 @@ Entity.prototype.updateDOMRowTotal = function() {
.text(vz.wui.formatNumber(this.totalconsumption, unit, 'k'));
}
else {
$('.total', row).data('total', 0).text('');
$('.total', row).data('total', 0).text('');
}
};

Expand Down
32 changes: 11 additions & 21 deletions htdocs/frontend/javascripts/functions.js
Expand Up @@ -125,14 +125,21 @@ vz.load = function(args, skipDefaultErrorHandling) {
null,
// error
function(xhr) {
if (!skipDefaultErrorHandling) {
vz.wui.dialogs.middlewareException(xhr);
}
return $.Deferred().rejectWith(this, [xhr]);
return vz.load.errorHandler(xhr, skipDefaultErrorHandling);
}
);
};

/**
* Reusable authorization-aware error handler
*/
vz.load.errorHandler = function(xhr, skipDefaultErrorHandling) {
if (!skipDefaultErrorHandling) {
vz.wui.dialogs.middlewareException(xhr);
}
return xhr;
};

/**
* Parse URL GET parameters
*/
Expand Down Expand Up @@ -208,23 +215,6 @@ vz.parseUrlParams = function() {
}
};

/**
* Get middleware by URL param
*/
vz.getMiddleware = function(url) {
var mw = $.grep(vz.middleware, function(middleware) {
if (url == middleware.url) {
return true;
}
});

if (mw.length) {
return mw[0];
}

return null;
};

/**
* Load capabilities from middleware
*/
Expand Down
10 changes: 4 additions & 6 deletions htdocs/frontend/javascripts/init.js
Expand Up @@ -63,11 +63,7 @@ $(document).ready(function() {

// middleware(s)
vz.options.middleware.forEach(function(middleware) {
vz.middleware.push($.extend(middleware, {
public: [ ], // public entities
session: null // WAMP session
/* capabilities: { } */
}));
vz.middleware.push(new Middleware(middleware));
});

// TODO make language/translation dependent (vz.options.language)
Expand Down Expand Up @@ -115,8 +111,10 @@ $(document).ready(function() {
vz.entities.loadTotalConsumption();
});

// create WAMP sessions for each middleware
// create WAMP session and load public entities for each middleware
vz.middleware.forEach(function(middleware) {
middleware.loadEntities();

var parser = document.createElement('a');
parser.href = middleware.url;
var host = parser.hostname || location.host; // location object for IE
Expand Down
71 changes: 71 additions & 0 deletions htdocs/frontend/javascripts/middleware.js
@@ -0,0 +1,71 @@
/**
* Middleware handling
*
* @author Andreas Götz <cpuidle@gmx.de>
* @copyright Copyright (c) 2017, The volkszaehler.org project
* @package default
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Middleware constructor
* @var middleware definition
*/
var Middleware = function(definition) {
this.title = definition.title;
this.url = definition.url;
this.live = definition.live || null;

this.public = [];
this.session = null;
};

/**
* Load public entities
*/
Middleware.prototype.loadEntities = function() {
return vz.load({
controller: 'entity',
url: this.url,
context: this
}).then(function(json) {
this.public = [];

json.entities.forEach(function(json) {
var entity = new Entity(json, this.url);
this.public.push(entity);
}, this);
this.public.sort(Entity.compare);

// chainable
return this;
});
};

/**
* Find middleware by url
*/
vz.middleware.find = function(url) {
var mw = $.grep(vz.middleware, function(middleware) {
if (url == middleware.url) {
return true;
}
});

return mw.length ? mw[0] : null;
};
41 changes: 9 additions & 32 deletions htdocs/frontend/javascripts/wui.js
Expand Up @@ -158,36 +158,20 @@ vz.wui.addEntity = function(entity) {
vz.wui.dialogs.init = function() {
// add middlewares
vz.middleware.forEach(function(middleware, idx) {
$('#entity-public-middleware').append($('<option>').val(middleware.url).text(middleware.title));
$('#entity-public-middleware, #entity-create-middleware').append($('<option>').val(middleware.url).text(middleware.title));
});

// initialize dialogs
$('#entity-add.dialog').dialog({
title: unescape('Kanal hinzuf%FCgen'),
title: unescape('Kanal hinzufügen'),
width: 650,
resizable: false,
open: function() {
// lazy load public entities
var title = $('#entity-public-middleware option:selected').text();

vz.middleware.forEach(function(middleware, idx) {
vz.load({
controller: 'entity',
url: middleware.url
}).done(function(json) {
var public = [];
json.entities.forEach(function(json) {
var entity = new Entity(json, middleware.url);
public.push(entity);
});

public.sort(Entity.compare);
vz.middleware[idx].public = public;

if (middleware.title == title) {
populateEntities(vz.middleware[idx]);
}
});
// populate and refresh public entities
var middleware = vz.middleware.find($('#entity-public-middleware option:selected').val());
populateEntities(middleware);
middleware.loadEntities().done(function(middleware) {
populateEntities(middleware);
});
}
});
Expand All @@ -207,19 +191,12 @@ vz.wui.dialogs.init = function() {

// set defaults
$('#entity-subscribe-middleware').val(vz.options.middleware[0].url);
$('#entity-create-middleware').val(vz.options.middleware[0].url);
$('#entity-subscribe-cookie').attr('checked', 'checked');
$('#entity-public-cookie').attr('checked', 'checked');

// actions
$('#entity-public-middleware').change(function() {
var title = $('#entity-public-middleware option:selected').text();
vz.middleware.forEach(function(middleware) {
// populate entities for selected middleware
if (middleware.title == title) {
populateEntities(middleware);
}
});
populateEntities(vz.middleware.find($('#entity-public-middleware option:selected').val()));
});

$('#entity-subscribe input[type=button]').click(function() {
Expand Down Expand Up @@ -266,7 +243,7 @@ vz.wui.dialogs.init = function() {
});
}

$('#entity-create select').change(function() {
$('#entity-create select[name=type]').change(function() {
$('#entity-create form table .required').remove();
$('#entity-create form table .optional').remove();

Expand Down

0 comments on commit 87f0553

Please sign in to comment.