From f9a71d965e2c8a7440dd7f9fcc357c3ea4a801da Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 9 May 2016 11:05:01 -0300 Subject: [PATCH] Refactor of article cms --- src/app/article/basic-editor/basic-editor.component.spec.ts | 84 ------------------------------------------------------------------------------------ src/app/article/basic-editor/basic-editor.component.ts | 69 --------------------------------------------------------------------- src/app/article/basic-editor/basic-editor.html | 34 ---------------------------------- src/app/article/basic-editor/basic-editor.scss | 21 --------------------- src/app/article/cms/basic-editor/basic-editor.component.ts | 11 +++++++++++ src/app/article/cms/basic-editor/basic-editor.html | 10 ++++++++++ src/app/article/cms/basic-editor/basic-editor.scss | 0 src/app/article/cms/basic-options/basic-options.component.ts | 11 +++++++++++ src/app/article/cms/basic-options/basic-options.html | 15 +++++++++++++++ src/app/article/cms/basic-options/basic-options.scss | 15 +++++++++++++++ src/app/article/cms/cms.component.spec.ts | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/app/article/cms/cms.component.ts | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/app/article/cms/cms.html | 18 ++++++++++++++++++ src/app/article/cms/cms.scss | 4 ++++ src/app/profile/profile.component.ts | 17 +++++++++-------- 15 files changed, 250 insertions(+), 216 deletions(-) delete mode 100644 src/app/article/basic-editor/basic-editor.component.spec.ts delete mode 100644 src/app/article/basic-editor/basic-editor.component.ts delete mode 100644 src/app/article/basic-editor/basic-editor.html delete mode 100644 src/app/article/basic-editor/basic-editor.scss create mode 100644 src/app/article/cms/basic-editor/basic-editor.component.ts create mode 100644 src/app/article/cms/basic-editor/basic-editor.html create mode 100644 src/app/article/cms/basic-editor/basic-editor.scss create mode 100644 src/app/article/cms/basic-options/basic-options.component.ts create mode 100644 src/app/article/cms/basic-options/basic-options.html create mode 100644 src/app/article/cms/basic-options/basic-options.scss create mode 100644 src/app/article/cms/cms.component.spec.ts create mode 100644 src/app/article/cms/cms.component.ts create mode 100644 src/app/article/cms/cms.html create mode 100644 src/app/article/cms/cms.scss diff --git a/src/app/article/basic-editor/basic-editor.component.spec.ts b/src/app/article/basic-editor/basic-editor.component.spec.ts deleted file mode 100644 index 7673a8e..0000000 --- a/src/app/article/basic-editor/basic-editor.component.spec.ts +++ /dev/null @@ -1,84 +0,0 @@ -import {quickCreateComponent} from "../../../spec/helpers"; -import {BasicEditorComponent} from "./basic-editor.component"; - - -describe("Article BasicEditor", () => { - - let $rootScope: ng.IRootScopeService; - let $q: ng.IQService; - let articleServiceMock: any; - let profileServiceMock: any; - let $state: any; - let $stateParams: any; - let $window: any; - let profile = { id: 1 }; - let notification: any; - - - beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { - $rootScope = _$rootScope_; - $q = _$q_; - })); - - beforeEach(() => { - $window = jasmine.createSpyObj("$window", ["back"]); - $state = jasmine.createSpyObj("$state", ["go"]); - notification = jasmine.createSpyObj("notification", ["success"]); - profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier"]); - articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "updateArticle", "get"]); - - $stateParams = { profile: "profile" }; - - let setCurrentProfileByIdentifierResponse = $q.defer(); - setCurrentProfileByIdentifierResponse.resolve(profile); - - let articleCreate = $q.defer(); - articleCreate.resolve({ data: { path: "path", profile: { identifier: "profile" } } }); - - let articleGet = $q.defer(); - articleGet.resolve({ data: { path: "parent-path", profile: { identifier: "profile" } } }); - - profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise); - articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise); - articleServiceMock.updateArticle = jasmine.createSpy("updateArticle").and.returnValue(articleCreate.promise); - articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise); - }); - - it("create an article in the current profile when save", done => { - $stateParams['parent_id'] = 1; - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); - component.save(); - $rootScope.$apply(); - expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); - expect(articleServiceMock.createInParent).toHaveBeenCalledWith(1, component.article); - done(); - }); - - it("got to the new article page and display an alert when saving sucessfully", done => { - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); - component.save(); - $rootScope.$apply(); - expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); - expect(notification.success).toHaveBeenCalled(); - done(); - }); - - it("go back when cancel article edition", done => { - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); - $window.history = { back: jasmine.createSpy('back') }; - component.cancel(); - expect($window.history.back).toHaveBeenCalled(); - done(); - }); - - it("edit existing article when save", done => { - $stateParams['parent_id'] = null; - $stateParams['id'] = 2; - let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); - component.save(); - $rootScope.$apply(); - expect(articleServiceMock.updateArticle).toHaveBeenCalledWith(component.article); - done(); - }); - -}); diff --git a/src/app/article/basic-editor/basic-editor.component.ts b/src/app/article/basic-editor/basic-editor.component.ts deleted file mode 100644 index 68311b9..0000000 --- a/src/app/article/basic-editor/basic-editor.component.ts +++ /dev/null @@ -1,69 +0,0 @@ -import {StateConfig, Component, Inject, provide} from 'ng-forward'; -import {ArticleService} from "../../../lib/ng-noosfero-api/http/article.service"; -import {ProfileService} from "../../../lib/ng-noosfero-api/http/profile.service"; -import {NotificationService} from "../../shared/services/notification.service.ts"; - -@Component({ - selector: 'article-basic-editor', - templateUrl: "app/article/basic-editor/basic-editor.html", - providers: [ - provide('articleService', { useClass: ArticleService }), - provide('profileService', { useClass: ProfileService }), - provide('notification', { useClass: NotificationService }) - ] -}) -@Inject(ArticleService, ProfileService, "$state", NotificationService, "$stateParams", "$window") -export class BasicEditorComponent { - - article: noosfero.Article = { type: "TextArticle" }; - parent: noosfero.Article = {}; - - id: number; - parentId: number; - profileIdentifier: string; - - constructor(private articleService: ArticleService, - private profileService: ProfileService, - private $state: ng.ui.IStateService, - private notification: NotificationService, - private $stateParams: ng.ui.IStateParamsService, - private $window: ng.IWindowService) { - - this.parentId = this.$stateParams['parent_id']; - this.profileIdentifier = this.$stateParams["profile"]; - this.id = this.$stateParams['id']; - - if (this.parentId) { - this.articleService.get(this.parentId).then((result: noosfero.RestResult) => { - this.parent = result.data; - }); - } - if (this.id) { - this.articleService.get(this.id).then((result: noosfero.RestResult) => { - this.article = result.data; - this.article.name = this.article.title; // FIXME - }); - } - } - - save() { - this.profileService.setCurrentProfileByIdentifier(this.profileIdentifier).then((profile: noosfero.Profile) => { - if (this.id) { - return this.articleService.updateArticle(this.article); - } else { - return this.articleService.createInParent(this.parentId, this.article); - } - }).then((response: noosfero.RestResult) => { - let article = (response.data); - this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier }); - this.notification.success({ title: "article.basic_editor.success.title", message: "article.basic_editor.success.message" }); - }).catch(() => { - this.notification.error({ message: "article.basic_editor.save.failed" }); - }); - } - - cancel() { - this.$window.history.back(); - } - -} diff --git a/src/app/article/basic-editor/basic-editor.html b/src/app/article/basic-editor/basic-editor.html deleted file mode 100644 index a22d9d0..0000000 --- a/src/app/article/basic-editor/basic-editor.html +++ /dev/null @@ -1,34 +0,0 @@ -
-
-
-
-
-
- - -
-
- - -
- - -
-
-
-
-
{{"article.basic_editor.visibility" | translate}}
-
-
- - {{"article.basic_editor.visibility.public" | translate}} -
-
- - {{"article.basic_editor.visibility.private" | translate}} -
-
-
-
-
-
diff --git a/src/app/article/basic-editor/basic-editor.scss b/src/app/article/basic-editor/basic-editor.scss deleted file mode 100644 index 7b8fdab..0000000 --- a/src/app/article/basic-editor/basic-editor.scss +++ /dev/null @@ -1,21 +0,0 @@ -.basic-editor { - @extend .container-fluid; - padding: 0 1%; - - .side-options { - @extend .col-md-3; - margin-top: 25px; - - .visibility { - .panel-heading { - background-color: transparent; - font-weight: bold; - } - .panel-body { - i { - color: #A5A5A5; - } - } - } - } -} diff --git a/src/app/article/cms/basic-editor/basic-editor.component.ts b/src/app/article/cms/basic-editor/basic-editor.component.ts new file mode 100644 index 0000000..42d4395 --- /dev/null +++ b/src/app/article/cms/basic-editor/basic-editor.component.ts @@ -0,0 +1,11 @@ +import {Component, Input} from 'ng-forward'; + +@Component({ + selector: 'article-basic-editor', + templateUrl: "app/article/cms/basic-editor/basic-editor.html" +}) +export class BasicEditorComponent { + + @Input() article: noosfero.Article; + +} diff --git a/src/app/article/cms/basic-editor/basic-editor.html b/src/app/article/cms/basic-editor/basic-editor.html new file mode 100644 index 0000000..516ef7c --- /dev/null +++ b/src/app/article/cms/basic-editor/basic-editor.html @@ -0,0 +1,10 @@ +
+
+ + +
+
+ + +
+
diff --git a/src/app/article/cms/basic-editor/basic-editor.scss b/src/app/article/cms/basic-editor/basic-editor.scss new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/app/article/cms/basic-editor/basic-editor.scss diff --git a/src/app/article/cms/basic-options/basic-options.component.ts b/src/app/article/cms/basic-options/basic-options.component.ts new file mode 100644 index 0000000..24e56ea --- /dev/null +++ b/src/app/article/cms/basic-options/basic-options.component.ts @@ -0,0 +1,11 @@ +import {Component, Input} from 'ng-forward'; + +@Component({ + selector: 'article-basic-options', + templateUrl: "app/article/cms/basic-options/basic-options.html" +}) +export class BasicOptionsComponent { + + @Input() article: noosfero.Article; + +} diff --git a/src/app/article/cms/basic-options/basic-options.html b/src/app/article/cms/basic-options/basic-options.html new file mode 100644 index 0000000..35761ad --- /dev/null +++ b/src/app/article/cms/basic-options/basic-options.html @@ -0,0 +1,15 @@ +
+
+
{{"article.basic_editor.visibility" | translate}}
+
+
+ + {{"article.basic_editor.visibility.public" | translate}} +
+
+ + {{"article.basic_editor.visibility.private" | translate}} +
+
+
+
diff --git a/src/app/article/cms/basic-options/basic-options.scss b/src/app/article/cms/basic-options/basic-options.scss new file mode 100644 index 0000000..e1bd411 --- /dev/null +++ b/src/app/article/cms/basic-options/basic-options.scss @@ -0,0 +1,15 @@ +.side-options { + margin-top: 25px; + + .visibility { + .panel-heading { + background-color: transparent; + font-weight: bold; + } + .panel-body { + i { + color: #A5A5A5; + } + } + } +} diff --git a/src/app/article/cms/cms.component.spec.ts b/src/app/article/cms/cms.component.spec.ts new file mode 100644 index 0000000..c11a6cd --- /dev/null +++ b/src/app/article/cms/cms.component.spec.ts @@ -0,0 +1,84 @@ +import {quickCreateComponent} from "../../../spec/helpers"; +import {CmsComponent} from "./cms.component"; + + +describe("Article Cms", () => { + + let $rootScope: ng.IRootScopeService; + let $q: ng.IQService; + let articleServiceMock: any; + let profileServiceMock: any; + let $state: any; + let $stateParams: any; + let $window: any; + let profile = { id: 1 }; + let notification: any; + + + beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { + $rootScope = _$rootScope_; + $q = _$q_; + })); + + beforeEach(() => { + $window = jasmine.createSpyObj("$window", ["back"]); + $state = jasmine.createSpyObj("$state", ["go"]); + notification = jasmine.createSpyObj("notification", ["success"]); + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier"]); + articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "updateArticle", "get"]); + + $stateParams = { profile: "profile" }; + + let setCurrentProfileByIdentifierResponse = $q.defer(); + setCurrentProfileByIdentifierResponse.resolve(profile); + + let articleCreate = $q.defer(); + articleCreate.resolve({ data: { path: "path", profile: { identifier: "profile" } } }); + + let articleGet = $q.defer(); + articleGet.resolve({ data: { path: "parent-path", profile: { identifier: "profile" } } }); + + profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise); + articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise); + articleServiceMock.updateArticle = jasmine.createSpy("updateArticle").and.returnValue(articleCreate.promise); + articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise); + }); + + it("create an article in the current profile when save", done => { + $stateParams['parent_id'] = 1; + let component: CmsComponent = new CmsComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); + component.save(); + $rootScope.$apply(); + expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); + expect(articleServiceMock.createInParent).toHaveBeenCalledWith(1, component.article); + done(); + }); + + it("got to the new article page and display an alert when saving sucessfully", done => { + let component: CmsComponent = new CmsComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); + component.save(); + $rootScope.$apply(); + expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" }); + expect(notification.success).toHaveBeenCalled(); + done(); + }); + + it("go back when cancel article edition", done => { + let component: CmsComponent = new CmsComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); + $window.history = { back: jasmine.createSpy('back') }; + component.cancel(); + expect($window.history.back).toHaveBeenCalled(); + done(); + }); + + it("edit existing article when save", done => { + $stateParams['parent_id'] = null; + $stateParams['id'] = 2; + let component: CmsComponent = new CmsComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams, $window); + component.save(); + $rootScope.$apply(); + expect(articleServiceMock.updateArticle).toHaveBeenCalledWith(component.article); + done(); + }); + +}); diff --git a/src/app/article/cms/cms.component.ts b/src/app/article/cms/cms.component.ts new file mode 100644 index 0000000..509b396 --- /dev/null +++ b/src/app/article/cms/cms.component.ts @@ -0,0 +1,73 @@ +import {StateConfig, Component, Inject, provide} from 'ng-forward'; +import {ArticleService} from "../../../lib/ng-noosfero-api/http/article.service"; +import {ProfileService} from "../../../lib/ng-noosfero-api/http/profile.service"; +import {NotificationService} from "../../shared/services/notification.service.ts"; +import {BasicOptionsComponent} from './basic-options/basic-options.component'; +import {BasicEditorComponent} from './basic-editor/basic-editor.component'; + +@Component({ + selector: 'article-cms', + templateUrl: "app/article/cms/cms.html", + providers: [ + provide('articleService', { useClass: ArticleService }), + provide('profileService', { useClass: ProfileService }), + provide('notification', { useClass: NotificationService }) + ], + directives: [BasicOptionsComponent, BasicEditorComponent] +}) +@Inject(ArticleService, ProfileService, "$state", NotificationService, "$stateParams", "$window") +export class CmsComponent { + + article: noosfero.Article; + parent: noosfero.Article = {}; + + id: number; + parentId: number; + profileIdentifier: string; + + constructor(private articleService: ArticleService, + private profileService: ProfileService, + private $state: ng.ui.IStateService, + private notification: NotificationService, + private $stateParams: ng.ui.IStateParamsService, + private $window: ng.IWindowService) { + + this.parentId = this.$stateParams['parent_id']; + this.profileIdentifier = this.$stateParams["profile"]; + this.id = this.$stateParams['id']; + + if (this.parentId) { + this.article = { type: this.$stateParams['type'] || "TextArticle", published: true }; + this.articleService.get(this.parentId).then((result: noosfero.RestResult) => { + this.parent = result.data; + }); + } + if (this.id) { + this.articleService.get(this.id).then((result: noosfero.RestResult) => { + this.article = result.data; + this.article.name = this.article.title; // FIXME + }); + } + } + + save() { + this.profileService.setCurrentProfileByIdentifier(this.profileIdentifier).then((profile: noosfero.Profile) => { + if (this.id) { + return this.articleService.updateArticle(this.article); + } else { + return this.articleService.createInParent(this.parentId, this.article); + } + }).then((response: noosfero.RestResult) => { + let article = (response.data); + this.$state.go('main.profile.page', { page: article.path, profile: article.profile.identifier }); + this.notification.success({ title: "article.basic_editor.success.title", message: "article.basic_editor.success.message" }); + }).catch(() => { + this.notification.error({ message: "article.basic_editor.save.failed" }); + }); + } + + cancel() { + this.$window.history.back(); + } + +} diff --git a/src/app/article/cms/cms.html b/src/app/article/cms/cms.html new file mode 100644 index 0000000..2943b85 --- /dev/null +++ b/src/app/article/cms/cms.html @@ -0,0 +1,18 @@ +
+
+
+
+ +
+
+ +
+
+
+
+
+ + +
+
+
diff --git a/src/app/article/cms/cms.scss b/src/app/article/cms/cms.scss new file mode 100644 index 0000000..a598cf2 --- /dev/null +++ b/src/app/article/cms/cms.scss @@ -0,0 +1,4 @@ +.cms { + @extend .container-fluid; + padding: 0 1%; +} diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index 988d97c..313025b 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -1,7 +1,8 @@ import {StateConfig, Component, Inject, provide} from 'ng-forward'; import {ProfileInfoComponent} from './info/profile-info.component'; import {ProfileHomeComponent} from './profile-home.component'; -import {BasicEditorComponent} from '../article/basic-editor/basic-editor.component'; +import {BasicEditorComponent} from '../article/cms/basic-editor/basic-editor.component'; +import {CmsComponent} from '../article/cms/cms.component'; import {ContentViewerComponent} from "../article/content-viewer/content-viewer.component"; import {ContentViewerActionsComponent} from "../article/content-viewer/content-viewer-actions.component"; import {ActivitiesComponent} from "./activities/activities.component"; @@ -46,12 +47,12 @@ import {MyProfileComponent} from "./myprofile.component"; }, { name: 'main.cms', - url: "^/myprofile/:profile/cms?parent_id", - component: BasicEditorComponent, + url: "^/myprofile/:profile/cms?parent_id&type", + component: CmsComponent, views: { "content": { - templateUrl: "app/article/basic-editor/basic-editor.html", - controller: BasicEditorComponent, + templateUrl: "app/article/cms/cms.html", + controller: CmsComponent, controllerAs: "vm" } } @@ -59,11 +60,11 @@ import {MyProfileComponent} from "./myprofile.component"; { name: 'main.cmsEdit', url: "^/myprofile/:profile/cms/edit/:id", - component: BasicEditorComponent, + component: CmsComponent, views: { "content": { - templateUrl: "app/article/basic-editor/basic-editor.html", - controller: BasicEditorComponent, + templateUrl: "app/article/cms/cms.html", + controller: CmsComponent, controllerAs: "vm" } } -- libgit2 0.21.2