FullCalendar - selectOverlap prevents creation of allday events - fullcalendar

Because selectOverlap function only passes through the event being overlapped, and not the selection, it is difficult to customise how to handle event creation.
In my case, we are working on a calendar/diary system, with background events showing the employee's shifts, and events showing their individual bookings.
At this point, other than the background events - absolutely no events should be able to overlap each other.
However... on top of that - we are then overlaying 'all day events' - which might be any number of things, but for examples sake, let's say they're 'staff birthdays' - therefore, you might have a couple of events today, but there will be an event in the all day section, showing someone's birthday.
I am checking for eventOverlap and doing some other checks on eventDrop and eventResize which handle different clashes, but these only work for existing events being moved or resized. I would like to do the same on event creation - which happens during the select. In order to disallow the select of spaces which already have events, I am using the example function from the selectOverlap documentation:
function(event) {
return event.rendering === 'background';
}
This works fantastically. However, if I try to create a new All Day event, it will 'overlap' any other events that exist on that day, and not pass this check.
I was hoping to be able to use the selection's object to check it for an allDay=true, but the function is only passed the existing event, and there is no way to check the selected section.
You can see a very simplified demo here:
https://codepen.io/anon/pen/NQrxOO
Try to create an allday event on the day which already has events.
Is there a better way to do this? I can completely remove the selectOverlap and do everything in the select callback instead, but I would need to essentially duplicate the overlap checks just to make this work, and I feel like that seems like overkill for something that should be relatively simple.
Is it possible to get not only the overlapped event object, but also the selection object when doing a selectOverlap function?

Current workaround is to remove the selectOverlap check, and instead do it myself within the select callback.
Below is a simplified version of a quick function I wrote to call when using select={this.handleEventCreate}:
class Diary extends React.Component {
//additional functions, state definitions, etc etc etc here.
//Define calendarRef as it will be needed in the function below
calendarRef = React.createRef();
handleEventCreate = (info) => {
// Get the Calendar API to allow unselection
let calendarApi = this.calendarRef.current.getApi();
// Get all Events
let checkEvents = calendarApi.getEvents();
// Loop through them
checkEvents.forEach(function(event){
// If it's not a background element, check whether the new element contains the existing, or vice versa.
if(event.rendering !== "inverse-background" &&
(
(event.start >= info.start && event.start <= info.end) ||
(event.end >= info.start && event.end <= info.end) ||
(info.start >= event.start && info.start <= event.end) ||
(info.end >= event.start && info.end <= event.end)
)
){
// It is an overlapping event, so we reject it.
return calendarApi.unselect();
}
});
alert('All good here to create the event.');
//extra event creation code to fire here.
}

Related

Fullcalendar permit selectOverlap for one day

I'm using Fullcalendar and I need to allow event overlap for one day: the last still event day can overlap on the first new event day.
For resizing and drop I resolved in this way:
eventOverlap: function(stillEvent, movingEvent) {
var a = movingEvent.start.startOf('day');
var b = stillEvent.end.startOf('day');
if(a.unix() == b.unix()){
return true;
}
return false;
}
Now I need to do the same on selecting, according to documentation, I have to use selectOverlap option by define a function, but differently of eventOverlap, this function not have an argument that include the current selection period, then I don't know how to make the check.
Somebody can say me how to do so?
UPDATE 19/05/2017:
I opened an issue on github's project repository here

Re-create template while switching routes

How can we re-create template while switching routes?
For example, i have subscriber template. It detects when user scrolls down to a display and subscribes to more data. It takes several parameters.
Example:
amazing_page.html
{{#each}}
{{amazing_topic}}
{{/each}}
{{>subscriber name='topics' count=5}}
subscriber.js
//rough sample code
Template.subscriber.onCreated(function() {
var self = this;
var type = Template.currentData().name;
var count = Template.currentData().count;
var user = Template.currentData().user;
var skipCount = 0;
self.autorun(function(c){
self.subscribe(type, skipCount, user);
var block = true;
$(window).scroll(function() {
if (($(window).scrollTop() + $(window).height()) >= ($(document).height()) && block) {
block = false;
skipCount = skipCount + count;
console.log(type);
console.log(skipCount);
self.subscribe(type, skipCount, user, {
onReady: function() {
block = true;
},
onStop: function() {
console.log('stopped');
}
});
}
});
})
});
I use this template with different parameters in different routes.
The problem is if user switches some routes, and scrolls down in one page, all subscribers he gets in another pages will actualy work in this page. More, they will store increased values for them variables, and will do all included logic.
I found a bad decision when we use Route.getName (for example) comparing and name parameter of subscriber. It is not a best option. Can someone help me to find a good practice for that?:)
Simple Example:
We have 3 different routes:
1)News
2)Videos
3)Topics
These routes templates have included special subscriber-templates. And subscribtion works fine on scroll.
Ok, now let's visit all of them: News, Videos, Topics.
Good, now scroll down and... I have three instance of subscriber template what will subscribe on them own publications, because they not destroyed when we switch routes.
And, as a result - when user scrolling Topics page, he will call subscribtion for News and Videos too, and he will take data from these collections too;)
And - this is a problem:)
UPD:
Looks like we find a decision. If i use Template.instance (autorun/subscribe) it will start working expected, except some strange cases:)
First of all, when i go in another route in next iteration (scroll down) it returns me data from old, destroyed template + error. Next time (next iteration) it will start to subscribe to a correct data. Hmm...it looks like i have mistake in autorun section...or not?
Attached print screen from console
this
It sounds like you have multiple subscriptions to the same collection and that therefore the list of documents shown in various contexts can change in unexpected ways. Meteor manages multiple subscriptions on the same collection by synchronizing the union of the selected documents.
The simplest way to manage each of your views is to make sure that the data context for a particular view uses a .find() with the query you need. This will typically be the same query that your publication is using.
A different but less efficient approach is to .stop() the subscription when you leave a view.

