PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/documentation_controller.js

https://github.com/moschel/jmvcdoc
JavaScript | 317 lines | 248 code | 24 blank | 45 comment | 43 complexity | e336d79e223ebf1248d4d4cdb16ba25b MD5 | raw file
  1. /**
  2. * @tag home
  3. *
  4. * Runs the documentation
  5. */
  6. jQuery.Controller.extend('DocumentationController',
  7. /* @Static */
  8. {
  9. onDocument: true
  10. },
  11. /* @Prototype */
  12. {
  13. /**
  14. * Keeps track of who is selected
  15. */
  16. init : function(){
  17. this.selected = [];
  18. },
  19. /**
  20. * Searches with the current value in #search or searches for 'home'
  21. */
  22. searchCurrent : function(){
  23. this.search( $('#search').val() || "" );
  24. },
  25. /**
  26. * Searches for a value and puts results on the left hand side
  27. * @param {Object} val
  28. */
  29. search : function(val){
  30. var list = Search.find(val);
  31. this.selected = [];
  32. $("#left").html("//jmvcdoc/views/results.ejs",
  33. {list: list, selected: this.selected, hide: false},
  34. DocumentationHelpers)
  35. },
  36. showDoc : function(docData){
  37. $("#doc").html("//jmvcdoc/views/"+docData.shortName.toLowerCase()+".ejs",
  38. docData,
  39. DocumentationHelpers
  40. ).find("h1.addFavorite").
  41. append('&nbsp;<span class="favorite favorite'+ (docData.isFavorite? 'on' : 'off')+'">&nbsp;&nbsp;&nbsp;</span>') ;
  42. //setTimeout(function(){
  43. $("#doc_container").scrollTop(0)
  44. //},100);
  45. $("#doc code").highlight();
  46. //check for api
  47. if($("#api").length){
  48. var names = [];
  49. for(var name in Search._data.list){
  50. names.push(name)
  51. }
  52. $("#api").html(
  53. DocumentationHelpers.link("["+names.sort(Search.sortJustStrings).join("]<br/>[")+"]" , true )
  54. )
  55. }
  56. // cleanup iframe menu when navigating to another page
  57. if($(".iframe_menu_wrapper").length) $(".iframe_menu_wrapper").remove();
  58. // hookup iframe ui
  59. var $iframe_wrapper = $(".iframe_wrapper");
  60. if ($iframe_wrapper.length) $iframe_wrapper.iframe();
  61. // hookup demo ui
  62. var $demo_wrapper = $(".demo_wrapper");
  63. if ($demo_wrapper.length) $demo_wrapper.demo();
  64. // add disqus comments
  65. $("#disqus_thread").children().remove();
  66. if(docData.name != "index" && typeof(COMMENTS_LOCATION) != "undefined" && $("#disqus_thread").length) {
  67. window.disqus_title = docData.name;
  68. // can't use subdomains or hashes
  69. var subdomain = location.href.match(/\/\/(.*\.)\w*\.\w*\//),
  70. disqus_url = location.href
  71. if(subdomain){
  72. disqus_url = location.href.replace(subdomain[1], "");
  73. }
  74. window.disqus_url = disqus_url.replace(/\#/, '');
  75. window.disqus_identifier = window.disqus_url;
  76. steal.insertHead(COMMENTS_LOCATION);
  77. }
  78. },
  79. showResultsAndDoc : function(searchResultsData, docData){
  80. $("#left").html("//jmvcdoc/views/results.ejs",
  81. searchResultsData,
  82. DocumentationHelpers)
  83. $("#results").slideDown("fast",function(){$('#results a:first')[0].focus()});
  84. this.showDoc(docData)
  85. },
  86. show : function(who, data){
  87. this.who = {name: data.name, shortName: data.shortName, tag: data.name};
  88. data.isFavorite = Favorites.isFavorite(data);
  89. if(data.children && data.children.length){ //we have a class or constructor
  90. this.selected.push(data);
  91. var list = $.makeArray(data.children).sort(Search.sortFn)
  92. var self = this;
  93. var results = $("#results");
  94. if(results.length){
  95. $("#results").slideUp("fast",
  96. this.callback( "showResultsAndDoc",
  97. {list: list, selected: this.selected, hide: true},
  98. data));
  99. }else{
  100. this.showResultsAndDoc({list: list, selected: this.selected, hide: true}, data)
  101. }
  102. }else{ //we have a function or attribute
  103. //see if we can pick it
  104. if($("#results a").length == 0){
  105. //we should probably try to get first parent as result, but whatever ...
  106. $("#left").html("//jmvcdoc/views/results.ejs",
  107. {list: Search.find(""), selected: this.selected, hide: false},
  108. DocumentationHelpers)
  109. }
  110. $(".result").removeClass("picked")
  111. $(".result[href=#&who="+who+"]").addClass("picked").focus()
  112. this.showDoc(data)
  113. }
  114. },
  115. //event handlers
  116. "#search focus" : function(el, ev){
  117. $('#results a:first').addClass("highlight");
  118. },
  119. "#search blur" : function(el, ev){
  120. $('#results a:first').removeClass("highlight");
  121. },
  122. "#search keyup" : function(el, ev){
  123. if(ev.keyCode == 40){ //down
  124. $('#results a:first').removeClass("highlight")
  125. $('#results a:nth-child(2)')[0].focus();
  126. }
  127. else if(ev.keyCode == 13){
  128. window.location.hash = $('#results a:first').attr("href")
  129. }
  130. else{
  131. if(this.skipSet){
  132. this.skipSet = false;
  133. return
  134. }
  135. window.location.hash = "#"
  136. this.search(el.val());
  137. $('#results a:first').addClass("highlight");
  138. }
  139. },
  140. "#results a focus" : function(el){
  141. el.addClass("highlight")//css("backgroundColor","#4B4C3F")
  142. },
  143. "#results a blur" : function(el){
  144. el.removeClass("highlight")//el.css("backgroundColor","")
  145. },
  146. "#results a mouseover" : function(el){
  147. el.addClass("highlight")//css("backgroundColor","#4B4C3F")
  148. },
  149. "#results a mouseout" : function(el){
  150. el.removeClass("highlight")//el.css("backgroundColor","")
  151. },
  152. "#results a keyup" : function(el,ev){
  153. if(ev.keyCode == 40){ //down
  154. var n = el.next();
  155. if(n.length) n[0].focus();
  156. ev.preventDefault();
  157. }
  158. else if(ev.keyCode == 38){ //up
  159. var p = el.prev(), p2 = p.prev()
  160. if(p2.length)
  161. p[0].focus()
  162. else{
  163. this.skipSet = true;
  164. $("#search")[0].focus();
  165. //this.highlightFirst();
  166. }
  167. ev.preventDefault();
  168. }
  169. },
  170. ".remove click" : function(el, ev){
  171. ev.stopImmediatePropagation();
  172. this.selected.pop();
  173. //fire to history
  174. if(this.selected.length){
  175. var who = this.selected.pop().name;
  176. $("#results").slideUp("fast", function(){
  177. window.location.hash = "#&who="+who;
  178. })
  179. }else{
  180. var self = this;
  181. $("#results").slideUp("fast", function(){
  182. window.location.hash = "#"
  183. })
  184. }
  185. },
  186. ".favorite click" : function(el){
  187. var isFavorite = Favorites.toggle(this.who)
  188. if(isFavorite){
  189. el.removeClass("favoriteoff")
  190. el.addClass("favoriteon")
  191. }else{
  192. el.removeClass("favoriteon")
  193. el.addClass("favoriteoff")
  194. }
  195. },
  196. "history.favorites.index subscribe" : function(called, data){
  197. this.selected = [];
  198. $("#search").val("favorites")
  199. var list = Favorites.findAll();
  200. $("#left").html("//jmvcdoc/views/results.ejs",
  201. {list: list, selected: this.selected, hide: false},
  202. DocumentationHelpers)
  203. if(!list.length)
  204. $('#doc').html("//jmvcdoc/views/favorite.ejs",{})
  205. },
  206. ready : function(){
  207. /*var self = this;
  208. this.find("#documentation").phui_filler({parent: $(window)});
  209. this.find("#bottom").phui_filler();
  210. this.find("#bottom").bind("resize", function(){
  211. var h = $(this).height();
  212. self.find("#left").height(h);
  213. self.find("#doc_container").height(h);
  214. });*/
  215. this.loaded = true;
  216. hljs.start();
  217. this.loadText = $("#search").val();
  218. $("#search").val("Loading ...")
  219. Search.load(this.callback('setSearchReady'));
  220. },
  221. setSearchReady : function(){
  222. this.searchReady = true;
  223. //do what you would normally do
  224. $("#search").attr('disabled', false)
  225. $("#search").val(this.loadText).focus();
  226. if(this.loadHistoryData){
  227. //need a timeout to allow reset of C function
  228. //by jQuery
  229. var self= this;
  230. setTimeout(function(){
  231. self.handleHistoryChange(self.loadHistoryData);
  232. },1)
  233. }
  234. },
  235. handleHistoryChange : function(data){
  236. if(data.search){
  237. $("#search").val(data.search);
  238. this.searchCurrent();
  239. if(!data.who) return;
  240. }
  241. if(!data.who){
  242. this.searchCurrent();
  243. if(this.who) return;
  244. data.who = "index"
  245. }
  246. var who = data.who;
  247. //might need to remove everyone under you from selected
  248. for(var i =0; i < this.selected.length; i++){
  249. if(this.selected[i].name == who){
  250. this.selected.splice(i,this.selected.length - i)
  251. break;
  252. }
  253. }
  254. $.ajax({
  255. url: DOCS_LOCATION + who.replace(/ /g, "_").replace(/&#46;/g, ".") + ".json",
  256. success: this.callback('show', who),
  257. error: this.callback('whoNotFound', who),
  258. jsonpCallback: "C",
  259. dataType: "jsonp"
  260. });
  261. },
  262. /**
  263. * A history event. Only want to act if search data is available.
  264. */
  265. "history.index subscribe" : function(called, data){
  266. if(!this.searchReady){ //if search is not ready .. wait until it is
  267. this.loadHistoryData = data;
  268. return;
  269. }
  270. this.handleHistoryChange(data)
  271. },
  272. whoNotFound : function(who) {
  273. var parts = who.split(".");
  274. parts.pop();
  275. if(parts.length) {
  276. who = parts.join(".");
  277. $.ajax({
  278. url: DOCS_LOCATION + who.replace(/ /g, "_").replace(/&#46;/g, ".") + ".json",
  279. success: this.callback('show', who),
  280. error: this.callback('whoNotFound', who),
  281. jsonpCallback: "C",
  282. dataType: "jsonp"
  283. });
  284. }
  285. }
  286. }
  287. );
  288. $.fn.highlight = function(){
  289. this.each(function(){
  290. hljs.highlightBlock(this)
  291. })
  292. return this;
  293. }