Skip to content

Commit 1f63719

Browse files
committed
Refactor Pages module
1 parent 3b6b77c commit 1f63719

7 files changed

Lines changed: 261 additions & 200 deletions

File tree

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,99 @@
11
'use strict';
2-
angular.module('com.module.pages')
3-
.config(function($stateProvider) {
4-
$stateProvider.state('app.pages', {
5-
abstract: true,
6-
url: '/pages',
7-
templateUrl: 'modules/pages/views/main.html'
8-
})
9-
.state('app.pages.list', {
10-
url: '',
11-
templateUrl: 'modules/pages/views/list.html',
12-
controller: 'PagesCtrl'
13-
})
14-
.state('app.pages.add', {
15-
url: '/add',
16-
templateUrl: 'modules/pages/views/form.html',
17-
controller: 'PagesCtrl'
18-
})
19-
.state('app.pages.edit', {
20-
url: '/:id/edit',
21-
templateUrl: 'modules/pages/views/form.html',
22-
controller: 'PagesCtrl'
23-
})
24-
.state('app.pages.view', {
25-
url: '/:id',
26-
templateUrl: 'modules/pages/views/view.html',
27-
controller: 'PagesCtrl'
2+
3+
var app = angular.module('com.module.pages');
4+
5+
app.config(function ($stateProvider) {
6+
$stateProvider.state('app.pages', {
7+
abstract: true,
8+
url: '/pages',
9+
templateUrl: 'modules/pages/views/main.html'
10+
}).state('app.pages.list', {
11+
url: '',
12+
templateUrl: 'modules/pages/views/list.html',
13+
controllerAs: 'ctrl',
14+
controller: function (pages) {
15+
this.pages = pages;
16+
},
17+
resolve: {
18+
pages: function (PageService) {
19+
return PageService.find();
20+
}
21+
}
22+
}).state('app.pages.add', {
23+
url: '/add',
24+
templateUrl: 'modules/pages/views/form.html',
25+
controllerAs: 'ctrl',
26+
controller: function ($state, PageService, page) {
27+
this.editorOptions = {
28+
theme: 'monokai',
29+
lineWrapping: true,
30+
lineNumbers: true,
31+
mode: 'markdown'
32+
};
33+
this.page = page;
34+
this.formFields = PageService.getFormFields();
35+
this.formOptions = {};
36+
this.submit = function () {
37+
PageService.upsert(this.page).then(function () {
38+
$state.go('^.list');
39+
});
40+
};
41+
},
42+
resolve: {
43+
page: function () {
44+
return {
45+
content: '# Hi!\n\n## This is a markdown editor.\n\n fine code goes here \n\n- lists \n- go \n- here ' +
46+
'\n\n*Find* **more information** about `markdown` [Here](https://daringfireball.net/projects/markdown/basics)!'
47+
};
48+
}
49+
}
50+
}).state('app.pages.edit', {
51+
url: '/:id/edit',
52+
templateUrl: 'modules/pages/views/form.html',
53+
controllerAs: 'ctrl',
54+
controller: function ($state, PageService, page) {
55+
this.editorOptions = {
56+
theme: 'monokai',
57+
lineWrapping: true,
58+
lineNumbers: true,
59+
mode: 'markdown'
60+
};
61+
this.page = page;
62+
this.formFields = PageService.getFormFields();
63+
this.formOptions = {};
64+
this.submit = function () {
65+
PageService.upsert(this.page).then(function () {
66+
$state.go('^.list');
67+
});
68+
};
69+
},
70+
resolve: {
71+
page: function ($stateParams, PageService) {
72+
return PageService.findById($stateParams.id);
73+
}
74+
}
75+
}).state('app.pages.view', {
76+
url: '/:id',
77+
templateUrl: 'modules/pages/views/view.html',
78+
controllerAs: 'ctrl',
79+
controller: function (page) {
80+
this.page = page;
81+
},
82+
resolve: {
83+
page: function ($stateParams, PageService) {
84+
return PageService.findById($stateParams.id);
85+
}
86+
}
87+
}).state('app.pages.delete', {
88+
url: '/:id/delete',
89+
template: '',
90+
controllerAs: 'ctrl',
91+
controller: function ($stateParams, $state, PageService) {
92+
PageService.delete($stateParams.id, function () {
93+
$state.go('^.list');
94+
}, function () {
95+
$state.go('^.list');
2896
});
97+
}
2998
});
99+
});

client/app/modules/pages/controllers/pages.ctrl.js

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
var app = angular.module('com.module.pages');
3+
4+
app.service('PageService', ['$state', 'CoreService', 'Page', 'gettextCatalog',
5+
function ($state, CoreService, Page, gettextCatalog) {
6+
7+
this.find = function () {
8+
return Page.find().$promise;
9+
};
10+
11+
this.findById = function (id) {
12+
return Page.findById({
13+
id: id
14+
}).$promise;
15+
};
16+
17+
this.upsert = function (page) {
18+
return Page.upsert(page).$promise
19+
.then(function () {
20+
CoreService.toastSuccess(
21+
gettextCatalog.getString('Page saved'),
22+
gettextCatalog.getString('Your page is safe with us!')
23+
);
24+
})
25+
.catch(function (err) {
26+
CoreService.toastError(
27+
gettextCatalog.getString('Error saving page '),
28+
gettextCatalog.getString('This page could no be saved: ' + err)
29+
);
30+
}
31+
);
32+
};
33+
34+
this.delete = function (id, successCb, cancelCb) {
35+
CoreService.confirm(
36+
gettextCatalog.getString('Are you sure?'),
37+
gettextCatalog.getString('Deleting this cannot be undone'),
38+
function () {
39+
Page.deleteById({id: id}, function () {
40+
CoreService.toastSuccess(
41+
gettextCatalog.getString('Page deleted'),
42+
gettextCatalog.getString('Your page is deleted!'));
43+
successCb();
44+
}, function (err) {
45+
CoreService.toastError(
46+
gettextCatalog.getString('Error deleting page'),
47+
gettextCatalog.getString('Your page is not deleted! ') + err);
48+
cancelCb();
49+
});
50+
},
51+
function () {
52+
cancelCb();
53+
}
54+
);
55+
};
56+
57+
58+
this.getFormFields = function () {
59+
var form = [{
60+
key: 'name',
61+
type: 'input',
62+
templateOptions: {
63+
label: gettextCatalog.getString('Name'),
64+
required: true
65+
}
66+
}, {
67+
key: 'slug',
68+
type: 'input',
69+
templateOptions: {
70+
label: gettextCatalog.getString('Slug'),
71+
required: true
72+
}
73+
}];
74+
return form;
75+
};
76+
77+
}
78+
]);
Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,41 @@
1-
<div class="box">
2-
<div class="box-header">
3-
<span class="box-title">
4-
<div class="input-group">
5-
<a href="" ui-sref="^.list" class=" btn btn-default " style="float: left;" translate><i
6-
class="fa fa-arrow-left"></i> Pages</a>
7-
<input ng-model="page.name" type="text" class="form-controls" placeholder="Name" required="required"
8-
style="margin-left: 2px; border: 0px;">
9-
</div>
10-
</span>
1+
<form name="form">
2+
<div class="panel panel-default">
3+
<div class="panel-heading">
4+
<a href="" ui-sref="^.list" class="btn btn-xs btn-default"><i class="fa fa-arrow-left"></i></a>
5+
<span ng-show="!ctrl.page.id" translate>Add a new page</span>
6+
<span ng-show="ctrl.page.id" translate>Edit page</span>
7+
</div>
8+
<div class="panel-body">
9+
<div class="row">
10+
<div class="col-md-6">
11+
<formly-form model="ctrl.page" fields="ctrl.formFields" options="ctrl.formOptions"></formly-form>
12+
13+
<ui-codemirror ui-codemirror-opts="ctrl.editorOptions" ng-model="ctrl.page.content"></ui-codemirror>
1114

12-
<div class="box-tools pull-right">
13-
<span class="pull-right btn-group">
14-
<button class="btn btn-primary btn-success btn-sm" ng-click="onSubmit()" translate><i class="fa fa-save"></i> Save
15-
</button>
16-
<a href="" class="btn btn-sm btn-default" ui-sref="^.view({id: page.id})">
17-
<i class="fa fa-eye"></i>
18-
</a>
19-
<a href="" class="btn btn-sm btn-default" ng-click="delete({id: page.id})">
20-
<i class="fa fa-trash-o"></i>
21-
</a>
22-
</span>
15+
<br/>
2316
</div>
17+
18+
<div class="col-md-6">
19+
<div btf-markdown="ctrl.page.content"></div>
20+
</div>
21+
</div>
22+
<div class="controls">
23+
<button type="submit" class="btn btn-primary btn-success" ng-click="ctrl.submit()" ng-disabled="form.$invalid">
24+
Submit
25+
</button>
26+
<button type="button" class="btn btn-default" ng-click="ctrl.formOptions.resetModel()">Reset</button>
27+
</div>
2428
</div>
25-
<div class="box-body">
26-
<div class="alert alert-danger" ng-if="addError" translate>Could not add page!</div>
27-
<tabset class="nav-tabs-custom">
28-
<tab heading="Editor">
29-
<ui-codemirror ui-codemirror-opts="editorOptions" ng-model="page.content"></ui-codemirror>
30-
</tab>
31-
<tab heading="Preview">
32-
<div btf-markdown="page.content"></div>
33-
</tab>
34-
</tabset>
35-
</div>
36-
<div ng-if="loading" class="overlay"></div>
37-
<div ng-if="loading" class="loading-img"></div>
38-
</div>
39-
<style>
40-
.CodeMirror {
41-
border: 1px solid #eee;
42-
height: auto;
43-
}
29+
</div>
30+
</form>
4431

45-
.CodeMirror-scroll {
46-
overflow-y: hidden;
47-
overflow-x: auto;
48-
}
49-
</style>
32+
<style>
33+
.CodeMirror {
34+
border: 1px solid #eee;
35+
height: auto;
36+
}
37+
.CodeMirror-scroll {
38+
overflow-y: hidden;
39+
overflow-x: auto;
40+
}
41+
</style>

0 commit comments

Comments
 (0)