Code.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.highlight.widget.Code"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.highlight.widget.Code"] = true;
  8. dojo.provide("dojox.highlight.widget.Code");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.require("dojox.highlight");
  12. // A simple source code formatting widget that adds line numbering, alternating line colors
  13. // and line range support on top of dojox.highlight module.
  14. dojo.declare("highlight.Code",[dijit._Widget, dijit._Templated],{
  15. url: "",
  16. range:null,
  17. style:"",
  18. listType:"1",
  19. lang:"",
  20. // Note: If more control over formatting is required, the order list items can be replaced
  21. // with a table implementation instead... Excercise is left for those that need it...
  22. templateString:
  23. '<div class="formatted" style="${style}">'+
  24. '<div class="titleBar"></div>'+
  25. '<ol type="${listType}" dojoAttachPoint="codeList" class="numbers"></ol>' +
  26. '<div style="display:none" dojoAttachPoint="containerNode"></div>' +
  27. '</div>',
  28. postCreate: function(){
  29. this.inherited(arguments);
  30. if(this.url){
  31. // load from a url
  32. dojo.xhrGet({
  33. url: this.url,
  34. // then poopulate:
  35. load: dojo.hitch(this,"_populate"),
  36. error: dojo.hitch(this,"_loadError")
  37. });
  38. }else{
  39. // or just populate from our internal content
  40. this._populate(this.containerNode.innerHTML);
  41. }
  42. },
  43. _populate: function(data){
  44. // put the content in a common node
  45. this.containerNode.innerHTML =
  46. "<pre><code class='" + this.lang + "'>" +
  47. data.replace(/\</g,"&lt;") +
  48. "</code></pre>";
  49. // highlight it
  50. dojo.query("pre > code",this.containerNode).forEach(dojox.highlight.init);
  51. // FIXME: in ie7, the innerHTML in a real <pre> isn't split by \n's ?
  52. // split the content into lines
  53. var lines = this.containerNode.innerHTML.split("\n");
  54. dojo.forEach(lines,function(line,i){
  55. // setup all the lines of the content as <li>'s
  56. var li = dojo.doc.createElement('li');
  57. // add some style sugar:
  58. dojo.addClass(li, (i % 2 !== 0 ? "even" : "odd"));
  59. line = "<pre><code>" + line + "&nbsp;</code></pre>";
  60. line = line.replace(/\t/g," &nbsp; ");
  61. li.innerHTML = line;
  62. this.codeList.appendChild(li);
  63. },this);
  64. // save our data
  65. this._lines = dojo.query("li",this.codeList);
  66. this._updateView();
  67. },
  68. setRange: function(/* Array */range){
  69. // summary: update the view to a new passed range
  70. if(dojo.isArray(range)){
  71. this.range = range;
  72. this._updateView();
  73. }
  74. },
  75. _updateView: function(){
  76. // summary: set the list to the current range
  77. if(this.range){
  78. var r = this.range;
  79. this._lines
  80. // hide them all
  81. .style({ display:"none" })
  82. .filter(function(n,i){
  83. // remove nodes out of range
  84. return (i + 1 >= r[0] && i + 1 <= r[1]);
  85. })
  86. // set them visible again
  87. .style({ display:"" })
  88. ;
  89. // set the "start" attribute on the OL so numbering works
  90. dojo.attr(this.codeList,"start",r[0]);
  91. }
  92. },
  93. _loadError: function(error){
  94. // summary: a generic error handler for the url=""
  95. console.warn("loading: ", this.url, " FAILED", error);
  96. }
  97. });
  98. }