Plugin system enhancements (Custom user's settings tabs)

This commit is contained in:
RainLoop Team 2015-02-19 05:01:59 +04:00
parent a3f28b09f6
commit 9a58fd0a66
13 changed files with 398 additions and 16 deletions

View file

@ -16,8 +16,10 @@
function Plugins()
{
this.oSettings = require('Storage/Settings');
this.oViewModelsHooks = {};
this.oSimpleHooks = {};
this.aUserViewModelsHooks = [];
this.aAdminViewModelsHooks = [];
}
/**
@ -26,9 +28,14 @@
Plugins.prototype.oSettings = {};
/**
* @type {Object}
* @type {Array}
*/
Plugins.prototype.oViewModelsHooks = {};
Plugins.prototype.aUserViewModelsHooks = [];
/**
* @type {Array}
*/
Plugins.prototype.aAdminViewModelsHooks = [];
/**
* @type {Object}
@ -82,17 +89,44 @@
* @param {string} sAction
* @param {Object=} oParameters
* @param {?number=} iTimeout
* @param {string=} sGetAdd = ''
* @param {Array=} aAbortActions = []
*/
Plugins.prototype.remoteRequest = function (fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions)
Plugins.prototype.remoteRequest = function (fCallback, sAction, oParameters, iTimeout)
{
if (Globals.__APP__)
{
Globals.__APP__.remote().defaultRequest(fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions);
Globals.__APP__.remote().defaultRequest(fCallback, 'Plugin' + sAction, oParameters, iTimeout);
}
};
/**
* @param {Function} SettingsViewModelClass
* @param {string} sLabelName
* @param {string} sTemplate
* @param {string} sRoute
*/
Plugins.prototype.addSettingsViewModel = function (SettingsViewModelClass, sTemplate, sLabelName, sRoute)
{
this.aViewModelsHooks.push([SettingsViewModelClass, sTemplate, sLabelName, sRoute]);
};
/**
* @param {Function} SettingsViewModelClass
* @param {string} sLabelName
* @param {string} sTemplate
* @param {string} sRoute
*/
Plugins.prototype.addSettingsViewModelForAdmin = function (SettingsViewModelClass, sTemplate, sLabelName, sRoute)
{
this.aAdminViewModelsHooks.push([SettingsViewModelClass, sTemplate, sLabelName, sRoute]);
};
Plugins.prototype.runSettingsViewModelHooks = function (bAdmin)
{
_.each(bAdmin ? this.aAdminViewModelsHooks : this.aViewModelsHooks, function (aView) {
require('Knoin/Knoin').addSettingsViewModel(aView[0], aView[1], aView[2], aView[3]);
});
};
/**
* @param {string} sPluginSection
* @param {string} sName

View file

@ -8,6 +8,8 @@
kn = require('Knoin/Knoin'),
Plugins = require('Common/Plugins'),
AbstractSettings = require('Screen/AbstractSettings')
;
@ -63,6 +65,8 @@
kn.addSettingsViewModel(require('Settings/Admin/About'),
'AdminSettingsAbout', 'About', 'about');
Plugins.runSettingsViewModelHooks(true);
if (fCallback)
{
fCallback();

View file

@ -10,6 +10,8 @@
Globals = require('Common/Globals'),
Translator = require('Common/Translator'),
Plugins = require('Common/Plugins'),
AppStore = require('Stores/User/App'),
Settings = require('Storage/Settings'),
@ -101,6 +103,8 @@
'SettingsOpenPGP', 'SETTINGS_LABELS/LABEL_OPEN_PGP_NAME', 'openpgp');
}
Plugins.runSettingsViewModelHooks(false);
if (fCallback)
{
fCallback();

8
dev/bootstrap.js vendored
View file

@ -39,12 +39,16 @@
// export
window['rl'] = window['rl'] || {};
window['rl']['i18n'] = _.bind(Translator.i18n, Translator);
window['rl']['addHook'] = _.bind(Plugins.addHook, Plugins);
window['rl']['settingsGet'] = _.bind(Plugins.mainSettingsGet, Plugins);
window['rl']['remoteRequest'] = _.bind(Plugins.remoteRequest, Plugins);
window['rl']['pluginSettingsGet'] = _.bind(Plugins.settingsGet, Plugins);
window['rl']['createCommand'] = Utils.createCommand;
window['rl']['addSettingsViewModel'] = _.bind(Plugins.addSettingsViewModel, Plugins);
window['rl']['pluginRemoteRequest'] = _.bind(Plugins.remoteRequest, Plugins);
window['rl']['pluginSettingsGet'] = _.bind(Plugins.settingsGet, Plugins);
window['rl']['EmailModel'] = EmailModel;
window['rl']['Enums'] = Enums;

View file

@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2015 RainLoop Team
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1 @@
1.0

View file

@ -0,0 +1,57 @@
<?php
class CustomSettingsTabPlugin extends \RainLoop\Plugins\AbstractPlugin
{
/**
* @return void
*/
public function Init()
{
$this->UseLangs(true); // start use langs folder
$this->addJs('js/CustomUserSettings.js'); // add js file
$this->addAjaxHook('AjaxGetCustomUserData', 'AjaxGetCustomUserData');
$this->addAjaxHook('AjaxSaveCustomUserData', 'AjaxSaveCustomUserData');
$this->addTemplate('templates/PluginCustomSettingsTag.html');
}
/**
* @return array
*/
public function AjaxGetCustomUserData()
{
$aSettings = $this->getUserSettings();
$sUserFacebook = isset($aSettings['UserFacebook']) ? $aSettings['UserFacebook'] : '';
$sUserSkype = isset($aSettings['UserSkype']) ? $aSettings['UserSkype'] : '';
// or get user's data from your custom storage ( DB / LDAP / ... ).
\sleep(1);
return $this->ajaxResponse(__FUNCTION__, array(
'UserFacebook' => $sUserFacebook,
'UserSkype' => $sUserSkype
));
}
/**
* @return array
*/
public function AjaxSaveCustomUserData()
{
$sUserFacebook = $this->ajaxParam('UserFacebook');
$sUserSkype = $this->ajaxParam('UserSkype');
// or put user's data to your custom storage ( DB / LDAP / ... ).
\sleep(1);
return $this->ajaxResponse(__FUNCTION__, $this->saveUserSettings(array(
'UserFacebook' => $sUserFacebook,
'UserSkype' => $sUserSkype
)));
}
}

