search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Copying and Pasting Multiple events accross resources

Asked by Michael Wolf
1 year ago.
//I managed to implement the copy multiple events at a go but still running into some errors in run time.
//1. I need to know how to clear selection after right clicking on paste. Otherwise even after pasting the selected //events for copying still remain highlighted and this courses undesirable operation for me.
//2 When I copy an event and paste it, then try to copy it again, I get weird new events that do not much the original //selection.
//could the problem lie with the fact that am not clearing the selection on the scheduler before I start copying new //multiple events.
//Heres my code, the control select part
    eventClickHandling: 'Enabled',
    onEventClick: async (args) => {
      if (args.ctrl) {
        args.control.multiselect.add(args.e);
        console.log("New args---", args.e.data);
        multiclipboard.push(args.e.data);
        console.log("multiclipboard", multiclipboard);
      }
      else {
        //do nohting
        return;
      }
    },
and heres the right click menu for copy multiple that has the clearSelection() function called
    contextMenu: new DayPilot.Menu({
      items: [
        {
          text: 'Delete',
          onClick: (args) => {
            const dp = args.source.calendar;
            dp.events.remove(args.source);
            //backend sync here
            console.log("Deleted", args.source.data);
            eventDelete(args.source.data);
          }
        },
        {
          text: "Copy",
          onClick: (args) => {
            clipboard = args.source.data;
            const dp = args.source.calendar;
            dp.clearSelection();
            console.log("Copicat", clipboard);
          }
        },
        {
          text: "Copy Multiple",
          onClick: (args) => {
            //clipboard = args.source.data;
            const dp = args.source.calendar;
            dp.clearSelection();
            console.log("Copicat", multiclipboard);
          }
        }
      ]
    }),
//And here's the part that does the actual pasting multiple events, its the last item in the contextMenuSelection attributes
 contextMenuSelection: new DayPilot.Menu({
      items: [
        {
          text: "Paste",
          onClick: (args) => {
            if (!clipboard) {
              alert("Nothing to paste, use Copy first");
              return;
            }
            const dp = args.source.calendar;
            console.log("logost", args.source);
            console.log("Paste", clipboard);
            var duration = new Date(clipboard.end).getTime() - new Date(clipboard.start).getTime();
            duration = duration / (3600 * 24000);
            function addDays(date, days) {
              var result = new Date(date);
              result.setDate(result.getDate() + days);
              return result;
            }
            console.log("duration", duration);
            const newEvent = {
              start: args.source.start,
              end: addDays(args.source.start, duration),
              id: DayPilot.guid(),
              barColor: clipboard.barColor,
              resource: args.source.resource,
              shift: clipboard.text,
              month: clipboard.month,
              text: clipboard.text,
              hours: clipboard.hours,
              days: clipboard.days,
            };
            console.log("newEvent", newEvent);
            dp.events.add(newEvent);
            redisSync(newEvent);
            // redisSync(newEvent);
          }
        },
        {
          text: "Paste Multiple",
          onClick: (args) => {
            if (!multiclipboard) {
              alert("Nothing to paste, use Copy first");
              return;
            }
       
            const dp = args.source.calendar;
            let currentStart = args.source.start;
       
            const copiedEventIds = new Set(); // Store IDs of copied events
       
            multiclipboard.forEach((item) => {
              if (!copiedEventIds.has(item.id)) {
                const duration = new Date(item.end).getTime() - new Date(item.start).getTime();
                const newEvent = {
                  start: currentStart,
                  end: new Date(currentStart.getTime() + duration),
                  id: DayPilot.guid(),
                  barColor: item.barColor,
                  resource: args.source.resource,
                  shift: item.text,
                  month: item.month,
                  text: item.text,
                  hours: item.hours,
                  days: item.days,
                };
       
                currentStart = new Date(newEvent.end.getTime()); // Set the start date for the next event
                dp.events.add(newEvent);
                redisSync(newEvent);
                copiedEventIds.add(item.id); // Add the copied event ID to the set to prevent duplicates
                // dp.clearSelection();
              }
            });
       
            multiclipboard = []; // Clear clipboard after pasting
           
          }
        }
      ]
    }),
This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.