Sweekit - aka Sweelix toolkit
The toolkit is under new BSD License except for 3rdParty elements which are under their own licenses.
The project can be found on github : https://github.com/pgaultier/sweekit
Simple demo can be checked here : https://www.sweelix.net/sweekit/demo/
3rd party tools used in the toolkit are
This toolkit was written to ease javascript access during our developments. In order to not create conflict, Sweekit classes are prefixed with Sw
A short explain is available in the doc directory.
Available features :
Sample app is available to see some features in action. Checkout the samples directory : https://github.com/pgaultier/sweekit/tree/devel/samples
Be carefull, this component is only available in devel branch
The mobile notifier allow the developper to send notification to iOS and/or Android system.
Of course, you'll need account information from Google or Apple to be able to send notifications.
Adding the notifier component to the application
/* configuration array */ mobileNotification' => array( 'class' => 'ext.sweekit.actions.SwMobileNotifier', 'mode' => 'production', // can be development to use the sandbox 'apnsCertificateFile' => 'my_apple_certificate_for_push.pem', 'apnsCertificatePassphrase' => 'my_apple_certificate_passphrase', // comment out if there is no passphrase 'apnsEmbeddedCaFile'=>true, // embed entrust ca file if needed (ssl errors, ...) 'c2dmUsername' => 'my_gmail_push_account@gmail.com', 'c2dmPassword' => 'my_gmail_push_account_password', 'c2dmApplicationIdentifier' => 'my_gmail_push_app_identifier', ),
You are now able to push messages to mobile devices
// send message to device $res = Yii::app()->getComponent('mobileNotification')->sendC2dmMessage( 'the_android_device_id', // look like base64 encoded data array('badge' => 12, 'message' => 'My message to send') // the payload to send to the device );
// send message to device $res = Yii::app()->getComponent('mobileNotification')->sendApnsMessage( 'the_ios_device_id', // 64 characters hex id array('aps' => array( 'badge' => CPropertyValue::ensureInteger(12), // make sure data is an integer 'alert' => 'My message to send', )) );
Apple added a function to check if device should still receive notification.
This method should be called using a cron system in order to purge old pushId from your database to avoid sending useless notifications
$res = Yii::app()->getComponent('mobileNotification')->readApnsFeedback(); /** * $res is an array of array : * $res = array( * array($timeStamp, $pushId1), * array($timeStamp, $pushId2), * // ... * ); */
As an example, you have created an online shop and in several places, the cart is updated. If in the header there is a widget which shows the number of products, this widget should be updated when the cart is updated.
Using the callback system you can :
Register an event in the widget using Sweeml
Sweeml::registerEvent('updateCart', "function(data){jQuery('#cart.nb').html(data+' products');}"); <div id="cart">0 product</div>
Whereever the cart is updated you can raise an event.
<script type="text/javascript"> function refreshCart() { // perform ajax call to refresh info jQuery.ajax({ url : 'http://targeturl', success : function(nbOfProducts) { // nbOfProducts is an integer with the number of products in cart <?php echo Sweeml::raiseEvent('updateCart', nbOfProducts); } }); } </script> <a href="#" onclick="refreshCart()">refresh cart</a>
The basic model
class MyFile extends CFormModel { public $thefile; public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('thefile', 'SwFileValidator', 'maxFiles' => 2, 'allowEmpty' => true), ); } }
The controller
class UploadController extends CController { /** * Attach generic actions to handle async uploads * * @return array */ public function actions() { return array( 'asyncUpload' => 'ext.sweekit.actions.SwUploadAction', 'asyncDelete' => 'ext.sweekit.actions.SwDeleteAction', ); } /** * initial action * * @return void */ public function actionIndex() { $file = new MyFile(); if(isset($_POST['MyFile']) == true) { // get uploaded files $realFiles = SwUploadedFile::getInstances($file, 'thefile'); foreach($realFiles as $realFile) { // save uploaded files $realFile->saveAs('files/'.$realFile->getName()); } } $this->render('index', array('file' => $file)); } }
The view
echo Sweeml::activeAsyncFileUpload($file, 'thefile',array( 'config' => array( 'runtimes' => 'html5, flash', 'auto' => true, 'ui' => true, 'maxFileSize' => '512mb', 'multiSelection' => true, ), 'events'=>array( 'beforeUpload' => 'js:function(up, file){ $(\'#submitButton\').attr(\'disabled\', \'disabled\'); }', 'uploadComplete' => 'js:function(up, files){ $(\'#submitButton\').removeAttr(\'disabled\'); }', ) )); <?php echo Sweeml::htmlButton('submit', array('type' => 'submit', 'id' => 'submitButton'));
Sweeml override the original CHtml helpers and add several functionnalities
The callback system allow the developper to register (js) events in the page -- some kind of listeners. In other places, the developer can raise an event. This can be seen as some loose coupling. An other advantage of the callback system is that even if the callback was raised in an iframe, the callback get caught in the main window. In other word, the raised event is re-routed to the main window. This is usefull when iframe (for lightboxes, ...) are used.
This method produce a javascript string
(string) raiseEvent($eventName, $parameters=array(), $context=null);
This method produce a javascript string which can be used directly as an url
(string) raiseEventUrl($eventName, $parameters=array(), $context=null);
Parameters
This method register directly the event using the clientScript
(string) registerEvent($eventName, $action, $context=null);
This method produce a javascript string
(string) registerEventScript($eventName, $action, $context=null);
Parameters
Produce an ajax call to $url and perform the update/replace of the target element if content-type is text/html. If the content-type is application/javascript the returned js code is executed.
This system raise also two jQuery events on the target :
This is usefull to prepare data or rebind events when the dom has been replaced.
This method produce a javascript string
(string) raiseAjaxRefresh($target, $url, $data=null, $mode=null);
This method produce a javascript string which can be used directly as an url
(string) raiseAjaxRefreshUrl($target, $url, $data=null, $mode=null);
Parameters
This script intercept form posting, perform the post in ajax and update/replace or execute the javavscript depending on the content-type
This method produce a javascript script which should be executed to enable form ajaxify
(string) ajaxSubmitHandlerScript($target);
This method produce a javascript which is automatically registered and ajaxify the form(s)
ajaxSubmitHandler($target);
Parameters
An easier way to ajaxify the forms using this system is to use the all in one tag generator.
replace the classic
(string) beginForm($action='',$method='post',$htmlOptions=array());
with
(string) beginAjaxForm($action='',$method='post',$htmlOptions=array());
And the form will be automagically ajaxified
Parameters
Usefull when we need to redirect the main window from an iframe. We don't need to know in advance if we are in an iframe or not.
This method produce a javascript string
(string) raiseRedirect($url, $timer=null);
This method produce a javascript string which can be used directly as an url
(string) raiseRedirectUrl($url, $timer=null);
Parameters
This method register all js and css needed to open a shadowbox and open it
This method produce a javascript string
(string) raiseOpenShadowbox($url='#', $shadowBoxOptions=array());
This method produce a javascript string which can be used directly as an url
(string) raiseOpenShadowboxUrl($url='#', $shadowBoxOptions=array());
Parameters
This method close current shadowbox
This method produce a javascript string
(string) raiseCloseShadowbox();
This method produce a javascript string which can be used directly as an url
(string) raiseCloseShadowboxUrl();
The methods
(string) activeAsyncFileUpload($model, $attribute, $htmlOptions=array()) ;(string) asyncFileUpload($name, $htmlOptions=array(), $value=null);This methods should work like the original ones. The difference is that they register js, css and everything to allow multi fileuploads with drag'n'drop and fancy features. See method doc clock for all options
echo Sweeml::activeAsyncFileUpload($myModel, 'uploadField', $options);
SwClientScriptBehavior add sweelix js package management. This behavior
is used in Sweeml to register Sweelix Javascript (and Styles) packages like
the original registerCoreScript() method
Available packages are
Attach the behavior to the clientScript component
'clientScript' => array( 'behaviors' => array( 'sweelixClientScript' => array( 'class' => 'ext.sweekit.behaviors.SwClientScriptBehavior', ), ), ),
Use it in your controller or view
// ... controller code class MyController extends CController { // ... public function actionTest() { // Register sweelix debug script Yii::app()->clientScript->registerSweelixScript('debug'); // ... } // ... }
SwAjaxBehavior add the following methods to CHttpRequest :
(bool) $res = Yii::app()->getRequest()->getIsJsRequest($isAjax=true)
Check if we should return javascript content
(bool) $res = Yii::app()->getRequest()->getIsJsonRequest($isAjax=true)
Check if we should return json content
Example :
Attach the behavior to the request component
'request' => array( 'behaviors' => array( 'sweelixAjax' => array( 'class' => 'ext.sweekit.behaviors.SwAjaxBehavior', ), ), ), ///...
Use it in your controller
// ... controller code if(Yii::app()->getRequest()->getIsJsRequest() === true) { // render javascript which will be executed by the borwser // see below SwRenderBehavior $this->renderJs('alert("Request was performed as expected");'); } elseif(Yii::app()->getRequest()->getIsAjaxRequest() === true) { // render partial html $this->renderPartial('_htmlPartialView'); } else { // render full html $this->render('htmlFullView'); }
SwRenderBehavior should be used with SwAjaxBehavior. SwRenderBehavior add the following methods to CController :
$this->redirectJs($url, $timer=null, $terminate=true)
Redirect to $url using javascript. Usefull when the ajax call should initiate a redirect.
$this->renderJs($script, $terminate=true)
Render javascript using correct headers
$this->renderJson($data, $httpCode=200, $terminate=true)
Render data as Json string with correct headers
Example :
Attach the behavior to the controller
class MyController extends CController { // ... public function behaviors() { return array( 'sweelixRendering' => array( 'class' => 'ext.sweekit.behaviors.SwRenderBehavior', ), ); } // ... }
Use it in your code
class MyController extends CController { // ... public function actionTest() { if(Yii::app()->request->isAjaxRequest == true) { // this will raise an event using sweelix callback in order to open a shadowbox $this->renderJs('alert("direct js rendering")'); } } // ... }
SwProtocolFilter allow automatic switching from HTTP to HTTPS
To force everything in https except the action parse
class MyController extends CController { // ... public function filters() { return array( array( 'ext.sweekit.filters.SwProtocolFilter - parse', 'mode' => 'https', ), ); } }
To force everything in http except the action parse
class MyController extends CController { // ... public function filters() { return array( array( 'ext.sweekit.filters.SwProtocolFilter + parse', 'mode' => 'https', ), ); } }
Package dependancies are automatically resolved.
Core package, this package define the base namespace jQuery.sweelix and a basic module system.
No usefull function is exported
Debugging package for js. This is a simple console wrapper. Once module is inited, following methods are added to the jQuery.sweelix object :
log, debug, info, warn, error, assert, dir, trace, count, group, groupEnd
// .. js code
jQuery.sweelix.log('Loging info in the console');
// .. js code
If the debug is activated, the console will display the string "Loging info in the console". If debug is deactivated, the console will remain empty
In order to be able to use the debug module you have to add SwClientScriptBehavior and configure it
// App config code 'clientScript' => array( 'behaviors' => array( 'sweelixClientScript' => array( 'class' => 'ext.sweekit.behaviors.SwClientScriptBehavior', 'config' => array( 'debug' => array( 'debug' => true, // default false - (de)activate debug 'method' => 'log', // default 'log' - default console method used when logging 'fallback' => false, // default false - if console is not available use alerts ), ) ), ), ),
// View code // register debug module <?php Yii::app()->clientScript->registerSweelixScript('debug'); // perform some debugging <script type="text/javascript"> jQuery(document).ready(function(){ jQuery.sweelix.log('Document is ready, starting js'); // ... js code }); </script>
Callback system. This system allow the developer to register and raise events
In order to be able to use the debug module you have to add SwClientScriptBehavior and configure it
// App config code 'clientScript' => array( 'behaviors' => array( 'sweelixClientScript' => array( 'class' => 'ext.sweekit.behaviors.SwClientScriptBehavior', 'config' => array( 'debug' => array( 'debug' => true, // default false - (de)activate debug 'method' => 'log', // default 'log' - default console method used when logging 'fallback' => false, // default false - if console is not available use alerts ), 'callback' => array( 'globalCblName' => 'swlx', // default 'swlx' - default namespace of registered events 'top' => true, // default true - if true, event are always re-routed to top window ), ) ), ), ),
// View code // register callback module <?php Yii::app()->clientScript->registerSweelixScript('callback'); // perform some regisration <script type="text/javascript"> jQuery(document).ready(function(){ jQuery.sweelix.register('myRegisteredEvent', function(params){ alert('my event was raised with myParam value : '+params.myParam); }); // ... js code }); </script> // ... <a href="#" onclick="jQuery.sweelix.raise('myRegisteredEvent', {'myParam':2})">Raise the event</a>
Ajax system allow the developper to perform easy ajax requests
// App config code 'clientScript' => array( 'behaviors' => array( 'sweelixClientScript' => array( 'class' => 'ext.sweekit.behaviors.SwClientScriptBehavior', 'config' => array( 'debug' => array( 'debug' => true, // default false - (de)activate debug 'method' => 'log', // default 'log' - default console method used when logging 'fallback' => false, // default false - if console is not available use alerts ), 'callback' => array( 'globalCblName' => 'swlx', // default 'swlx' - default namespace of registered events 'top' => true, // default true - if true, event are always re-routed to top window ), // ajax module has no configuration vars ) ), ), ), //...
// View code
// register ajax module
<?php Yii::app()->clientScript->registerSweelixScript('ajax'); ?>
// perform some regisration
<script type="text/javascript">
var parameters ={
'targetUrl' : 'http://www.myurl.com?r=site/cool',
'data' : {}, // data to post
'mode' : '', // can be replace or empty
'targetSelector' : '#myDiv'
};
</script>
// ...
<a href="#" onclick="jQuery.sweelix.raise('ajaxRefreshHandler', parameters)">Raise the event</a>
// ...
<div id="#myDiv">
This div should be updated
</div>
C1C // ... <div id="#myDiv"> This div should be updated </div>
// Controller code public function actionCool() { // ... controller code if(Yii::app()->getRequest()->getIsAjaxRequest() === true) { // render partial html $this->renderPartial('_htmlPartialView'); } else { // render full html $this->render('htmlFullView'); } }
Shadowbox system allow easy lightbox management
// App config code 'clientScript' => array( 'behaviors' => array( 'sweelixClientScript' => array( 'class' => 'ext.sweekit.behaviors.SwClientScriptBehavior', 'config' => array( 'debug' => array( 'debug' => true, // default false - (de)activate debug 'method' => 'log', // default 'log' - default console method used when logging 'fallback' => false, // default false - if console is not available use alerts ), 'callback' => array( 'globalCblName' => 'swlx', // default 'swlx' - default namespace of registered events 'top' => true, // default true - if true, event are always re-routed to top window ), ) ), ), ), //...
// View code
// register ajax module
<?php Yii::app()->clientScript->registerSweelixScript('shadowbox'); ?>
// perform some regisration
<script type="text/javascript">
var parameters ={
content: 'http://www.example.com/?r=site/lb',
player: "iframe",
title: "Lightbox",
height: 350,
width: 350
};
</script>
// ...
<a href="javascript:void(0);" onclick="jQuery.sweelix.raise('shadowboxOpen', parameters)">Open the shadowbox</a>
// ...
<a href="javascript:void(0);" onclick="jQuery.sweelix.raise('shadowboxClose')">Close shadowbox</a>
C3C <?php echo CHtml::link('Close shadowbox','#', array( 'onclick'=> Sweeml::raiseCloseShadowbox(), ));
// ... // or using js in url C4C <?php echo CHtml::link('Close shadowbox',Sweeml::raiseCloseShadowboxUrl());
Be the first person to leave a comment
Please login to leave your comment.