View file

@ -0,0 +1,73 @@
(function () {
/**
* @constructor
*/
function CustomUserSettings()
{
this.userSkype = ko.observable('');
this.userFacebook = ko.observable('');
this.loading = ko.observable(false);
this.saving = ko.observable(false);
this.savingOrLoading = ko.computed(function () {
return this.loading() || this.saving();
}, this);
}
CustomUserSettings.prototype.customAjaxSaveData = function ()
{
var self = this;
if (this.saving())
{
return false;
}
this.saving(true);
window.rl.pluginRemoteRequest(function (sResult, oData) {
self.saving(false);
if (window.rl.Enums.StorageResultType.Success === sResult && oData && oData.Result)
{
// true
}
else
{
// false
}
}, 'AjaxSaveCustomUserData', {
'UserSkype': this.userSkype(),
'UserFacebook': this.userFacebook()
});
};
CustomUserSettings.prototype.onBuild = function () // special function
{
var self = this;
this.loading(true);
window.rl.pluginRemoteRequest(function (sResult, oData) {
self.loading(false);
if (window.rl.Enums.StorageResultType.Success === sResult && oData && oData.Result)
{
self.userSkype(oData.Result.UserSkype || '');
self.userFacebook(oData.Result.UserFacebook || '');
}
}, 'AjaxGetCustomUserData');
};
window.rl.addSettingsViewModel(CustomUserSettings, 'PluginCustomSettingsTag',
'SETTINGS_CUSTOM_PLUGIN/TAB_NAME', 'custom');
}());

View file

@ -0,0 +1,6 @@
[SETTINGS_CUSTOM_PLUGIN]
TAB_NAME = "Custom Plugin"
LEGEND_CUSTOM = "Custom Plugin (Example)"
LABEL_SKYPE = "Skype"
LABEL_FACEBOOK = "Facebook"
BUTTON_SAVE = "Save"

View file

@ -0,0 +1,6 @@
[SETTINGS_CUSTOM_PLUGIN]
TAB_NAME = "Плагин"
LEGEND_CUSTOM = "Плагин (Пример)"
LABEL_SKYPE = "Skype"
LABEL_FACEBOOK = "Facebook"
BUTTON_SAVE = "Сохранить"

View file

@ -0,0 +1,34 @@
<div>
<div class="form-horizontal">
<div class="legend">
<span class="i18n" data-i18n-text="SETTINGS_CUSTOM_PLUGIN/LEGEND_CUSTOM"></span>
&nbsp;&nbsp;&nbsp;
<i class="icon-spinner animated" style="margin-top: 5px" data-bind="visible: loading"></i>
</div>
<div class="control-group">
<label class="control-label">
<span class="i18n" data-i18n-text="SETTINGS_CUSTOM_PLUGIN/LABEL_SKYPE"></span>
</label>
<div class="controls">
<input type="text" data-bind="value: userSkype, enable: !savingOrLoading()" />
</div>
</div>
<div class="control-group">
<label class="control-label">
<span class="i18n" data-i18n-text="SETTINGS_CUSTOM_PLUGIN/LABEL_FACEBOOK"></span>
</label>
<div class="controls">
<input type="text" data-bind="value: userFacebook, enable: !savingOrLoading()" />
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn" data-bind="click: customAjaxSaveData, enable: !savingOrLoading()">
<i data-bind="css: {'icon-floppy': !saving(), 'icon-spinner animated': saving()}" class="icon-floppy"></i>
&nbsp;&nbsp;
<span class="i18n" data-i18n-text="SETTINGS_CUSTOM_PLUGIN/BUTTON_SAVE"></span>
</button>
</div>
</div>
</div>
</div>

