Skip to content
This repository has been archived by the owner on Nov 23, 2017. It is now read-only.

Commit

Permalink
Merge pull request #3 from iKlaus/feature/client-change-group
Browse files Browse the repository at this point in the history
group features
  • Loading branch information
Schubidu committed Jul 16, 2015
2 parents 50737d0 + e7171ab commit ec3aa93
Show file tree
Hide file tree
Showing 15 changed files with 570 additions and 186 deletions.
2 changes: 1 addition & 1 deletion lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Group(data) {
/** @type {Boolean} */
var panicState = false;
/** @type {String} */
var title = data.title || data.id;
var title = data.title || id;
/** @type {Boolean} */
var state = data.state;
/** @type {Array.<CronJob>} */
Expand Down
18 changes: 17 additions & 1 deletion master/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ module.exports = {
cb(e);
}
},
/**
* @param {String} group
* @param {String} which
* @param {Function} cb
*/
changeGroup: function (group, which, cb) {
try {
var client = this.getClient(which, true);
client.group = group;

cb(null, client);
}
catch (e) {
cb(e);
}
},
/**
* @param {String} title
* @param {String} which
Expand Down Expand Up @@ -232,7 +248,7 @@ module.exports = {
/**
* @param {String} id
*
* @return {Client}
* @return {Group}
*/
getGroup: function (id) {
var group = clientPool.getGroupById(id);
Expand Down
29 changes: 29 additions & 0 deletions master/lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var serverService = require('./server');
var socketEvents = require('./socketEvents');
var debug = require('debug')('master:socket');
var SocketIO = require('socket.io');
var Group = require('../../lib/group');

function Socket(server) {
var self = this;
Expand Down Expand Up @@ -71,6 +72,16 @@ function Socket(server) {
});
});

socket.on(socketEvents.CLIENT_MOVED, function (data) {
debug([socketEvents.CLIENT_MOVED, data]);

serverService.changeGroup(data.group, data.id, function (err) {
if (err) {
socketError({id: data.id, message: 'Error on changeGroup!'});
}
});
});

socket.on(socketEvents.GROUP_CHANGETITLE, function (data) {
debug([socketEvents.GROUP_CHANGETITLE, data]);

Expand All @@ -81,6 +92,24 @@ function Socket(server) {
});
});

socket.on(socketEvents.GROUP_CHANGESETTINGS, function (data) {
debug([socketEvents.GROUP_CHANGESETTINGS, data]);

//TODO save schedules in ClientPool
//serverService.changeGroupTitle(data.title, data.id, function (err) {
// if (err) {
// socketError({id: data.id, message: 'Error on changeTitle!'});
// }
//});
});

socket.on(socketEvents.GROUP_ADD, function (data) {
debug([socketEvents.GROUP_ADD, data]);

var clientPool = serverService.getClientPool();
clientPool.addGroup(new Group());
});

serverService.getClientPool().on('clientsUpdated', function () {
self.ioServer.emit(socketEvents.CLIENTS_LIST, serverService.getClientPool());
});
Expand Down
3 changes: 3 additions & 0 deletions master/lib/socketEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
CLIENT_CHANGEPANICURL: 'client:changepanicurl',
CLIENT_CHANGEPANICSTATE: 'client:changepanicstate',
CLIENT_CHANGETITLE: 'client:changetitle',
CLIENT_MOVED: 'client:moved',
GROUP_CHANGETITLE: 'group:changetitle',
GROUP_CHANGESCHEDULES: 'group:changeschedules',
GROUP_ADD: 'group:new',
ERROR: 'error'
};
137 changes: 122 additions & 15 deletions master/public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ require('bootstrap/js/transition');
require('bootstrap/js/collapse');


var CronJob = require('cron').CronJob;

var full = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
global.socket = require('socket.io-client')(full);

Expand All @@ -13,7 +15,7 @@ var socketEvents = require('../../lib/socketEvents');
var clientState = require('./lib/clientState');
var Group = require('./model/group');

var showSettings = true;
window.showSettings = true;
var clientPool;

