Making JavaScript code available to given widget

    • 75 posts
    April 18, 2018 3:35 PM EDT

    Hi all,

    I know javascript is supposed to go inside the externals folder of the module.

    But what is its actual scope / visibility inside the system / site?

    Let me elaborate:

    - we have JS script called MyScript

    - we have a widget called MyWidg that is part of a module of our creation MyMod

    - the widget MyWidg needs the script MyScript, but MyScript is to big to just hardcode it inside the view of MyWidg: it would be better and more elegant to source it

    - MyWidg can go on any page of the site, not just pages / routes created by its module MyMod, but also other pages, core pages, as the webmaster's leisure

    - so how can the widget access that script and its functions regardless of what pages the widget has been put on?

     

    Is that automatic? If I put the script in externals in my module, will all pages have access to it? Or not? Is there some... code? header? something that i need to add to my widget?

    Please advise?


    This post was edited by David at April 18, 2018 3:36 PM EDT
    • Moderator
    • 6923 posts
    April 18, 2018 5:00 PM EDT

    Bit late for me atm but have you checked my free Happy Holidays plugin and the other freebies? They show examples of how to add the files that your plugin needs, in the manifest.php file. The ones I made with the language files might be of interest as those show how to grab the external files in other folders. I know, bad explanation but I'm a bit too tired to respond all that well atm. Hope this helps some.

    Also check the free Hello World example plugin in the developer docs as it has good examples. 

    • 75 posts
    April 18, 2018 5:33 PM EDT

    I remember you gave me a list of your plugins and widgets but I can't find it :-(

    Is there anywhere a list of one's own past threads and posts???

    We have been writing on and off quite a bit and I'm starting to loose track of teh older stuff (including myself)

    • Moderator
    • 6923 posts
    April 19, 2018 5:06 AM EDT

    On your profile there's a tab for posts. My developer name is ScriptTechs. It should be on my profile but here's a link, https://www.socialengine.com/experts/profile/scripttechs

    • 75 posts
    April 20, 2018 4:16 PM EDT

    if I understand that correctly you use the ->headScript()->appendFile() method of the views and simply use the the path from layout()->staticBaseUrl as a parameter.

    Correct?

    Which answers another question I had: those externals are NOT being loaded automatically - they have to be explicitly loaded. Correct?

    Two more questions though:

    i always found the ->headScript inside either actual page views or partials - not really inside a widget view... but I assume a view is a view is a view :-)  - or not?

    I also noticed a very interesting "requirements" configuration inside the content.php description of the widgets. I assume that: 

    'subject' => 'user' requires a registered user to be logged in and 'no-subjec' must mean that the user must be a non logged-in guest... but what about 'viewer' ???  

    And finally: is there a way for the controller of a widget to find out on what page the widget is being displayed?

    Thank you very much, Donna, for all the help you may give me - I know you are an expert on the widget's topic... hence my expectations are very high :-P

     

    • Moderator
    • 6923 posts
    April 21, 2018 5:26 AM EDT

    Which plugin are you looking at?

    I enjoy making the widgets - not an expert.Or if I am, Lucy was a chocolate expert:


    This post was edited by socialenginestaff at April 21, 2018 5:26 AM EDT
    • 75 posts
    April 21, 2018 9:14 AM EDT

    I looked at your first 2-3 lsited widgets as well as at a number of other widgets included in the core modules

    • Moderator
    • 6923 posts
    April 23, 2018 4:43 AM EDT

    Ok I'll  have to make some time to look at them again. I do believe the externals need to be loaded because clients "disable" plugins and so we wouldn't want to just load everything from every plugin automatically as it used to do. If I am understanding the first part of your question. It's early and so I will come back to look at this again later.

    • Moderator
    • 6923 posts
    April 23, 2018 10:09 AM EDT

    Not having the exact plugin and file you are looking at makes it a bit tough. 

    Looking at the User module in the profile-fields plugin, the controller.php we see:

     

    // Don't render this if not authorized $viewer = Engine_Api::_()->user()->getViewer(); if( !Engine_Api::_()->core()->hasSubject() ) { return $this->setNoRender(); } // Get subject and check auth $subject = Engine_Api::_()->core()->getSubject('user'); if( !$subject->authorization()->isAllowed($viewer, 'view') ) { return $this->setNoRender(); }

     

    Which tells us some of what I think you are asking but again, I don't know for sure. 

    • 75 posts
    May 28, 2018 10:16 AM EDT

    Hi Donna, I'm finally back!

    Some of my questions are answered but I'd appreciate some more insights regarding these two:

    i always found the ->headScript inside either actual page views or partials - not really inside a widget view... but I assume a view is a view is a view :-)  - or not?

    I also noticed a very interesting "requirements" configuration inside the content.php description of the widgets. I assume that: 

    'subject' => 'user' requires a registered user to be logged in and 'no-subjec' must mean that the user must be a non logged-in guest... but what about 'viewer' ???  

    And finally: is there a way for the controller of a widget to find out on what page the widget is being displayed? (<- THIS ONE IS VERY IMPORTANT)

    • 75 posts
    June 6, 2018 4:12 PM EDT

    Hey Donna,

    is there a way to insert a specific external file in a specific place of the view of a widget?

    I'm talking about some javascript. I know about this method:

    $headScript = new Zend_View_Helper_HeadScript(); $headScript->appendFile('application/modules/User/externals/scripts/core.js');

     

    But in this case the javascript should NOT be at the top of the page. it should come last.

    I'd rather not write the whole thing inside the widget view for obvious legibility reasons. And I definitely don't want to write anything like:

    <script src="mysite.com/path-to-my-script" ></script>

     

    because I don't want to have to mess with the .htaccess file unless absolutely necessary.

    I can think of a couple of solutions but they are so dirty I'm even ashamed of having thought of them !!!! :-D

    Can you suggest any "proper" way to achieve the goal?

    • Moderator
    • 6923 posts
    June 6, 2018 5:24 PM EDT

    I put mine in the tpl file so I'm not sure but will see if I can find anything in the code.

    • Moderator
    • 6923 posts
    June 6, 2018 5:34 PM EDT

    Looking at the user login block for fb js stuffs, it's in the midst of the tpl too. I'm not much help to you.

    • 75 posts
    June 11, 2018 12:12 PM EDT

    thanks any way - I resolved to simply include my js files via the standard include() php command - it works quite well, you may want to consider doing that.

    I noticed that when you append stuff in the header, the appendFile method adds a ?c=XX (where XX is a number) to the end of the URL (I eblieve that is a reference to the layout or something like that)

    I do not add that to my include path (since I do not know how to calculate that number) but it seems to work well regardless

     

    • Moderator
    • 6923 posts
    June 11, 2018 12:20 PM EDT

    Glad you worked it out. I hope it's all going well for you cool

    • 75 posts
    June 18, 2018 6:42 AM EDT

    hard at work on our widgets :-)