View file

@ -176,7 +176,7 @@ abstract class AbstractPlugin
return $this;
}
/**
* @param string $sName
*
@ -354,4 +354,67 @@ abstract class AbstractPlugin
return $this;
}
/**
* @param string $sName
* @param string $sPlace
* @param string $sHtml
* @param bool $bPrepend = false
*
* @return self
*/
protected function ajaxResponse($sFunctionName, $aData)
{
if ($this->oPluginManager)
{
return $this->oPluginManager->AjaxResponseHelper(
$this->oPluginManager->convertPluginFolderNameToClassName($this->Name()).'::'.$sFunctionName, $aData);
}
return \json_encode($aData);
}
/**
* @param string $sKey
* @param mixed $mDefault = null
*
* @return mixed
*/
public function ajaxParam($sKey, $mDefault = null)
{
if ($this->oPluginManager)
{
return $this->oPluginManager->Actions()->GetActionParam($sKey, $mDefault);
}
return '';
}
/**
* @return array
*/
public function getUserSettings()
{
if ($this->oPluginManager)
{
return $this->oPluginManager->GetUserPluginSettings($this->Name());
}
return array();
}
/**
* @param array $aSettings
*
* @return bool
*/
public function saveUserSettings($aSettings)
{
if ($this->oPluginManager && \is_array($aSettings))
{
return $this->oPluginManager->SaveUserPluginSettings($this->Name(), $aSettings);
}
return false;
}
}

View file

@ -192,7 +192,7 @@ class Manager
*
* @return string
*/
private function convertPluginFolderNameToClassName($sFolderName)
public function convertPluginFolderNameToClassName($sFolderName)
{
$aParts = \array_map('ucfirst', \array_map('strtolower',
\explode(' ', \preg_replace('/[^a-z0-9]+/', ' ', $sFolderName))));
@ -515,19 +515,19 @@ class Manager
/**
* @param string $sActionName
* @param mixed $mCallbak
* @param mixed $mCallback
*
* @return \RainLoop\Plugins\Manager
*/
public function AddAdditionalAjaxAction($sActionName, $mCallbak)
public function AddAdditionalAjaxAction($sActionName, $mCallback)
{
if ($this->bIsEnabled && \is_callable($mCallbak) && 0 < \strlen($sActionName))
if ($this->bIsEnabled && \is_callable($mCallback) && 0 < \strlen($sActionName))
{
$sActionName = 'Do'.$sActionName;
$sActionName = 'DoPlugin'.$sActionName;
if (!isset($this->aAdditionalAjax[$sActionName]))
{
$this->aAdditionalAjax[$sActionName] = $mCallbak;
$this->aAdditionalAjax[$sActionName] = $mCallback;
}
}
@ -562,6 +562,82 @@ class Manager
return false;
}
/**
* @param string $sFunctionName
* @param mixed $mData
*
* @return mixed
*/
public function AjaxResponseHelper($sFunctionName, $mData)
{
return $this->oActions->DefaultResponse($sFunctionName, $mData);
}
/**
* @param string $sPluginName
*
* @return array
*/
public function GetUserPluginSettings($sPluginName)
{
$oAccount = $this->oActions->GetAccount();
if ($oAccount)
{
$oSettings = $this->oActions->SettingsProvider()->Load($oAccount);
if ($oSettings)
{
$aData = $oSettings->GetConf('Plugins', array());
if (isset($aData[$sPluginName]) && \is_array($aData[$sPluginName]))
{
return $aData[$sPluginName];
}
}
}
return array();
}
/**
* @param string $sPluginName
* @param array $aSettings
*
* @return bool
*/
public function SaveUserPluginSettings($sPluginName, $aSettings)
{
$oAccount = $this->oActions->GetAccount();
if ($oAccount && \is_array($aSettings))
{
$oSettings = $this->oActions->SettingsProvider()->Load($oAccount);
if ($oSettings)
{
$aData = $oSettings->GetConf('Plugins', array());
if (!\is_array($aData))
{
$aData = array();
}
$aPluginSettings = array();
if (isset($aData[$sPluginName]) && \is_array($aData[$sPluginName]))
{
$aPluginSettings = $aData[$sPluginName];
}
foreach ($aSettings as $sKey => $mValue)
{
$aPluginSettings[$sKey] = $mValue;
}
$aData[$sPluginName] = $aPluginSettings;
$oSettings->SetConf('Plugins',$aData);
return $this->oActions->SettingsProvider()->Save($oAccount, $oSettings);
}
}
return false;
}
/**
* @param string $sLang
* @param array $sActionName