Invoke value event handler only on value change in Firebase

Is there a way to invoke the value event handler only on value change in Firebase? If I just add the value event handler without any special code, it will be called once on reading the initial value and then on any value change. I want to avoid invoking the event handler on reading the initial value.
One of the use cases where I need this functionality is that I am working on an app where user can add tasks for some other users. The other users can either accept or reject a task. There is a server which is monitoring these tasks on Firebase and it sends a push notification to the creator of the task whenever a user accepts or rejects a task i.e. whenever there is a change in the status of task.
I have tried two solutions.
Solution 1:
Have a map and add an entry to the map when the value event handler is called if it is not already present and if the the entry is present, then do the work required on value change. This solution works but I need this in several different cases and I have to create this map in all such cases and I didn't find it to be a convenient solution.
Solution 2:
Add the value event handler like this
ref.on('value', (snapshot) => {
console.log('Initial call ', this.initialCall);
if(!this.initialCall) {
// Do work
} else {
this.initialCall = false;
}
}, {initialCall: true}) // {initialCall: true} is the context which is provided as this in the event handler
Here my thinking was that I can check if the event handler is called for the initial value by checking if this.initialCall is set to true. If it is, then set the this.initialCall to false and then in the subsequent invocations, this.initialCall should be false and I can do the work required for a value change. Using this solution what I observed was that the this.initialCall was undefined the first time the handler is called and then it was set to true and then it was true for all value change event handler invocations for all the tasks, not just the one for which it was set to true.
With regards to solution 2, I am not a JS expert and it is possible that due to some gap in my JS knowledge, I am doing something wrong due to which it doesn't work as I expected.
Please let me know if you have an idea/solution which can be used to call the value event handler only on value change.
A value event in Firebase is invoked immediately with the current value and then whenever the value changes. If you only care abut when the value changes, you can simply ignore the initial event:
var isInitialValue = true;
ref.on('value', function(snapshot) {
if (isInitialValue) {
isInitialValue = false;
}
else {
// TODO: handle subsequent changes
}
});

temporarily block interaction event handling in vis.js network

