Code Explanation Request

Post any bugs, problems or feature requests here.
Mercury
Mercury
Brend
Brend

Code Explanation Request

Post Mercury » Sat Jan 12, 2013 1:58 pm
User avatar
Mercury
Storyteller
 
I believe we have code that explains how to use the icon rendering outside of the wiki / forum. I specifically am interested in rendering a combination of the specialised and resource icons for the corporate zones and I believe we have library code for this, but I'm unable to figure out how it works. I can write it on my own, but since we already have it, if you could explain what I have to import and what functions I have to call to render for example "200 (:crystals+corp)" into the proper 200 with specialised crystals icon, it'd be much easier ^_^
Post Brend » Sat Jan 12, 2013 4:56 pm
User avatar
Brend
 
The following goes into the header of your html:

Code: Select all
<script language="javascript" src="/fwurg-lib/fwurg.js"></script>
<script language="javascript" src="/fwurg-lib/fwurg.icons.js"></script>
<script type="application/javascript">fwurg.url=function(r){return "/fwurg-lib/"+r;};</script>


The first two lines import the two relevant source files, the third line ensures that the fwurg-lib libraries know how to build urls that point to the correct icons.

Now you can call fwurg.icons.draw("(:whatever)") and it will spit out the necessary HTML. Note that you can only feed it icons, any rubbish around the icons will choke the method. Therefore, the following snippet is very useful: fwurg.icons.render("This message is about 200 (:food)"). The fwurg.icons.render method will try to replace all substrings starting with (: and ending in ) by their icons; if an icon is unknown, the empty string will be used.

(The render method exploits a very nice feature in javascript: javascript's replace function accepts regular expressions as the search string, and a function as the replacement string.)
Post Mercury » Sat Jan 12, 2013 5:37 pm
User avatar
Mercury
Storyteller
 
Excellent, this works perfectly, thank you very much! I knew there was working code, I just wasn't able to figure it out, but now it works very well.

Am I correct to presume I need to do :dot1: replacements by hand? It is not an issue since there's nothing complicated about that, but if it is already featured, then doing so myself would be unnecessary.
Post Brend » Sat Jan 12, 2013 5:54 pm
User avatar
Brend
 
There is not :dot1: code as of yet. So go for it by hand for now. (I suggest looking at the fwurg.icons.render method for inspiration -- you basically want the same...)
Post Mercury » Sat Jan 12, 2013 5:56 pm
User avatar
Mercury
Storyteller
 
Note to future code editors. The following include is also recommended for the best results. It contains the CSS to properly render the icons.

Code: Select all
<link type="text/css" rel="stylesheet" href="/fwurg-lib/include.tax-css.php" />
Post Mercury » Sat Jan 12, 2013 5:57 pm
User avatar
Mercury
Storyteller
 
Thanks for the clarification. I used this:

Code: Select all
str = replaceAll(str, ":dot1:", "<big>&#9679;</big>");


The replace all function is handwritten to replace all entries. Its very convenient and probably someone else did it better but mine works on O(n) so for n < 100 it really doesn't matter.

Note: :dot1: is the only case that applies for me - all higher dots aren't relevant. If they are, its better to use a proper method, but I'm lazy atm.
Post Brend » Sat Jan 12, 2013 6:00 pm
User avatar
Brend
 
Nooooooooooooooooooooooooo! That's not how it works, you lazy peasant!

Mercury wrote:Note to future code editors. The following include is also recommended for the best results. It contains the CSS to properly render the icons.

Code: Select all
<link type="text/css" rel="stylesheet" href="/fwurg-lib/include.tax-css.php" />


That includes all css for the tax tool -- which might change without due notice.

You want to use this:
Code: Select all
<link type="text/css" rel="stylesheet" href="/fwurg-lib/fwurg.icons.css" />



Mercury wrote:Thanks for the clarification. I used this:

Code: Select all
str = replaceAll(str, ":dot1:", "<big>&#9679;</big>");


The replace all function is handwritten to replace all entries. Its very convenient and probably someone else did it better but mine works on O(n) so for n < 100 it really doesn't matter.

Note: :dot1: is the only case that applies for me - all higher dots aren't relevant. If they are, its better to use a proper method, but I'm lazy atm.


No custom code required: str = str.replace(/:dot1:/g, "<big>&#9679;</big>"); as long as you want to replace a constant... And this is MUCH more efficient than your find/replace loop.
Post Mercury » Sat Jan 12, 2013 6:05 pm
User avatar
Mercury
Storyteller
 
Fixed and fixed!

Return to Technical

cron