From ab9ceaf3f44cc9df63dfb6c03b93815ef5c8299d Mon Sep 17 00:00:00 2001 From: Victor Dubiniuk Date: Fri, 9 Aug 2013 19:31:20 +0300 Subject: [PATCH] Code deduplication --- ajax/controller.php | 16 ++++++++++++++++ ajax/otpoll.php | 18 ++++++------------ appinfo/routes.php | 9 +++++++++ js/office.js | 10 +++++----- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/ajax/controller.php b/ajax/controller.php index 01c9622b..823eec43 100644 --- a/ajax/controller.php +++ b/ajax/controller.php @@ -67,6 +67,22 @@ class Controller { \OCP\JSON::error(); } + public static function listSessions(){ + self::getUser(); + $sessions = Session::getAll(); + if (!is_array($sessions)){ + $sessions = array(); + } + + $preparedSessions = array_map( + function($x){return ($x['es_id']);}, + $sessions + ); + \OCP\JSON::success(array( + "session_list" => $preparedSessions + )); + } + protected static function getUser(){ \OCP\JSON::checkLoggedIn(); return \OCP\User::getUser(); diff --git a/ajax/otpoll.php b/ajax/otpoll.php index 4c73e3a6..2c7291fe 100644 --- a/ajax/otpoll.php +++ b/ajax/otpoll.php @@ -34,9 +34,10 @@ * @source: http://www.webodf.org/ * @source: http://gitorious.org/webodf/webodf/ */ -// OCP\JSON::checkLoggedIn(); -// OCP\JSON::checkAppEnabled('office'); -// session_write_close(); + + OCP\JSON::checkLoggedIn(); + OCP\JSON::checkAppEnabled('office'); + // session_write_close(); $response = array(); try{ @@ -44,15 +45,8 @@ try{ $command = $request->getParam('command'); switch ($command){ case 'session-list': - $sessions = OCA\Office\Session::getAll(); - if (!is_array($sessions)){ - $sessions = array(); - } - - $response["session_list"] = array_map( - function($x){return ($x['es_id']);}, - $sessions - ); + OCA\Office\Controller::listSessions(); + exit(); break; case 'join-session': $response = "true"; // should fail when session is non-existent diff --git a/appinfo/routes.php b/appinfo/routes.php index 8353a2c3..91bb7919 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -27,6 +27,15 @@ $this->create('office_session_start', 'ajax/session/start') ->action('\OCA\Office\Controller', 'startSession') ; +$this->create('office_session_list', 'ajax/session/list') + ->get() + ->action('\OCA\Office\Controller', 'listSessions') +; +$this->create('office_session_list', 'ajax/session/list') + ->post() + ->action('\OCA\Office\Controller', 'listSessions') +; + $this->create('office_session_join', 'ajax/session/join/{es_id}') ->get() ->action('\OCA\Office\Controller', 'joinSession') diff --git a/js/office.js b/js/office.js index 196f0e4d..06b69878 100644 --- a/js/office.js +++ b/js/office.js @@ -23,7 +23,7 @@ var officeMain = { "use strict"; OC.addScript('office', 'editor/boot_editor').done(function() { - var doclocation = response.genesis_url; + var doclocation = response.es_id; // fade out files menu and add odf menu $('.documentslist, #emptyfolder').fadeOut('slow').promise().done(function() { @@ -97,18 +97,18 @@ var officeMain = { $.post(OC.Router.generate('office_session_list'), {}, officeMain.onSessions); }, onSessions : function(response){ - if (response && response.sessions){ - $(response.sessions).each( function(i, s){ officeMain.addSession(s) } ); + if (response && response.session_list){ + $(response.session_list).each( function(i, s){ officeMain.addSession(s) } ); } }, addSession : function(s){ if (!$('#allsessions').length){ $(document.body).append('
'); } - $('
'+s.es_id+ '
').appendTo('#allsessions').click( + $('
'+s+ '
').appendTo('#allsessions').click( function(event){ event.preventDefault(); - officeMain.joinSession(s); + officeMain.joinSession({es_id : s}); } ); },