9

I'm fairly new to Angular 2 and are trying to get a grip of how to integrate Angular 2 with existing Javascript UI Framework libraries.

Now I'm trying to play with the jQuery plugin http://fullcalendar.io Or actually I want to use the premium add-on called Scheduler.

However I created a simple example in Plunker...

Feel free to use it and enlighten me in how to make it display and also how to respond to clicking on specific events.

https://plnkr.co/edit/eMK6Iy

...the component FullCalendarComponent needs modification of course. Problem is I don't know how.

import {Component} from 'angular2/core';

@Component({
    selector: 'full-calendar',
    template: '<p>Here I would like to see a calendar</p>'
})

export class FullCalendarComponent { }
Magnus Wallström
  • 1,469
  • 4
  • 15
  • 23
  • 1
    PrimeNG has integration with FullCalendar called Schedule Component. http://www.primefaces.org/primeng/#/schedule Code of the component is open source at https://github.com/primefaces/primeng/blob/master/components/schedule/schedule.ts – Cagatay Civici Apr 06 '16 at 08:07
  • Ah, that looks like a good starting point. Actually I need to use the add-on called Scheduler. I notice that you seem to be the one working with these briljant components. So if you have some time over. Feel free to add support for the Scheduler add-on as well :) – Magnus Wallström Apr 06 '16 at 08:57
  • Thanks, scheduler module seems a commercial add-on so not sure how we can integrate it in PrimeNG. – Cagatay Civici Apr 06 '16 at 11:53
  • @MagnusWallström, did you find a solution for scheduler in angular 2 ? – mihai May 04 '16 at 06:00
  • 2
    Yes I actually did meorfi. I added an answer below. I hope you find it helpful. Good luck! – Magnus Wallström May 04 '16 at 16:56
  • @CagatayCivici I think you can use the GPL license for FullCalendar-Scheduler plugin. As noted at their site, https://fullcalendar.io/scheduler/license/ "Under the GPL license, you may use Scheduler without charge. You may even modify its source code and redistribute it under the same license. However, there is one big caveat. Any project that leverages Scheduler must be open source. " – Harinder Jul 11 '17 at 22:38

5 Answers5

7

Here is the way I managed to get the Scheduler working in an Angular2 project. I started with the component called Schedule created by PrimeNG as suggested in a comment above by Cagatay Civici.

I had to modify the file scheduler.component.ts like below.