var doc = window.document.documentElement;
Expand Down Expand Up @@ -62,8 +64,8 @@ function makeHTML(clientPool) {
var html = require('./views/main')({
id: 'all',
title: headline,
settings: showSettings,
editable: !showSettings,
settings: window.showSettings,
editable: !window.showSettings,
groups: groups,
up: groups.reduce(function (carry, group) {
return carry || !!group.up;
Expand All @@ -79,8 +81,8 @@ function makeHTML(clientPool) {

function toggleShowSettings() {
$(doc)
.toggleClass('show-settings', !showSettings)
.toggleClass('hidden-settings', showSettings);
.toggleClass('show-settings', !window.showSettings)
.toggleClass('hidden-settings', window.showSettings);
}

function showClients() {
Expand All @@ -94,6 +96,26 @@ function showClients() {
.toggleClass('hidden', !show);
}

var validate = {
url: function (value) {
return value.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/) !== null;
},
cron: function (value) {
try {
new CronJob(value, function () {
});

return true;
} catch (ex) {
return false;
}
}
};


$.fn.findGroup = function () {
return this.parents('.clients').first();
};

$(function () {
var $waiting = $('.clients-waiting');
Expand Down Expand Up @@ -129,11 +151,74 @@ $(function () {

$(document.body)
.on('click', '.js-settings', function (event) {
showSettings = !showSettings;
window.showSettings = !window.showSettings;
makeHTML(clientPool);
toggleShowSettings();
showClients();
})
.on('click', '.js-add-group', function (event) {

$waiting.toggleClass('active', true);
socket.emit(socketEvents.GROUP_ADD);
})
.on('click', '.js-group-settings', function (event) {
$($(this).data('target')).collapse('toggle');
$(this).toggleClass('active');
})
.on('click', '.js-add-schedule', function (event) {
var $inputGroup = $(this).parents('.input-group').first();
var $inputGroupClone = $inputGroup.clone(true);
$inputGroupClone
.find('.js-add-schedule').toggleClass('js-add-schedule js-remove-schedule')
.find('i').toggleClass('fa-plus fa-minus');
$inputGroup.before($inputGroupClone);
})
.on('click', '.js-remove-schedule', function (event) {
$(this).parents('.input-group').first().remove();
})
.on('submit', '.js-form-settings', function (event) {
event.preventDefault();
event.stopPropagation();

var $clients = $(event.target).parents('.clients').first().find('.client');
var panicUrl,
submitValues = {
id: null,
panicUrl: null,
schedules: {
on: [],
off: []
}
};

$(this).serializeArray().forEach(function (item) {
if (item.value.trim().length > 0) {

switch (item.name) {
case 'id':
submitValues.id = item.value;
break;
case 'panicUrl':
panicUrl = item.panicUrl;
break;
case 'on':
submitValues.schedules.on.push(item.value);
break;
case 'off':
submitValues.schedules.off.push(item.value);
break;
}
}
});

$clients.each(function ($client) {
var data = {id: $client.data('id')};
data['panicUrl'] = panicUrl;
socket.emit(socketEvents.CLIENT_CHANGEPANICURL, data);
});

socket.emit(socketEvents.GROUP_CHANGESCHEDULES, submitValues);
})
.on('click', '[data-action]', function (event) {
event.preventDefault();
event.stopPropagation();
Expand All @@ -154,7 +239,7 @@ $(function () {
});
break;
case 'switch-all':
$(event.target).parents('.clients').first().find('.client').not('[disabled]').each(function (client) {
$(event.target).findGroup().find('.client').not('[disabled]').each(function (client) {
var id = $(this).attr('client');
socket.emit(socketEvents.CLIENT_SWITCH, {
id: id,
Expand All @@ -178,8 +263,15 @@ $(function () {

})
.on('focus blur keyup', '.form-control', function (event) {
var self = this;
var $this = $(this);
var value = this.value;
var emit = function (eventName, data) {
eventName = eventName || $this.data('event');
data = (typeof data === 'undefined') ? {} : data;
data = $.extend(data, {id: $this.data('id')});
data[self.name] = value;
};

if (event.type == 'keyup') {
if (event.keyCode == 13) {
Expand All @@ -195,7 +287,7 @@ $(function () {
}

if (this.type == 'url') {
var isValideUrl = value.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/) !== null;
var isValideUrl = validate.url(value);
$this.toggleClass('has-error', !isValideUrl);
if (!isValideUrl) {
alert("Please enter valid URL!");
Expand All @@ -204,26 +296,41 @@ $(function () {
}
}

if ($this.attr('type') == 'cron') {
if (!validate.cron(value)) {
alert("Please enter valid Schedule!");
$this.focus();
return;
}
}

$this.addClass('has-changed');

if ($this.data('event') == 'client:changeurl-all') {
$(event.target).parents('.clients').first().find('input[data-event="' + socketEvents.CLIENT_CHANGEURL + '"]').each(function () {
$(event.target).findGroup().find('input[data-event="' + socketEvents.CLIENT_CHANGEURL + '"]').each(function () {
this.value = value;
$(this).trigger('blur');
});

$(event.target).parents('.clients').first().find('.js-button-changeurl-all').trigger('click');
$(event.target).findGroup().find('.js-button-changeurl-all').trigger('click');

} else {
var data = {id: $this.data('id')};
data[this.name] = value;
socket.emit($this.data('event'), data);
emit();
}

}

})
.on('show.bs.collapse hidden.bs.collapse', function (event) {
$(event.target).parents('.clients').first().find('.js-button-changeurl-all').filter('[data-target="#' + event.target.id + '"]').toggleClass('active', event.type == 'show');
});

/* drap and drop */

var dragDrop = require('./lib/dragdrop');
dragDrop.on(dragDrop.CLIENT_MOVED, function (client, group) {
socket.emit(socketEvents.CLIENT_MOVED, {
id: $(client).attr('client'),
group: $(group).attr('group')
});
})

});
Loading

0 comments on commit ec3aa93

Please sign in to comment.