PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/app/helpers/widgets_helper.rb

https://bitbucket.org/kapilnakhwa/demo-teachme
Ruby | 112 lines | 91 code | 17 blank | 4 comment | 9 complexity | f6d451f47421c018c21cf06c000d6d6c MD5 | raw file
  1. module WidgetsHelper
  2. def widget_category_selector(w,g=nil)
  3. return if(w.nil?);
  4. q,cats=
  5. if(g.nil?) then
  6. ['',w.public_categories.split('|')]
  7. else
  8. [(w.url.index('?').nil? ? '?' : '&')+'group='+g.lookup_key,g.categories.split('|')]
  9. end
  10. t_rand=w.url.gsub('::cat::',cats.sample)+q
  11. o='<option value="-0-">CHOOSE A CATEGORY</option>'
  12. cats.each do |cat|
  13. o+=%{<option value="#{cat}">#{cat}</option>}
  14. end
  15. %{
  16. <select onchange="if(this.value!='-0-'){window.location=#{w.url.to_json.gsub('"',"'")}.replace('::cat::',this.value)+'#{q}';}">
  17. #{o}
  18. </select>
  19. <table >
  20. <tr>
  21. <td valign="top" >or</td>
  22. <td style="font-size:smaller">
  23. <a href="#{w.list_url}">Visit #{w.type.index('Pdf').nil? ? "widget's" : "the"} index page</a><br/>
  24. <a href="#{t_rand}">Go to random category</a><br/>
  25. <a href="#{w.class.list_all_page}#top_activities">See all #{w.type.index('Pdf').nil? ? "activities" : "worksheets"}</a><br/>
  26. </td>
  27. </tr>
  28. </table>
  29. }.html_safe
  30. end
  31. #Create a list page to make search engine landing page
  32. def widget_list_page(w,all=false)
  33. return if (w.nil?)
  34. cats=
  35. if(all) then
  36. #TODO: Do all condition
  37. else
  38. w.public_categories.split('|')
  39. end
  40. s=w.description.split("\n")
  41. d1=s[0]
  42. d2=s[1,999].map{|t| t.blank? ? nil : t}.compact.join("<br/>")
  43. str='<p>'+d1+'<br/>'
  44. str+='<div style="float:right;padding:8px;text-align:center"><img src="http://static.123teachme.com'+w.screenshot+'" alt="'+w.name+'"/>'
  45. str+=%Q{<br /><br />Want more games? Visit our <a href="/learn_spanish/fun_and_games">Spanish games</a> page</div>}
  46. str+=d2 if(!d2.blank?)
  47. str+="</p>"
  48. str+="<ul>"+(cats.sort.map do |cat|
  49. '<li><a href="'+w.url.gsub('::cat::',cat)+'">'+cat.capitalize+'</a></li>'
  50. end.join)+"</ul><br/>"
  51. str.html_safe
  52. end
  53. #Show code to load widget in an iframe
  54. def widget_show_iframe_code(w,cat=nil)
  55. return if (w.nil? || w.iframe_code.nil?)
  56. s=cat.nil? ? w.iframe_code : w.iframe_code.gsub("::cat::",h(cat.gsub(/[^a-zA-Z0-9_]/,'_'))) #do minimal sanitizing
  57. zid=rand(999).to_s
  58. %Q{
  59. <center>
  60. <table class="show-iframe-code" ><tr>
  61. <td class="iframe-embed-label" >Embedding Code:<a href="#" onclick="z_about();return false;">(?)</a></td>
  62. <td class="iframe-embed-code"><input value='#{s}'></td>
  63. <td class="iframe-embed-button"><div id="zOuter_#{zid}" ><input type="button" id="zInner_#{zid}" value="Copy to Clipboard" onclick="alert('please wait for page to load more')"/></div></td>
  64. </tr></table>
  65. </center>
  66. <script>
  67. if (!zLoaded) {
  68. var zLoaded=true;
  69. var zClients=[];
  70. document.write(
  71. '<'+'link href="/stylesheets/iframe.css" media="all" rel="Stylesheet" type="text/css" />'
  72. );
  73. document.write('<scr'+'ipt type="text/javascript" src="/javascripts/ZeroClipboard.js"></scr'+'ipt>');
  74. function z_complete( client, text ) {
  75. alert("Copied text to clipboard: " + text );
  76. }
  77. function z_about() {
  78. alert('If you have a blog or a webpage, this is the code to insert so as to display this widget on your own site');
  79. }
  80. $(function(){
  81. ZeroClipboard.setMoviePath( '/javascripts/ZeroClipboard.swf' );
  82. });
  83. }
  84. $(function(){
  85. var zClient=new ZeroClipboard.Client();
  86. zClient.setText(#{s.to_json});
  87. zClient.setHandCursor( true );
  88. zClient.addEventListener( 'onComplete', z_complete );
  89. zClient.glue( 'zInner_#{zid}', 'zOuter_#{zid}' );
  90. zClients.push(zClient);
  91. });
  92. </script>
  93. }.html_safe
  94. end
  95. end