diff --git a/protected/humhub/docs/guide/dev-javascript.md b/protected/humhub/docs/guide/dev-javascript.md index 0333dcbd3b..b9a7198a59 100644 --- a/protected/humhub/docs/guide/dev-javascript.md +++ b/protected/humhub/docs/guide/dev-javascript.md @@ -2,17 +2,17 @@ Javascript API ======= Since version 1.2 HumHub provides a module based Javascript API within the `humhub` namespace. -Instead of using inline script blocks in your views it's highly recommended using the new module system for your -frontend scripts. The core components of this api are described in the following. +Instead of embeding inline script blocks into your view files, it's highly recommended to use the new module system for your js scripts. + +The usage of this API and it's core components are described in the following. ## Modules ### Module Asset -Your Module script files should reside within the `asset/js` folder of your backend module and be appended at the bottom of your document by using yii's asset bundles. +Module script files should reside within the `resources/js` folder of your humhub module and should ideally be appended at the bottom of your document. This can be achieved by using [Asset Bundles](http://www.yiiframework.com/doc-2.0/guide-structure-assets.html): -Example: ```php namespace humhub\modules\example\assets; @@ -21,29 +21,36 @@ use yii\web\AssetBundle; class ExampleAsset extends AssetBundle { + // You can also use View::POS_BEGIN to append your scripts to the beginning of the body element. public $jsOptions = ['position' => \yii\web\View::POS_END]; - public $sourcePath = '@example/assets'; - public $css = []; + public $sourcePath = '@example/resources'; public $js = [ 'js/humhub.example.js' ]; } ``` +> Note: Make sure to add your assets after the core scripts, which are added within the html head. + +> Note: Your Asset Bundle should reside in the `assets` subdirectory of your module. + ### Module Registration -Modules are registered by calling the `humhub.initModule`. This function -requires an unique module id and your module function. The module function provides the following arumgents +New Module are registered by calling the `humhub.module` function as in the following example. + +The first parameter of the `module` function is an unique module id. The second argument is the module function, which allows you to add module functions either by adding functions directly to the `module` argument or using the `module.export` function (prefered). + +The `module` function provides the following arguments: 1. `module`: Your module instance, for exporting module functions and attributes. 2. `require`: Method for injecting other modules. -3. `$`: jQuery. +3. `$`: jQuery instance. -The following example shows the registraion of module with id 'example': +> Note: Only exported functions and attributes will be visible outside of the module. ```javascript // After registration, all exported functions will be available under the namespace humhub.modules.example -humhub.initModule('example', function(module, require, $) { +humhub.module('example', function(module, require, $) { // We require the client module var client = require('client'); @@ -58,13 +65,15 @@ humhub.initModule('example', function(module, require, $) { // Some logic } + var test = function() { + // Test function + } + // Export multiple values by calling module.export. module.export({ + test: test, myFunction: function() { ... - }, - init: function() { - //This code will automatically be executed when dom is ready. } }); }); @@ -77,6 +86,9 @@ humhub.initModule('example.mySubmodule', function(module, require, $) { } ``` + + + Accessing your example module: ```javascript diff --git a/protected/humhub/modules/activity/tests/codeception/_support/_generated/UnitTesterActions.php b/protected/humhub/modules/activity/tests/codeception/_support/_generated/UnitTesterActions.php index d118550b7e..740c2cd488 100644 --- a/protected/humhub/modules/activity/tests/codeception/_support/_generated/UnitTesterActions.php +++ b/protected/humhub/modules/activity/tests/codeception/_support/_generated/UnitTesterActions.php @@ -1,4 +1,4 @@ - '1', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e21', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '1', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 1, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1'), - array('id' => '2', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e22', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '2', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 1, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1'), - array('id' => '3', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e23', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '3', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 2, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '2', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '2'), - array('id' => '4', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e24', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '4', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 2, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '2', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '2'), - array('id' => '5', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e25', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '5', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 3, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '3', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '3'), - array('id' => '6', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e26', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '6', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 3, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '3', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '3'), + array('id' => '1', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e21', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '1', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 1, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1', 'stream_channel' => 'default'), + + array('id' => '2', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e22', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '2', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 1, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1', 'stream_channel' => 'default'), + array('id' => '3', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e23', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '3', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 2, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '2', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '2', 'stream_channel' => 'default'), + array('id' => '4', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e24', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '4', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 2, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '2', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '2', 'stream_channel' => 'default'), + array('id' => '5', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e25', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '5', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 3, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '3', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '3', 'stream_channel' => 'default'), + array('id' => '6', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e26', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '6', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 3, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '3', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '3', 'stream_channel' => 'default'), // Posts to Space 1 (Id: 1) of User 1 & 3 - array('id' => '7', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e27', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '7', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 4, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1'), - array('id' => '8', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e28', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '8', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 4, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1'), - array('id' => '9', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e29', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '9', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 4, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '3', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '3'), + array('id' => '7', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e27', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '7', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 4, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1', 'stream_channel' => 'default'), + array('id' => '8', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e28', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '8', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 4, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1', 'stream_channel' => 'default'), + array('id' => '9', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e29', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '9', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 4, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '3', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '3', 'stream_channel' => 'default'), // Posts to Space 2 of User 2 (Public & Private) - array('id' => '10', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e30', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '10', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 5, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1'), - array('id' => '11', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e31', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '11', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 5, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1'), + array('id' => '10', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e30', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '10', 'visibility' => '1', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 5, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1', 'stream_channel' => 'default'), + array('id' => '11', 'guid' => 'e0acdc54-13c8-4778-adc1-7302e3243e31', 'object_model' => 'humhub\modules\post\models\Post', 'object_id' => '11', 'visibility' => '0', 'pinned' => '0', 'archived' => '0', 'contentcontainer_id' => 5, 'created_at' => '2014-08-08 05:36:05', 'created_by' => '1', 'updated_at' => '2014-08-08 05:36:05', 'updated_by' => '1', 'stream_channel' => 'default'), ); \ No newline at end of file diff --git a/protected/humhub/modules/friendship/tests/codeception/_support/_generated/UnitTesterActions.php b/protected/humhub/modules/friendship/tests/codeception/_support/_generated/UnitTesterActions.php index 0a46e6c45e..e792058406 100644 --- a/protected/humhub/modules/friendship/tests/codeception/_support/_generated/UnitTesterActions.php +++ b/protected/humhub/modules/friendship/tests/codeception/_support/_generated/UnitTesterActions.php @@ -1,4 +1,4 @@ - 1]); - $unserialized = unserialize(serialize($model)); - - $this->assertEquals($unserialized->id, 1); - $this->assertEquals($unserialized->message, 'User 1 Profile Post Private'); - $this->assertEquals($model->test, 'aaa'); - $this->assertEquals($unserialized->test, 'aaa'); - } - -}