Is there a way to temporarily (and programmatically) block interaction event handling in a vis.js network? (that is, clicks, roll overs, etc.). I would like to do this to avoid nasty interaction with other code that implements a 'time lapse' feature (that is, it essentially displays different parts of the network at consecutive, set intervals). In other words, I'd like to know whether I can prevent vis.js from drawing in response to events not originated by my script, while the script is executing. I noticed there's a 'clickToUse' feature, but it would not work well in this case because I need the script itself to determine when to stop blocking the event handling.
Any suggestions will be appreciated. Thanks!
You need manipulate interaction options. For example:
// Save interaction options to saveOpt variable
//
var saveOpt = {
interaction: JSON.parse( JSON.stringify( network.interactionHandler.options ) )
};
// Set all interaction options to false:
//
var newOpt = { interaction: {} };
Object.keys( saveOpt.interaction ).forEach( function(k) {
// Except number
if (typeof saveOpt.interaction[k] !== 'number') {
newOpt.interaction[k] = false;
}
});
// Prevent any interactions
//
network.setOptions( newOpt );
// Allow interactions (restore saved)
//
network.setOptions( saveOpt );

Drupal hook to change date CCK field value

I need to send out a reminder email the DAY BEFORE a Calendar Event as well as the DAY AFTER. Unfortunately, I can't use Rules Scheduler, because the Tokens can't be manipulated with PHP. It doesn't work if I have
[node:field_event_date-datetime] -1 day
as the scheduled time.
What I've ended up doing is creating two "dummy" date fields for DAY BEFORE and DAY AFTER, and I'm trying to hook into the form, grabbing the event date, using some PHP like strtotime() to add/subtract a day, and make these the values that would go into the database.
I've tried linking to the #submit part of the form, but in phpMyAdmin, all values are NULL.
For this code i haven't even changed the date, I'm just trying to get values to appear in the database.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "event_node_form") {
$form['#submit'][] = '_mymodule_after_build';
// Makes the fields invisible
$form["field_event_day_before"]["#access"] = FALSE;
$form["field_event_day_after"]["#access"] = FALSE;
}
}
function _mymodule_after_build($form, &$form_state) {
$eventcopy = array();
// copy the value part from the Event
$eventcopy = $form['field_event_date'][0]['#value'];
// without doing any PHP yet, simply copy the values. Doesn't show up in db.
$form['field_event_day_before'][0]['#value'] = $eventcopy;
dsm($form);
return $form;
}
I've read the tutorials about using Rules Scheduler with CCK and
I'm also following Schedule email to go out based on CCK date field - not working for me
Am I using the right hooks? How do I intercept the inputted date value properly?
I don't think you are approaching your problem the correct way. If you want to try to go down the path you are proposing then you would want to look at hook_nodeapi(). You can add some code for the 'insert' and/or 'save' (or maybe even 'presave') operations so you can update your $node->field_event_day_before'][0]['#value'] and $node->field_event_day_after'][0]['#value'] fields based on the event_date value.
However, you really don't want to add extra fields for date before and date after when you can just calculate those from the event_date.
What I think the better solution is to just implement hook_cron() and have that function handle querying for all events in your database whose event day is TODAY() +1. For all those results, send out an email. Do another query that looks for any event whose event_date is TODAY() - 1 and send out an email for those results. You'll want to make sure you only run this process once in every 24 hour period.
I want to share the answer, thanks to help from the community. If you run into this same problem, try this:
function mymodule_form_event_node_form_alter(&$form, &$form_state) {
// hide these dummy fields, will fill in programatically
$form["field_event_day_before"]["#access"] = FALSE;
$form["field_event_day_after"]["#access"] = FALSE;
}
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
switch ($op) {
//if the node is inserted in the database
case 'insert':
if($node->type == 'event') {
// Day before (+10 hours because I'm in Hawai`i, far from GMT)
$daybefore = strtotime('-1 day +10 hours', strtotime($node->field_event_date[0]['value']));
$daybefore = date('Y-m-j\TH:i:s', $daybefore);
$node->field_event_day_before[0]['value'] = $daybefore;
// Day after (+10 hours because I'm in Hawai`i)
$dayafter = strtotime('+1 day +10 hours', strtotime($node->field_event_date[0]['value']));
$dayafter = date('Y-m-j\TH:i:s', $dayafter);
$node->field_event_day_after[0]['value'] = $dayafter;
}
}
}
The rules scheduler can then take tokens from the day_before/day_after fields, and you can use their interface for scheduling.
You can do this by using the rules module, i did this in my one project, basically you have to create two rules, one for one day before, and another for one day after. Let me know if you want any clarification.
Thanks
K

Resources