export class Scheduler {
// Support for Scheduler
@Input() resources: any[];   
@Input() resourceAreaWidth: string;            
@Input() resourceLabelText: string; 
// End Support for Scheduler

// Added functionality
@Input() slotLabelFormat: any;
@Input() titleFormat: any;
@Input() eventBackgroundColor: any;
// End added properties

@Input() events: any[];
............
............
ngAfterViewInit() {
    this.schedule = jQuery(this.el.nativeElement.children[0]);
    this.schedule.fullCalendar({
        resources: this.resources,
        resourceAreaWidth: this.resourceAreaWidth,
        resourceLabelText: this.resourceLabelText,
        titleFormat: this.titleFormat,
        slotLabelFormat: this.slotLabelFormat,
        eventBackgroundColor: this.eventBackgroundColor,
        theme: true,
        header: this.header,
...........

Then I had to add the css and script for fullcalendar and scheduler to the index.html.

Magnus Wallström
  • 1,469
  • 4
  • 15
  • 23
  • how did you used this Scheduler (posted in answer) : like a directive, or somehow in a html template? – mihai May 05 '16 at 13:43
  • I found out how to use it :) Thanks. By the way: are you able to display the resources in first column when are switching to week view? – mihai May 05 '16 at 14:56
  • The resources in the left column was available but the events disappeared when I tested to switch to week view. However for my purpose I'm not interested in the week view and have limited time to investigate the issue. I hope that you manage to resolve it! – Magnus Wallström May 06 '16 at 08:01
  • Do we need to import JQuery as dependencies? And if yes, how please? – fabLouis Jun 21 '16 at 12:35
  • @fabLouis Yes, I have declared jquery in the scheduler.component.ts. Add `declare var jquery: any;` before the @Component-decorator. – Magnus Wallström Jun 21 '16 at 13:39
  • @magnusWallstrom Im following what you did, how did you manage to get 'jQuery' working? – Linc Abela Jun 28 '16 at 07:30
  • 2
    @LincAbela I uploaded a demo to github. Take a look at [https://github.com/magwalls/fcscheduler](https://github.com/magwalls/fcscheduler). Maybe that can be of some help. – Magnus Wallström Jun 29 '16 at 14:26
  • @MagnusWallström- Github repo is not opening can you check and provide a new link. Is this file need to add in component https://github.com/primefaces/primeng/blob/master/components/schedule/schedule.ts... can you add complete this.schedule.fullCalendar function – Sandeep Chikhale Aug 05 '16 at 06:25
  • @LincAbela Did you manage to make it work. if yes can you share any solution with us- what about JQuery – Sandeep Chikhale Aug 05 '16 at 07:04
  • 2
    How do you import the typescript files for fullcalendar, i cannot seem to get fullcalendar.js to load? is there an import statement? - i am loading jquery through import statements not via script tag into index.html. – Piotr Stulinski Sep 01 '16 at 14:28
4

I created an npm package

npm install angular2-fullcalendar --save

you can use the options input to fully customize the fullcalendar component

check out the docs here https://github.com/nekken/ng2-fullcalendar

Nekken
  • 71
  • 2
  • 1
    Could you please provide/add demo to your github repo using angular2. – SaiKiran Mandhala Jan 23 '17 at 17:04
  • 1
    @SaiKiranMandhala i could not make it run with Angular2 and SystemJs. After changing to webpack i could load the fullcalendar. But i struggle with displaying the events now...https://github.com/nekken/ng2-fullcalendar/issues/13 this is the issue i opened, maybe it can help you – messerbill Jan 24 '17 at 17:03
0

The angular2-fullcalendar is not running with the latest versions of Angular CLI. I opened a bug informing about the solution here: Angular2-fullcalendar is not an NgModule. Unexpected directive CalendarComponent

Community
  • 1
  • 1
Lupe
  • 232
  • 1
  • 12
0

ng-fullcalendar wraps the fullcalendar module for Angular 2.

https://github.com/jamaks/ng-fullcalendar

I do not think wrapping it by yourself is a good idea and ng2-fullcalendar is not under maintain. ng-fullcalendar is a relatively new project and has many active check-in.

Nicholas Lu
  • 1,655
  • 15
  • 14
-1

Before starting, some prerequisites in javascript directory:

jquery-ui.min.js

jquery.min.js

fullcalendar.js

calendar.js

angular.js

bootstrap.js

Files that will be needed in CSS directory:

fullcalendar.css

bootstrap.css

Now create the controller to handle the data and events:-

angular.module('myCalendarApp', ['ui.calendar']);
function CalendarCtrl($scope, $http) {

    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
    var currentView = "month";


    //event source that pulls from google.com
    $scope.eventSource = {
            url: "http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic",
            className: 'gcal-event',           // an option!
            currentTimezone: 'America/Chicago' // an option!
    };


    //This will call onLoad and you can assign the values the way you want to the calendar
    //here DataRetriever.jsp will give me array of JSON data generated from the database data
    $http.get('DataRetriever.jsp').success(function(data) {
        for(var i = 0; i < data.length; i++)
        {
            $scope.events[i] = {id:data[i].id, title: data[i].task,start: new Date(data[i].start), end: new Date(data[i].end),allDay: false};
        }
    });

    /*
    //to explicitly add events to the calendar
    //you can add the events in following ways
    $scope.events = [
      {title: 'All Day Event',start: new Date('Thu Oct 17 2013 09:00:00 GMT+0530 (IST)')},
      {title: 'Long Event',start: new Date('Thu Oct 17 2013 10:00:00 GMT+0530 (IST)'),end: new Date('Thu Oct 17 2013 17:00:00 GMT+0530 (IST)')},
      {id: 999,title: 'Repeating Event',start: new Date('Thu Oct 17 2013 09:00:00 GMT+0530 (IST)'),allDay: false},
      {id: 999,title: 'Repeating Event',start: new Date(y, m, d + 4, 16, 0),allDay: false},
      {title: 'Birthday Party',start: new Date(y, m, d + 1, 19, 0),end: new Date(y, m, d + 1, 22, 30),allDay: false},
      {title: 'Click for Google',start: new Date(y, m, 28),end: new Date(y, m, 29),url: 'http://google.com/'}
    ];
    //we don't need it right now*/


    //with this you can handle the events that generated by clicking the day(empty spot) in the calendar
    $scope.dayClick = function( date, allDay, jsEvent, view ){
        $scope.$apply(function(){
          $scope.alertMessage = ('Day Clicked ' + date);
        });
    };


    //with this you can handle the events that generated by droping any event to different position in the calendar
     $scope.alertOnDrop = function(event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view){
        $scope.$apply(function(){
          $scope.alertMessage = ('Event Droped to make dayDelta ' + dayDelta);
        });
    };


    //with this you can handle the events that generated by resizing any event to different position in the calendar
    $scope.alertOnResize = function(event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view ){
        $scope.$apply(function(){
          $scope.alertMessage = ('Event Resized to make dayDelta ' + minuteDelta);
        });
    };

    /*
    //this code will add new event and remove the event present on index
    //you can call it explicitly in any method
      $scope.events.push({
        title: 'New Task',
        start: new Date(y, m, 28),
        end: new Date(y, m, 29),
        className: ['newtask']
      });

    $scope.events.splice(index,1);*/


    //with this you can handle the click on the events
    $scope.eventClick = function(event){           
        $scope.$apply(function(){
          $scope.alertMessage = (event.title + ' is clicked');
        });
    };


    //with this you can handle the events that generated by each page render process
    $scope.renderView = function(view){    
        var date = new Date(view.calendar.getDate());
        $scope.currentDate = date.toDateString();
        $scope.$apply(function(){
          $scope.alertMessage = ('Page render with date '+ $scope.currentDate);
        });
    };


    //with this you can handle the events that generated when we change the view i.e. Month, Week and Day
    $scope.changeView = function(view,calendar) {
        currentView = view;
        calendar.fullCalendar('changeView',view);
        $scope.$apply(function(){
          $scope.alertMessage = ('You are looking at '+ currentView);
        });
    };


    /* config object */
    $scope.uiConfig = {
      calendar:{
        height: 450,
        editable: true,
        header:{
          left: 'title',
          center: '',
          right: 'today prev,next'
        },
        dayClick: $scope.dayClick,
        eventDrop: $scope.alertOnDrop,
        eventResize: $scope.alertOnResize,
        eventClick: $scope.eventClick,
        viewRender: $scope.renderView
      }    
    };

    /* event sources array*/
    $scope.eventSources = [$scope.events, $scope.eventSource, $scope.eventsF];
}

Now in your view file(jsp, gsp or html) add the following codes:- This have to be added before the head, AngularJS norms (for more detail go through AngularJS tutorial)

1
2
<html lang="en" ng-app="myCalendarApp" id="top">

This will give the basic calendar structure with 3 agenda buttons.

<div class="btn-toolbar">
    <div class="btn-group">
        <button class="btn btn-success" ng-click="changeView('agendaDay', myCalendar)">Day</button>
        <button class="btn btn-success" ng-click="changeView('agendaWeek', myCalendar)">Week</button>
        <button class="btn btn-success" ng-click="changeView('month', myCalendar)">Month</button>
    </div>
</div>
<div class="calendar" ng-model="eventSources" calendar="myCalendar" config="uiConfig.calendar" ui-calendar="uiConfig.calendar"></div>

This is where the alert message will be shown

<div class="alert-success calAlert" ng-show="alertMessage != undefined && alertMessage != ''">
    <h4>{{alertMessage}}</h4>
</div>

This will give the list of task for the current date

<ul class="unstyled">
    <li ng-repeat="e in events | filter:currentDate">
        <div class="alert alert-info">
            <a class="close" ng-click="remove($index)"><i class="icon-remove"></i></a>
            <b> {{e.title}}</b> - {{e.start | date:"MMM dd"}}
        </div>
    </li>
</ul>