Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module settings PoC #148

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions controls/js/modules_s.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ angular.module('breach.services').
},
kill: function(path) {
return _req.post('/modules/kill', { path: path });
},
settings_get: function(path) {
return _req.post('/modules/settings_get', { path: path });
},
settings_post: function(path, settings) {
return _req.post('/modules/settings_post', { path: path, settings: settings });
}
};

Expand Down
11 changes: 11 additions & 0 deletions controls/lib/ace/ace.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions controls/lib/ace/mode-json.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions controls/lib/ace/theme-monokai.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions controls/lib/ace/worker-json.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions controls/modules/css/settings.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.settings #editor {
margin: 0;
position: absolute;
top: 30px;
bottom: 0;
left: 0;
right: 0;
}

3 changes: 3 additions & 0 deletions controls/modules/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<link rel="stylesheet" href="../css/main.css" rel="stylesheet"/>
<link rel="stylesheet" href="css/modules.css" rel="stylesheet"/>
<link rel="stylesheet" href="css/out.css" rel="stylesheet"/>
<link rel="stylesheet" href="css/settings.css" rel="stylesheet"/>
</head>

<body ng-controller="TopCtrl">
Expand All @@ -25,9 +26,11 @@
<script src="/lib/angular-route-1.3.0-beta.13.min.js"></script>
<script src="/lib/jquery-2.1.0.min.js"></script>
<script src="/lib/async-0.9.0.min.js"></script>
<script src="/lib/ace/ace.js"></script>

<script src="js/modules_c.js"></script>
<script src="js/out_c.js"></script>
<script src="js/settings_c.js"></script>

<script src="js/app.js"></script>

Expand Down
8 changes: 3 additions & 5 deletions controls/modules/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ angular.module('breach', ['ngRoute',
when('/out/:name',
{ templateUrl: '/modules/partials/out.html',
controller: OutCtrl }).
/*
when('/:name/config',
{ templateUrl: '/modules/partials/config.html',
controller: ConfigCtrl }).
*/
when('/:name/settings',
{ templateUrl: '/modules/partials/settings.html',
controller: SettingsCtrl }).
otherwise({ redirectTo: '/' });
}]);

Expand Down
58 changes: 58 additions & 0 deletions controls/modules/js/settings_c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Breach: [module] out_c.js
*
* Copyright (c) 2014, Stanislas Polu. All rights reserved.
*
* @author: spolu
*
* @log:
* - 2014-07-15 @julien-c Creation
*/
'use strict';

//
// ## App Module
//
angular.module('breach', ['breach.services',
'breach.directives',
'breach.filters']);

//
// ### SettingsCtrl
// Controller to manage module settings
//
function SettingsCtrl($scope, $location, $rootScope, $window, $timeout, $routeParams,
_bind, _modules, _req, _socket) {

/****************************************************************************/
/* INITIALIZATION */
/****************************************************************************/

$window.document.title = 'Settings::' + $routeParams.name;

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/json");
editor.setShowPrintMargin(false);

_modules.settings_get($routeParams.name).then(function(data) {
if (data.ok) {
editor.setValue(JSON.stringify(data.module.settings, null, 2));
editor.selection.clearSelection();
}
});

/****************************************************************************/
/* COMMANDS */
/****************************************************************************/

$scope.settings_save = function() {
var settings = JSON.parse(editor.getValue());

_modules.settings_post($routeParams.name, settings).then(function(data) {
console.log('Settings saved', data);
});
};

};

6 changes: 6 additions & 0 deletions controls/modules/partials/settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="settings">
<div class="header">
<button ng-click="settings_save()">Save</button>
</div>
<div id="editor"></div>
</div>
5 changes: 5 additions & 0 deletions lib/core_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ var core_module = function(spec, my) {
my.session.module_manager().core_expose('store_get', my.core_store.store_get);
my.session.module_manager().core_expose('store_push', my.core_store.store_push);

/* Settings API. */
my.session.module_manager().core_expose('settings_init', my.core_store.settings_init);
my.session.module_manager().core_expose('settings_get', my.core_store.settings_get);
my.session.module_manager().core_expose('settings_store', my.core_store.settings_store);

/* Session API. */
my.session.module_manager().core_expose('session_kill', function(src, args, cb_) {
setTimeout(function() {
Expand Down
45 changes: 45 additions & 0 deletions lib/core_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ var core_store = function(spec, my) {
var store_get; /* store_get(src, args, cb_); */
var store_push; /* store_push(src, args, cb_); */

var settings_init;
var settings_get;
var settings_store;
my.settings = {};

//
// #### _private_
//
Expand Down Expand Up @@ -148,6 +153,42 @@ var core_store = function(spec, my) {
my.session.gig().push('modules', type, path, payload, cb_);
};


// ### settings_init
//
// ```
// @src {string} the source module
// @args {object} { defaults }
// @cb_ {function(err, res)}
// ```
settings_init = function(src, args, cb_) {
/* In-memory mock */
if (my.settings[src]) {
return cb_();
}
else {
my.settings[src] = args.defaults;
return cb_();
}
};

settings_get = function(src, key, cb_) {
/* In-memory mock */
if (key) {
return cb_(null, my.settings[src][key]);
}
else {
/* Return the whole object */
return cb_(null, my.settings[src]);
}
};

settings_store = function(src, settings, cb_) {
my.settings[src] = settings;
return cb_(null, my.settings[src]);
};


/****************************************************************************/
/* INITIALIZATION */
/****************************************************************************/
Expand Down Expand Up @@ -178,6 +219,10 @@ var core_store = function(spec, my) {
common.method(that, 'store_register', store_register, _super);
common.method(that, 'store_get', store_get, _super);
common.method(that, 'store_push', store_push, _super);

common.method(that, 'settings_init', settings_init, _super);
common.method(that, 'settings_get', settings_get, _super);
common.method(that, 'settings_store', settings_store, _super);

return that;
};
Expand Down
9 changes: 5 additions & 4 deletions lib/core_tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
var common = require('./common.js');
var async = require('async');
var api = require('exo_browser');
var urlModule = require('url');

// ## core_tabs
//
Expand Down Expand Up @@ -247,10 +248,10 @@ var core_tabs = function(spec, my) {
// ```
translate_url = function(url) {
/* `breach://` translation. */
var breach_url_r = /^breach\:\/\/(.*)$/;
var breach_url_m = breach_url_r.exec(url);
if(breach_url_m) {
return my.core_module.core_ui().url_for_ui(breach_url_m[1]);
var urlParts = urlModule.parse(url);
if (urlParts.protocol === 'breach:') {
var uri = urlParts.host;
return my.core_module.core_ui().url_for_ui(uri, urlParts.hash);
}
return url;
};
Expand Down
Loading