From acdd182754625fabcfc2c6ed4bdad8a63bce8ad6 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 16 Sep 2014 00:05:06 +0200 Subject: web: implement EventLogStore --- web/.jshintrc | 3 +- web/gulpfile.js | 1 - web/src/js/Connection.es6.js | 23 +++++---- web/src/js/Dispatcher.es6.js | 4 ++ web/src/js/actions.es6.js | 7 ++- web/src/js/app.js | 1 + web/src/js/components/EventLog.react.js | 21 +++++++- web/src/js/components/Header.react.js | 14 ++--- web/src/js/components/ProxyApp.react.js | 16 +++++- web/src/js/components/TrafficTable.react.js | 33 +++++------- web/src/js/stores/EventLogStore.es6.js | 79 +++++++++++++++++++---------- web/src/js/stores/SettingsStore.es6.js | 23 +-------- web/src/js/stores/base.es6.js | 2 +- 13 files changed, 134 insertions(+), 93 deletions(-) (limited to 'web') diff --git a/web/.jshintrc b/web/.jshintrc index acdf04d3..d5eae073 100644 --- a/web/.jshintrc +++ b/web/.jshintrc @@ -1,4 +1,5 @@ { "loopfunc": true, - "esnext": true + "esnext": true, + "validthis": true } \ No newline at end of file diff --git a/web/gulpfile.js b/web/gulpfile.js index 939f4fcc..3aebcd95 100644 --- a/web/gulpfile.js +++ b/web/gulpfile.js @@ -38,7 +38,6 @@ var path = { 'js/stores/SettingsStore.es6.js', 'js/stores/EventLogStore.es6.js', 'js/Connection.es6.js', - 'js/connection.es6.js', 'js/components/Header.react.js', 'js/components/TrafficTable.react.js', 'js/components/EventLog.react.js', diff --git a/web/src/js/Connection.es6.js b/web/src/js/Connection.es6.js index 9daa82e2..05d86c93 100644 --- a/web/src/js/Connection.es6.js +++ b/web/src/js/Connection.es6.js @@ -1,14 +1,17 @@ -class Connection { - constructor(root){ - if(!root){ +class _Connection { + constructor(root) { + if (!root) { root = location.origin + "/api/v1"; } this.root = root; + } + + init() { this.openWebSocketConnection(); } - openWebSocketConnection(){ - this.ws = new WebSocket(this.root.replace("http","ws") + "/ws"); + openWebSocketConnection() { + this.ws = new WebSocket(this.root.replace("http", "ws") + "/ws"); var ws = this.ws; ws.onopen = this.onopen.bind(this); @@ -17,17 +20,19 @@ class Connection { ws.onclose = this.onclose.bind(this); } - onopen(open){ + onopen(open) { console.log("onopen", this, arguments); } - onmessage(message){ + onmessage(message) { + //AppDispatcher.dispatchServerAction(...); console.log("onmessage", this, arguments); } - onerror(error){ + onerror(error) { console.log("onerror", this, arguments); } - onclose(close){ + onclose(close) { console.log("onclose", this, arguments); } } +var Connection = new _Connection(); \ No newline at end of file diff --git a/web/src/js/Dispatcher.es6.js b/web/src/js/Dispatcher.es6.js index 9bf70878..6ac17f9f 100644 --- a/web/src/js/Dispatcher.es6.js +++ b/web/src/js/Dispatcher.es6.js @@ -33,4 +33,8 @@ AppDispatcher = new Dispatcher(); AppDispatcher.dispatchViewAction = function(action){ action.actionSource = PayloadSources.VIEW_ACTION; this.dispatch(action); +}; +AppDispatcher.dispatchServerAction = function(action){ + action.actionSource = PayloadSources.SERVER_ACTION; + this.dispatch(action); }; \ No newline at end of file diff --git a/web/src/js/actions.es6.js b/web/src/js/actions.es6.js index b6770074..6b3a5870 100644 --- a/web/src/js/actions.es6.js +++ b/web/src/js/actions.es6.js @@ -1,11 +1,14 @@ var ActionTypes = { SETTINGS_UPDATE: "SETTINGS_UPDATE", - LOG_ADD: "LOG_ADD" + EVENTLOG_ADD: "EVENTLOG_ADD" }; var SettingsActions = { update(settings) { - settings = _.merge({}, SettingsStore.getSettings(), settings); + settings = _.merge({}, SettingsStore.getAll(), settings); + //TODO: Update server. + + //Facebook Flux: We do an optimistic update on the client already. AppDispatcher.dispatchViewAction({ actionType: ActionTypes.SETTINGS_UPDATE, settings: settings diff --git a/web/src/js/app.js b/web/src/js/app.js index 2e4557af..2049ef25 100644 --- a/web/src/js/app.js +++ b/web/src/js/app.js @@ -1,5 +1,6 @@ $(function(){ + Connection.init(); app = React.renderComponent(ProxyApp, document.body); }); \ No newline at end of file diff --git a/web/src/js/components/EventLog.react.js b/web/src/js/components/EventLog.react.js index 0ecf40b1..3a7dedc8 100644 --- a/web/src/js/components/EventLog.react.js +++ b/web/src/js/components/EventLog.react.js @@ -1,17 +1,36 @@ /** @jsx React.DOM */ var EventLog = React.createClass({ + getInitialState(){ + return { + log: [] + }; + }, + componentDidMount(){ + this.log = EventLogStore.getView(); + this.log.addListener("change",this.onEventLogChange); + }, + componentWillUnmount(){ + this.log.removeListener("change",this.onEventLogChange); + this.log.close(); + }, + onEventLogChange(){ + this.setState({ + log: this.log.getAll() + }); + }, close(){ SettingsActions.update({ showEventLog: false }); }, render(){ + var messages = this.state.log.map(row =>
{row.message}
); return (
             
-            much log.
+            {messages}
             
); diff --git a/web/src/js/components/Header.react.js b/web/src/js/components/Header.react.js index d1f18a82..41202463 100644 --- a/web/src/js/components/Header.react.js +++ b/web/src/js/components/Header.react.js @@ -1,15 +1,14 @@ /** @jsx React.DOM */ var MainMenu = React.createClass({ - mixins: [SettingsMixin], - handleSettingsChange() { + toggleEventLog() { SettingsActions.update({ - showEventLog: !this.state.settings.showEventLog + showEventLog: !this.props.settings.showEventLog }); }, render(){ return
-
; @@ -46,7 +45,6 @@ var _Header_Entries = { }; var Header = React.createClass({ - mixins: [SettingsMixin], getInitialState(){ return { active: "main" @@ -69,11 +67,13 @@ var Header = React.createClass({ onClick={this.handleClick.bind(this, item)}>{ _Header_Entries[item].title }); } - var menu = _Header_Entries[this.state.active].menu(); + var menu = _Header_Entries[this.state.active].menu({ + settings: this.props.settings + }); return (
- mitmproxy { this.state.settings.version } + mitmproxy { this.props.settings.version }