Skip to content

Commit e1a7c25

Browse files
committed
general cleanup; moved assets
1 parent 137ebc8 commit e1a7c25

File tree

12 files changed

+154
-67
lines changed

12 files changed

+154
-67
lines changed

src/actions/CloseEntryAction.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
* --- PROPERTIES --
2525
*
2626
* @property EditorEntry|ActiveRecord $modelClass;
27+
* @property string $returnAction
28+
* @property bool $delete
29+
* @property bool $deleteAction
2730
*/
2831
class CloseEntryAction extends Action
2932
{
3033
public $modelClass;
3134
public $returnAction = 'editor';
35+
public $delete = false;
36+
public $deleteAction = 'delete';
3237

3338

3439
/**
@@ -41,10 +46,17 @@ class CloseEntryAction extends Action
4146
public function run($entryId)
4247
{
4348
$modelClass = $this->modelClass;
44-
if ($modelClass::removeEntry($entryId) === false) {
49+
$entryRemoved = $modelClass::removeEntry($entryId);
50+
if (($this->delete === false) && $entryRemoved === false) {
4551
throw new NotFoundHttpException(Yii::t('prototype', 'Entry not found'));
4652
}
4753

48-
return $this->controller->redirect([$this->returnAction]);
54+
55+
if ($this->delete) {
56+
$url = [$this->deleteAction, 'id' => $entryId];
57+
} else {
58+
$url = [$this->returnAction];
59+
}
60+
return $this->controller->redirect($url);
4961
}
5062
}

src/actions/EditorAction.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace dmstr\modules\prototype\actions;
1111

1212

13+
use dmstr\modules\prototype\assets\EditorAsset;
1314
use dmstr\modules\prototype\models\Edit;
1415
use dmstr\modules\prototype\models\Less;
1516
use dmstr\modules\prototype\models\Search;
@@ -18,6 +19,7 @@
1819
use yii\base\Action;
1920
use yii\db\ActiveRecord;
2021
use yii\db\Exception;
22+
use yii\helpers\Url;
2123
use yii\web\Response;
2224

2325
/**
@@ -38,12 +40,20 @@ class EditorAction extends Action
3840
public $openEntryUrl = 'open-entry';
3941
public $mode;
4042

43+
public function init()
44+
{
45+
parent::init();
46+
EditorAsset::register($this->controller->view);
47+
}
48+
4149
/**
4250
* @return string|Response
4351
* @throws Exception
4452
*/
4553
public function run()
4654
{
55+
Yii::$app->session['__crudReturnUrl'] = [$this->id];
56+
4757
$modelClass = $this->modelClass;
4858
$activeEntries = $modelClass::activeEntries();
4959

@@ -74,7 +84,7 @@ public function run()
7484

7585
return $this->controller->render('@vendor/dmstr/yii2-prototype-module/src/actions/views/editor/editor',
7686
[
77-
'activeEntries' => $modelClass::activeEntries(),
87+
'activeEntries' => $activeEntries,
7888
'allEntries' => $allEntries,
7989
'currentEntries' => $currentEntries,
8090
'searchModel' => $searchModel,

src/actions/views/editor/_editor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<?php
3939
if ($activeEntry['id'] !== Edit::NEW_MODEL_ID) {
4040
echo Html::a(FA::icon(FA::_TIMES), ['close-entry', 'entryId' => $activeEntry['id']]);
41+
} else {
42+
echo Html::a(FA::icon(FA::_TIMES), ['editor']);
4143
}
4244
?>
4345
</li>

src/actions/views/editor/_sidebar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<div class="list-group-item">
5353
<?php
5454
echo Html::a($entry['name'], ['open-entry', 'entryId' => $entry['id'], 'term' => $searchModel->term]);
55-
echo Html::a(FA::icon(FA::_TRASH), ['delete', 'id' => $entry['id']], ['class' => 'btn-danger', 'data-confirm' => Yii::t('prototype', 'Are you sure that you want to delete this record?')]);
55+
echo Html::a(FA::icon(FA::_TRASH), ['delete-entry', 'entryId' => $entry['id']], ['class' => 'btn-danger', 'data-confirm' => Yii::t('prototype', 'Are you sure that you want to delete this record?')]);
5656
?>
5757
</div>
5858
<?php

src/actions/views/editor/editor.php

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,6 @@
1919
use dmstr\modules\prototype\models\Less;
2020
use yii\web\View;
2121

22-
$this->registerCss(<<<CSS
23-
.editor-main {
24-
display: flex;
25-
}
26-
.editor-sidebar {
27-
width: 25%;
28-
padding-right: 1rem;
29-
}
30-
.editor-sidebar .list-group {
31-
max-height: 350px;
32-
overflow-y: scroll;
33-
}
34-
35-
.editor-sidebar .list-group-item a:last-of-type{
36-
right: 0;
37-
}
38-
.editor-top-navigation {
39-
list-style-type: none;
40-
padding-left: 0;
41-
margin-bottom: 1rem;
42-
}
43-
.editor-top-navigation > li {
44-
display: inline-block;
45-
}
46-
.editor-canvas {
47-
width: 75%;
48-
}
49-
CSS
50-
);
51-
52-
$this->registerJs(<<<JS
53-
var currentLocation = document.location.toString();
54-
var activeHash = currentLocation.split('#')[1];
55-
var localStorageKey = 'dmstr.yii2.prototype.lastActiveHash';
56-
57-
$(function(){
58-
if (typeof activeHash === "undefined") {
59-
var lastActiveHash = localStorage.getItem(localStorageKey);
60-
if (lastActiveHash) {
61-
activeHash = lastActiveHash;
62-
}
63-
} else {
64-
activeHash = '#' + activeHash;
65-
}
66-
67-
var tabEl = $('.editor-top-navigation a[data-target="' + activeHash + '"]');
68-
69-
if (tabEl.length < 1) {
70-
tabEl = $('.editor-top-navigation > li:first-of-type > a')
71-
}
72-
tabEl.tab('show');
73-
74-
$('.editor-top-navigation a').on('shown.bs.tab', function (e) {
75-
e.preventDefault();
76-
$(this).tab('show');
77-
var newActiveHash = $(e.target).data('target');
78-
window.location.hash = newActiveHash;
79-
localStorage.setItem(localStorageKey, newActiveHash);
80-
})
81-
});
82-
JS
83-
);
84-
8522
?>
8623
<main class="editor-main">
8724
<?php

src/assets/EditorAsset.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* @link http://www.diemeisterei.de/
4+
* @copyright Copyright (c) 2020 diemeisterei GmbH, Stuttgart
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace dmstr\modules\prototype\assets;
11+
12+
13+
use yii\bootstrap\BootstrapAsset;
14+
use yii\web\AssetBundle;
15+
16+
/**
17+
* @package dmstr\modules\prototype\assets
18+
* @author Elias Luhr <[email protected]>
19+
*/
20+
class EditorAsset extends AssetBundle
21+
{
22+
public $sourcePath = __DIR__ . '/web/editor';
23+
24+
public $css = [
25+
'styles/editor.less'
26+
];
27+
public $js = [
28+
'js/editor.js'
29+
];
30+
31+
public $depends = [
32+
BootstrapAsset::class
33+
];
34+
}

src/assets/web/editor/js/editor.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var currentLocation = document.location.toString();
2+
var activeHash = currentLocation.split('#')[1];
3+
var localStorageKey = 'dmstr.yii2.prototype.lastActiveHash';
4+
5+
$(function(){
6+
if (typeof activeHash === "undefined") {
7+
var lastActiveHash = localStorage.getItem(localStorageKey);
8+
if (lastActiveHash) {
9+
activeHash = lastActiveHash;
10+
}
11+
} else {
12+
activeHash = '#' + activeHash;
13+
}
14+
15+
var tabEl = $('.editor-top-navigation a[data-target="' + activeHash + '"]');
16+
17+
if (tabEl.length < 1) {
18+
tabEl = $('.editor-top-navigation > li:first-of-type > a')
19+
}
20+
21+
if (window.location.pathname.indexOf('new') > -1) {
22+
console.log('YES');
23+
tabEl = $('.editor-top-navigation a[data-target="#tab-9999999"]');
24+
}
25+
26+
tabEl.tab('show');
27+
28+
$('.editor-top-navigation a').on('shown.bs.tab', function (e) {
29+
e.preventDefault();
30+
$(this).tab('show');
31+
var newActiveHash = $(e.target).data('target');
32+
window.location.hash = newActiveHash;
33+
localStorage.setItem(localStorageKey, newActiveHash);
34+
})
35+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.editor-main {
2+
display: flex;
3+
}
4+
5+
.editor-sidebar {
6+
width: 25%;
7+
padding-right: 1rem;
8+
9+
.list-group {
10+
max-height: 75vh;
11+
overflow-y: scroll;
12+
13+
.list-group-item a:last-of-type {
14+
right: 0;
15+
}
16+
}
17+
}
18+
19+
.editor-top-navigation {
20+
list-style-type: none;
21+
padding-left: 0;
22+
margin-bottom: 1rem;
23+
24+
& > li {
25+
display: inline-block;
26+
}
27+
}
28+
29+
.editor-canvas {
30+
width: 75%;
31+
}

src/controllers/HtmlController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function actions()
3939
'class' => CloseEntryAction::class,
4040
'modelClass' => $this->modelClass
4141
];
42+
$actions['delete-entry'] = [
43+
'class' => CloseEntryAction::class,
44+
'modelClass' => $this->modelClass,
45+
'delete' => true
46+
];
4247
return $actions;
4348
}
4449
}

src/controllers/LessController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function actions()
4343
'class' => CloseEntryAction::class,
4444
'modelClass' => $this->modelClass
4545
];
46+
$actions['delete-entry'] = [
47+
'class' => CloseEntryAction::class,
48+
'modelClass' => $this->modelClass,
49+
'delete' => true
50+
];
4651
return $actions;
4752
}
4853
}

src/controllers/TwigController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function actions()
3939
'class' => CloseEntryAction::class,
4040
'modelClass' => $this->modelClass
4141
];
42+
$actions['delete-entry'] = [
43+
'class' => CloseEntryAction::class,
44+
'modelClass' => $this->modelClass,
45+
'delete' => true
46+
];
4247
return $actions;
4348
}
4449
}

src/traits/EditorEntry.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public static function activeEntries()
4848
{
4949
$allEntries = Yii::$app->session->get(md5(self::$cacheKey), []);
5050

51+
foreach ($allEntries as $key => $entries) {
52+
foreach ($entries as $entryId => $data) {
53+
$model = $key::find()->andWhere(['id' => $entryId])->asArray()->one();
54+
if ($model === null) {
55+
self::removeEntry($entryId);
56+
} else {
57+
$allEntries[$key][$entryId]['name'] = $model['key'];
58+
}
59+
}
60+
}
61+
5162
if (isset($allEntries[self::class])) {
5263
return $allEntries[self::class];
5364
}

0 commit comments

Comments
 (0)