profile.component.ts 6.17 KB
import {StateConfig, Resolve, Component, Inject, provide} from 'ng-forward';
import {ProfileInfoComponent} from './info/profile-info.component';
import {ProfileHomeComponent} from './profile-home.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";
import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service";
import {NotificationService} from "../shared/services/notification.service";
import {MyProfileComponent} from "./myprofile.component";
import {ProfileActionsComponent} from "./profile-actions.component";
import {ArticleService} from './../../lib/ng-noosfero-api/http/article.service';
/**
 * @ngdoc controller
 * @name profile.Profile
 * @description
 *  This is the profile controller. It provide routes to supported Noosfero Profiles.
 */

@Component({
    selector: 'profile',
    templateUrl: "app/profile/profile.html",
    directives: [ActivitiesComponent],
    providers: [
        provide('profileService', { useClass: ProfileService }),
        provide('notificationService', { useClass: NotificationService })
    ]
})
@StateConfig([
    {
        name: 'main.profile.info',
        url: "^/profile/:profile",
        component: ProfileInfoComponent,
        views: {
            "mainBlockContent": {
                templateUrl: "app/profile/info/profile-info.html",
                controller: ProfileInfoComponent,
                controllerAs: "vm"
            },
            "actions@main": {
                templateUrl: "app/profile/navbar-actions.html",
                controller: ProfileActionsComponent,
                controllerAs: "vm"
            }
        },
        resolve: {
            currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) {
                return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]);
            }
        }
    },
    {
        name: 'main.profile.settings',
        url: "^/myprofile/:profile",
        component: MyProfileComponent,
        views: {
            "actions@main": {
                templateUrl: "app/profile/navbar-actions.html",
                controller: ProfileActionsComponent,
                controllerAs: "vm"
            }
        },
        resolve: {
            currentProfile: function (ProfileService: ProfileService, $stateParams: angular.ui.IStateParamsService) {
                return ProfileService.getCurrentProfileByIdentifier($stateParams["profile"]);
            }
        }
    },
    {
        name: 'main.cms',
        url: "^/myprofile/:profile/cms?parent_id&type",
        component: CmsComponent,
        views: {
            "content": {
                templateUrl: "app/article/cms/cms.html",
                controller: CmsComponent,
                controllerAs: "vm"
            }
        }
    },
    {
        name: 'main.cmsEdit',
        url: "^/myprofile/:profile/cms/edit/:id",
        component: CmsComponent,
        views: {
            "content": {
                templateUrl: "app/article/cms/cms.html",
                controller: CmsComponent,
                controllerAs: "vm"
            }
        },
        resolve: {
            currentArticle: function ($q: ng.IQService, currentProfile: noosfero.Profile, articleService: ArticleService, $stateParams: angular.ui.IStateParamsService): ng.IPromise<noosfero.Article> {
                let deferred: ng.IDeferred<noosfero.Article> = $q.defer<noosfero.Article>();
                articleService.getArticleByProfileAndPath(currentProfile, $stateParams["page"]).then(result => deferred.resolve(result.data));
                return deferred.promise;
            }
        }
    },
    {
        name: 'main.profile.home',
        url: "",
        component: ProfileHomeComponent,
        views: {
            "mainBlockContent": {
                controller: ProfileHomeComponent,
                controllerAs: "vm"
            }
        }
    },
    {
        name: 'main.profile.page',
        url: "/{page:any}",
        component: ContentViewerComponent,
        views: {
            "mainBlockContent": {
                templateUrl: "app/article/content-viewer/page.html",
                controller: ContentViewerComponent,
                controllerAs: "vm"
            },
            "actions@main": {
                templateUrl: "app/article/content-viewer/navbar-actions.html",
                controller: ContentViewerActionsComponent,
                controllerAs: "vm"
            }
        },
        resolve: {
            currentArticle: function ($q: ng.IQService, currentProfile: noosfero.Profile, articleService: ArticleService, $stateParams: angular.ui.IStateParamsService): ng.IPromise<noosfero.Article> {
                let deferred: ng.IDeferred<noosfero.Article> = $q.defer<noosfero.Article>();
                articleService.getArticleByProfileAndPath(currentProfile, $stateParams["page"]).then(result => deferred.resolve(result.data));
                return deferred.promise;
            }
        }
    }
])


/**
 * @ngdoc controller
 * @name ProfileComponent
 * @description
 *  Used as base controller for the route /:profile
 *  the  "currentProfile" injected is provided by the "resolve" currentProfile
 *  available at @MainComponent @StateConfig definition
 */
@Inject(ProfileService, "$state", "currentProfile")
export class ProfileComponent {
    boxes: noosfero.Box[];

    constructor(
        profileService: ProfileService,
        $state: ng.ui.IStateService,
        notificationService: NotificationService,
        public profile: noosfero.Profile) {
        profileService.getBoxes(<number>this.profile.id)
            .then((response: restangular.IResponse) => {
                this.boxes = response.data.boxes;
            }).catch(() => {
                $state.transitionTo('main.environment.home');
                notificationService.error({ message: "notification.profile.not_found" });
            });
    }
}