Initialize chat js application from controller

This commit is contained in:
Dmitriy Simushev 2014-10-13 13:30:07 +00:00
parent dcd17ed01c
commit 452446a375
4 changed files with 96 additions and 59 deletions

View File

@ -68,4 +68,66 @@ abstract class AbstractController extends BaseAbstractController
return $this->redirect($path);
}
/**
* Generates JavaScript code that starts client side application.
*
* @param Request $request Incomming request.
* @param array $options Client side application options. At the moment the
* method accepts the following options:
* - "company": array, a set of company info. See {@link setup_logo()}
* for details.
* - "mibewHost": string, a URL which is used as a Mibew host. See
* {@link setup_logo()} for details.
* - "page.title": string, a value which will be used as a page title.
* - "startFrom": string, indicates what module should be invoked first.
* - "chatOptions": array, (optional) list of chat module options.
* - "surveyOptions": array, (optional) list of pre-chat survey module
* options.
* - "leaveMessageOptions": array, (optional) list of leave message module
* options.
* - "invitationOptions": array, (optional) list of invitation module
* options.
* @return string JavaScript code that starts "users" client side
* application.
* @todo The way options passed here should be reviewed. The method must get
* finite number of well-structured arguments.
*/
protected function startJsApplication(Request $request, $options)
{
$app_settings = array(
'server' => array(
'url' => $this->generateUrl('chat_thread_update'),
'requestsFrequency' => Settings::get('updatefrequency_chat'),
),
'page' => array(
'style' => $this->getStyle()->getName(),
'mibewBasePath' => $request->getBasePath(),
'mibewBaseUrl' => $request->getBaseUrl(),
'stylePath' => $request->getBasePath() . '/' . $this->getStyle()->getFilesPath(),
'company' => $options['company'],
'mibewHost' => $options['mibewHost'],
'title' => $options['page.title'],
),
'startFrom' => $options['startFrom'],
);
// Add module specific options
$module_options_list = array(
'chatOptions',
'surveyOptions',
'leaveMessageOptions',
'invitationOptions',
);
foreach ($module_options_list as $key) {
if (isset($options[$key])) {
$app_settings[$key] = $options[$key];
}
}
return sprintf(
'jQuery(document).ready(function() {Mibew.Application.start(%s);});',
json_encode($app_settings)
);
}
}

View File

@ -19,6 +19,7 @@
namespace Mibew\Controller\Chat;
use Mibew\Asset\AssetManagerInterface;
use Mibew\Http\Exception\NotFoundException;
use Mibew\Thread;
use Symfony\Component\HttpFoundation\Request;
@ -68,13 +69,15 @@ class OperatorChatController extends AbstractController
);
// Build js application options
$page['chatOptions'] = json_encode($page['chat']);
$page['mibewBasePath'] = $request->getBasePath();
$page['mibewBaseUrl'] = $request->getBaseUrl();
$page['chatOptions'] = $page['chat'];
// Initialize client side application
$this->getAssetManager()->attachJs('js/compiled/chat_app.js');
$this->getAssetManager()->attachJs(
$this->startJsApplication($request, $page),
AssetManagerInterface::INLINE,
1000
);
// Render the page with chat.
return $this->render('chat', $page);

View File

@ -19,6 +19,7 @@
namespace Mibew\Controller\Chat;
use Mibew\Asset\AssetManagerInterface;
use Mibew\Http\Exception\NotFoundException;
use Mibew\Settings;
use Mibew\Thread;
@ -54,13 +55,15 @@ class UserChatController extends AbstractController
);
// Build js application options
$page['chatOptions'] = json_encode($page['chat']);
$page['mibewBasePath'] = $request->getBasePath();
$page['mibewBaseUrl'] = $request->getBaseUrl();
$page['chatOptions'] = $page['chat'];
// Initialize client side application
$this->getAssetManager()->attachJs('js/compiled/chat_app.js');
$this->getAssetManager()->attachJs(
$this->startJsApplication($request, $page),
AssetManagerInterface::INLINE,
1000
);
// Expand page
return $this->render('chat', $page);
@ -150,13 +153,15 @@ class UserChatController extends AbstractController
$group_id,
$info,
$referrer
),
array(
'mibewBasePath' => $request->getBasePath(),
'mibewBaseUrl' => $request->getBaseUrl(),
)
);
$page['leaveMessageOptions'] = json_encode($page['leaveMessage']);
$page['leaveMessageOptions'] = $page['leaveMessage'];
$this->getAssetManager()->attachJs(
$this->startJsApplication($request, $page),
AssetManagerInterface::INLINE,
1000
);
return $this->render('chat', $page);
}
@ -186,13 +191,15 @@ class UserChatController extends AbstractController
$group_id,
$info,
$referrer
),
array(
'mibewBasePath' => $request->getBasePath(),
'mibewBaseUrl' => $request->getBaseUrl(),
)
);
$page['surveyOptions'] = json_encode($page['survey']);
$page['surveyOptions'] = $page['survey'];
$this->getAssetManager()->attachJs(
$this->startJsApplication($request, $page),
AssetManagerInterface::INLINE,
1000
);
return $this->render('chat', $page);
}
@ -254,13 +261,15 @@ class UserChatController extends AbstractController
$page = setup_invitation_view($thread);
// Build js application options
$page['invitationOptions'] = json_encode($page['invitation']);
$page['mibewBasePath'] = $request->getBasePath();
$page['mibewBaseUrl'] = $request->getBaseUrl();
$page['invitationOptions'] = $page['invitation'];
// Initialize client side application
$this->getAssetManager()->attachJs('js/compiled/chat_app.js');
$this->getAssetManager()->attachJs(
$this->startJsApplication($request, $page),
AssetManagerInterface::INLINE,
1000
);
// Expand page
return $this->render('chat', $page);

View File

@ -6,43 +6,6 @@
<!-- Add style scripts -->
<script type="text/javascript" src="{{asset "@CurrentStyle/js/compiled/scripts.js"}}"></script>
<!-- Run application -->
<script type="text/javascript"><!--
jQuery(document).ready(function(){
Mibew.Application.start({
server: {
url: '{{route "chat_thread_update"}}',
requestsFrequency: {{frequency}}
},
page: {
style: '{{styleName}}',
mibewBasePath: '{{mibewBasePath}}',
mibewBaseUrl: '{{mibewBaseUrl}}',
stylePath: '{{mibewBasePath}}/{{stylePath}}',
company: {
name: '{{#jsString}}{{company.name}}{{/jsString}}',
chatLogoURL: '{{company.chatLogoURL}}'
},
mibewHost: '{{mibewHost}}',
title: '{{#jsString}}{{[page.title]}}{{/jsString}}'
},
{{#if chatOptions}}
chatOptions: {{{chatOptions}}},
{{/if}}
{{#if surveyOptions}}
surveyOptions: {{{surveyOptions}}},
{{/if}}
{{#if leaveMessageOptions}}
leaveMessageOptions: {{{leaveMessageOptions}}},
{{/if}}
{{#if invitationOptions}}
invitationOptions: {{{invitationOptions}}},
{{/if}}
startFrom: '{{startFrom}}'
});
});
//--></script>
{{/override}}
{{#override "page"}}