Javascript library include

Permalink 1 user found helpful
I want to use the Hover Intent jquery plugin for several blocks. I could add this to the folder of every block that needs it, but I'm planning on using it a lot. How do I make this a Concrete5 library or something similar so it is accessible by all? Or should I just treat each block like its own independent unit and include it with each one?

 
mdzoidberg replied on at Permalink Reply
mdzoidberg
You can make it part of your theme, or you can also put it on the root js folder.
TheRealSean replied on at Permalink Reply
TheRealSean
I have mine in my theme header

I placed the hoverIntent in my themes folder and into a Js folder within that.
<script src="<?php echo $this->getThemePath()?>/js/jquery.hoverIntent.min.js"></script>


Remember JQuery is included already so just call in the hoverIntent don't recall jquery otherwise things will break.
orourkedd replied on at Permalink Reply
Thanks for the replies!

Doesn't this then make the block dependent on the theme? It seems like there's not a strong enough separation between the block and the theme. If I change themes then my block breaks. I'm coming from Drupal and trying to find out the best practices for C5.
TheRealSean replied on at Permalink Reply
TheRealSean
yes it would, if you want it to be accessible throughout.

From you controller you can add,
$this->addHeaderItem($html->javascript('location.js'));

which requires the JS to be in the root/js

or within a Package
$this->addHeaderItem($html->javascript('location.js'), 'pkghandle');

If your in a block, add a JS folder then add the js in there and it will auto include.

--A condensed version from here
http://www.concrete5.org/community/forums/customizing_c5/adding-jav...
TheRealSean replied on at Permalink Reply
TheRealSean
You could maybe check if the javascript has been included.

In concrete/libaries/view.php

There is the function, getHeaderItems, you might be able to grab that loop over the result checking if the HoverIntent is found within there if not then add the js?
orourkedd replied on at Permalink Reply
I like that method. Thanks!