ka215 / jquery.timeline

You can easily create the horizontal timeline with two types by using this jQuery plugin.
MIT License
240 stars 43 forks source link

addEvent ID generation #13

Closed mattgallivan closed 4 years ago

mattgallivan commented 7 years ago

Thanks for the library!

I'm running into an issue using 'addEvent' with custom eventIds. It seems that the library generates its own IDs even if I do supply my own, which makes it difficult to use the relations to connect them.

The code in question

addEvent : function( events, callback ) {
      return this.each(function(){
        var $this       = $(this),
            data        = $this.data('timeline'),
            eventNodes  = ( new Function( 'return ' + data.timeline.text() ) )(),
            incrementId = 1,
            _ids        = [ incrementId ];
        // add events
        if ( events.length > 0 ) {
          $.each(eventNodes, function( i, evt ) {
            _ids.push( Number( evt.eventId ) );
          });
          incrementId = Math.max.apply( null, _ids ) + 1;
          $.each(events, function( i, evt ) {
            evt['eventId'] = incrementId;
            incrementId++;
            eventNodes.push(evt);
          });
          data.timeline.text( JSON.stringify( eventNodes ) );
        }

Note that it runs through all the event IDs, adding them to the _ids variable, and then it uses that list to generate an event ID that is one higher than the maximum.

So if I had two 'addEvents', with custom IDs 1 and 2 respectively, the system would really start the IDs at 3 and 4.

I think there should be some sort of check for a custom event ID before relying on system generation.

Thanks!

ka215 commented 7 years ago

Hi, I'm so sorry for not replying sooner.

Certainly, as you said, it seems necessary to modify the ID generation process when adding an event. I will investigate and fix as soon as possible, so please be patient.

mattgallivan commented 7 years ago

Thanks!

gaelcost commented 4 years ago

Dear ka215, great work! By cons I encounter the same problem I need to be able to inject my own ID or have the possibility of injecting an onclick = myFunction () without that I will not be able to go further. thanks in advance

ka215 commented 4 years ago

Fixed this issue in version 2.1.0.

Since ver.2.1.0, the events with duplicate IDs cannot be added and an error will occur. And if you specify a unique ID, it will be added with that event ID. An ID is automatically generated as in the past when an event with an unspecified ID is added.