Building a plugin engine
You might ask yourself why you would want to have a plugin engine. The whole concept is derived out of object-oriented programming. The idea behind it is that certain sections of your code are so specific to the action that you're doing that you don't want to modify your object. A plugin architecture enables you to on-the-fly add features or make changes to the way your object behaves. Although our main topic is not object oriented programming, this will be really helpful for us throughout the next few chapters. If you want to learn more about object oriented programming, please check one of our titles.
Video Transcript
00:00So, this is already lecture 91 in the group of lectures about animation and JavaScript.
00:05I've been doing a lot of recordings, hours, I don't even remember how many hours of recording
00:10this has been.
00:11But this is my favorite so far, literally, because we're going to be talking about a
00:16concept that is so complicated.
00:19I love complicated concepts.
00:21And it's actually coming from the world of object-oriented programming.
00:24And we're going to see something really cool by how do you add optional things you could
00:31add to an object, optional controls.
00:33And we call that plugins.
00:35And really also when you're looking at what the tween animation library itself is doing,
00:40it's actually using plugins.
00:41So in many ways, we're not going to talk about the jargon and all the complicated object-oriented
00:47world lingo, because we will talk about that in a course about object-oriented programming
00:52if you choose to take one of our object-oriented courses.
00:54But we're going to see it in action, and we're going to look at it from a functional level
00:58and see why and how this is really useful, because it's going to help us throughout this
01:02chapter and even the next one, we're going to use this plugin and we'll be able to
01:06enhance our slide without messing our slide at all if we want to use it in other places
01:12as well.
01:13So let's see what I mean by that.
01:14This is a really complicated subject, love that.
01:18And if you feel it's a little bit too complicated, ask away.
01:22Ask me questions and please watch this twice if you feel a little bit confused at the end
01:26because it is a complicated topic, all right?
01:29So this is really complicated and it's advanced and we're not really adding any tween animations
01:35right now.
01:36It's all behind the scenes.
01:38What I really want to do is I want to add a logic, because right now when we have our
01:41text animating, it's animating immediately, which is wonderful.
01:46But I would love for it to be able to animate again once I come back to it.
01:51Beyond that, I want to be able to reset it.
01:53And that's going to be the logic that we're going to be building behind the scenes right
01:57now.
01:58And it's going to enable us to create different animations that would happen as we go into
02:02different sections of our slideshow, making the slideshow incredibly robust.
02:07All right, so to be able to do that, what I'm going to do next is what I want to do
02:12next is I'm going to take my learning JavaScript.
02:14Well, what am I doing?
02:15Let's just get started.
02:17Let's start doing it.
02:18So the first thing that really what I want to do is I want to go into my OG slideshow.
02:23And what I want to do is I want to create a new method here.
02:26And this method is going to be, I'm going to call this add triggers.
02:30Now add triggers are going to have one parameter, which is going to be the index of our trigger
02:35based on the slideshow that we're in.
02:37The next item it's going to have, oh, let me just cut right here, equals function.
02:41And by the way, why am I not just baking these animations inside of our slideshow?
02:47I don't want to bake the animations into the slideshow because then the slideshow would not
02:51be reusable if I wanted to use it in other sections of my site.
02:55So I want to take that extra logic out of my function, build functions externally and
03:01basically send them into my slideshow and tell them to trigger it in specific points
03:05in time.
03:07So the properties that I really want to define is maybe on enter, on exit.
03:19So basically the index that once we're entering it, once we're exiting it, do we want to do
03:24specific things once we exit it or once we enter it?
03:29Next item that I want to do is I'm going to put there a comma there.
03:33Let's just start building out.
03:34We'll see if we need any other information.
03:36So I'm going to go ahead here and what I'm going to do is I'm just going to create another
03:39variable here and I'm going to call this callbacks and I'm going to define that as
03:47an array and I'm going to go ahead and continue by setting this callbacks and just setting
03:54my index.
03:55Notice how I could put items that doesn't have to be in the sequential order.
03:58Whatever item that will come in, I'll just position it in that position and I'm just
04:02going to go ahead here and just create here an object and I'm going to call it on enter.
04:06Force property and send in that on enter function and I'm going to do that for on exit
04:10as well and I'm going to send on my on exit, what did I do here?
04:19On enter, on enter, on exit, on exit, we wanted to do that on exit, oh on enter, that's
04:26why it looked weird.
04:27All right, here we go.
04:28So all we're really doing here is we're storing the information.
04:33The next step for us is we want to know where do we actually trigger it.
04:36We'll probably trigger it in our highlight button.
04:39What we want to do is we're going to basically ask here if, and by the way, we're going
04:44to have to figure out a solution for our on exit, but we're going to do that in a second.
04:48We'll do here if, if, if, if, if this callback has that specific index and only in that
04:56case what we're going to do is we're going to actually call that callback index dot
05:00on enter and we're going to trigger it.
05:03I'm going to go ahead also and send into it the index itself so that callback will
05:07know what item is being animated right now.
05:11Now that's it, so literally that's all I have to do from the logic inside of the hub of
05:15my application.
05:16I'm just going to repeat it one more time because we did something relatively complex
05:19which is called a plugin architecture and what we basically did is we created an on
05:23triggers function and send, and we're going to send into a different functions that will
05:27trigger in specific points in time.
05:30Right now we configured already the on enter, and what, what that means is if we
05:34set in this items index, it's going to basically call our on enter function that we
05:39send into it whenever our first index is, or that specific index is being called.
05:44All right, without further ado, let's actually build out that logic and to do
05:48that I'm going to go back all the way to the beginning where we have here this
05:51text animation.
05:53And what I want to do is I literally, what I want to do is I want to take this
05:56logic, and I'm just going to create a new function here, function, and I'm just
06:01going to call it on slide zero enter, and because I know which slide it is, I
06:12don't really care that it's going to be sending me in an index, they could send
06:15it an index, but if at any point, any time I need that index, I'll be able to
06:19refer to it, and literally I could just go ahead right here and slap that
06:23copy right inside here, and I'm already approaching that specific index when
06:27I'm doing it, so I'm basically set, I'm configured. Now I'm just trying to
06:33check to see if there's anything else I need to do, not really, all that's left
06:36for me to do is actually call that callback, call that logic of actually
06:40calling the set trigger, and I'm going to do that after I created that slide, I
06:44could delete this version, well let's leave it, why not. So I'm going to go
06:49into my slide, and I'm going to add another trigger, the first item is going
06:53to be the index is going to be index zero because that's our first element,
06:56next thing is what function do we want to call when it enters, and I'm going to
07:00send it a value, for now I'm going to send nothing for when it goes out just so we
07:04see that our construction actually works. Now if we run our application right now,
07:08and I click on refresh, we'll see that it's not going to do anything in the
07:13beginning, but next time that I roll over it, it's going to automatically start
07:16our animation. Now if I go somewhere else, come back in there, it's going to
07:21start over our animation again, but not really because we never reset it, so
07:26it's going to work in the same process as it did before, but what we really
07:30need to do is we need to basically have auto canceling of our animation. So to
07:36create a canceling of our animation, I'm just going to go ahead here, and I'm
07:39going to create something rudimentary that's not necessarily the most pretty
07:43thing, and it's definitely not going to be the most pretty thing, and I'm just
07:46going to create an on exit function as well really quickly, and I'm just
07:52going to hard code fix what it was, literally just going to our item zero
07:58here, and resetting its actual content. So I'm going to go here, I'm going to wrap
08:03that with the jQuery function again as well, I'm going to go into the text itself,
08:07I'm going to define that text, and it's a text as a function, and I'm going to
08:12define that text to that original text that was in our slide, which is learning
08:16better is better, I'm just going to copy it. Now this is going to be a slap, it's
08:20going to be a big, a big hump once it changes. This is going to be on exit,
08:26alright, and we're going to make sure that we send this to our function instead
08:33of sending nothing, and let's figure out where do we trigger that on slide zero
08:36exit. So I'm going to go right into our code here, and really we're in reality
08:41where it's going to happen is in our highlight buttons again, but because of
08:45the way we built our code earlier, and the way we built it incorrectly earlier,
08:49when we used our for loop, it's going to be really hard for us because what we'd
08:55have to do is call that on exit for every single item every time it exits,
08:59which is probably the worst thing for us to do because it might not be relevant,
09:05so we might be creating an animation three times when we don't need to, again
09:09making things more complex than they should be. So literally what I could do
09:13and let's just do it, it's time we do it, I could create this variable say
09:17current index, set that initially to be minus one or to zero because that's the
09:23first item that's being selected. Now that I have a current index, I could go
09:27ahead and literally once I'm done with my index, I could literally just go and
09:32tween that element out by animating it back to white, so literally, you know, let
09:41me just copy this, get rid of that, copy that, get rid of that, set my current, if
09:50my current, if I, we can even set it to minus one, that makes sense, that way I
09:53could say if my current index is not minus one, that means we already had
10:02something that was selected, if my current index is not minus one, then I'm
10:06going to go and change the current index, set it back to white, let me just
10:11put your word wrap for a second, all right, we set it back to white, this is not,
10:17this for loop is not needed anymore just by this basic logic and all that's left
10:23for me to do is basically set my current index right after that, after
10:27we're done with everything here, to set my current index to be that index. Now
10:32what I did by that is that my current index is actually, once we call this
10:35highlight, is actually the last index that was. So I literally, all I really
10:40need to do extra inside of here, is wrap this inside of a if statement, because I
10:45want to do two things, not only one thing, and I can literally just add to it
10:49to another extra logic, basically saying, well if the callback exists, if callbacks
10:55that current index exists, we're gonna go ahead and call the callback itself,
11:02but this time exit. Now I know I did a lot of stuff right now, so let me just
11:06walk through it one more time really quickly. So right, what I did is I got
11:10rid of that for loop, and by the way that was your homework in the last lesson, so
11:13hopefully you did it, and what I did is I set a current index so I could know what
11:18is the last thing that was selected. By default I'm setting it to be minus 1
11:23because nothing was selected, and then this line is exactly the same where
11:28we're just gonna animate the color of the current selected, the new selected
11:31item in. The next step is I ask myself, is the current index, the one that was
11:37before, different than minus 1? If it's different than minus 1, then I want to
11:41animate it back to its original color. Next I also want to check if there are
11:46any callbacks, and if there are callbacks I want to animate out that
11:50exit function by calling that on exit and sending the current index. Now by
11:55doing that I just set and configured my on slide 0 exit on slide enter when
12:01it's entering it's gonna animate in, and when it's exiting it's gonna exit out.
12:05Now there might be bugs here because if this is still animating and I delete it
12:10we're gonna create an issue. So if I save this, go to my application, and click
12:14on refresh, that's gonna be saying that we're gonna have to fix in our own
12:17element there, so if I roll over this item it's gonna start animating, and once
12:21I move out of there it's gonna type an error. Okay this is an
12:25error that we could fix easily, so let's fix this error first in line 67. So
12:29let's go to line 67 really quickly see what's going on there, and we can see
12:34that if this callback this current index exists then call on exit. Let's see why
12:41that's not working, because it should exist on exit. Oh the reason is because
12:48we're using index, right? We're asking if current index exists, and then
12:53we're working with that current index. So what I want to do is I want to send
12:57in that last index item to it, and if I save this again and go back into my
13:02application and click on refresh, that bug will go away, but we're gonna have
13:05another bug. So let me just run it. So this animation is working, but once I go
13:09to another element, and maybe I'll go back in there, it's did it stop? It sure
13:15did. So there's no bug and everything is working fine. jQuery, not jQuery, between
13:20max, between light was smart enough to automatically reset every time I roll
13:25over it, which is absolutely fantastic. It's even better than I expected it to
13:30work, because I thought that we're gonna have to actually stop our animation
13:34that was going on manually when this tween light was going on. If we delete
13:39the item itself suddenly, or change the item itself suddenly while it's in the
13:44middle of animating, I thought it would create some confusion to that animation
13:47library, but obviously it didn't, because it actually is working. But we
13:52can double check and see if it really is working the way we would expect it
13:55to work. And what I'm gonna do, I'm just gonna, what I want to do is I want to
13:57highlight that element, select it. Oh, let me just select that element. And I would
14:04just look at these spans, and I want to see if there's suddenly like this
14:06multitude of spans that shouldn't be there. So if I roll over and roll out,
14:11roll over, and roll out, just do it a little bit of times, a few times. I just
14:15want to see if the spans go crazy, and there's like an endless amount of
14:19spans. And I'm so impressed right now with tween light. I think this is an
14:24amazing plugin. This is something that a lot of programmers wouldn't even notice,
14:28that if you roll over something a lot of times, that would go crazy. Notice that
14:32our highlight right now is not working as we would want it to work. It's working
14:36when we're going to all these different elements. But once we go back to our
14:40element here, go out, go back in, it's rolling out, which means that we
14:44have your logic error. So let's fix that logic error before we wrap up
14:49this lesson. And if I go right in here to the, this error would have to happen
14:54in our highlight button, I want to check and see the problem that we have here
15:00is very simple. Right now, we are separating between the index and the
15:05current index. Now the problem is, is that our current index shouldn't be the same.
15:10So if I rolled over on one, right, which I just did, now I rolled over it
15:15again, my current index equals, in our case, it equals zero. Now if I roll
15:19over again on zero, so this goes through, but zero is different than minus
15:23one, so it's overriding our rollover. So for us to solve this problem, I'm going
15:27to have to add another item to my if statement saying, not only if it's minus
15:31one, but also if it equals the current index. So we don't want to go in if it's
15:36the current index, and we don't want to go in if it's minus one. That way, if I
15:41save this, we're not going to create this callback exit twice. So if I go
15:45ahead here and click on refresh, and go to my item and roll over it, and then I
15:51go over and roll over it again, and again, and again, and again, I'm just testing it
15:54out really, really thoroughly. And if I do this quite a few times, and I go ahead
15:58inside of here to see if my h2 got became crazy, and it surely did not, it's
16:04looking exactly the way we wanted it to look like, and our rollovers are
16:07working beautifully as well. And that's exactly the logic that we are going
16:12to do throughout the next few lessons as we explore other plugins. That way we
16:16could add different plugins to different sections and create specific
16:20animations for those specific elements. Now, we probably might want to have
16:25another, we might want to have another callback before we wrap this up. So I want
16:30to create an auto, I want to have also an on enter callback and not only an on exit,
16:35because maybe when we're entering something at a later day, we're going to
16:39want to also basically start off from a specific position and not start off from
16:43this specific position. But really what I'm thinking about this, that's the on
16:48enter, so we already have that. Alright, so we're set, we're good, we're ready. All
16:53that's left for us to do is to start doing things, and we're going to do that
16:56right away in the next lesson where we continue exploring different things that
17:01relate to our plugins in Gsap library.
