dojox/highlight/widget/Code.js

  • Provides:

    • dojox.highlight.widget.Code
  • Requires:

    • dijit._Widget in common in project dijit
    • dijit._Templated in common in project dijit
    • dojox.highlight in common
  • highlight.Code

    • type
      Function
    • chains:
      • dijit._Widget: (prototype)
      • dijit._Widget: (call)
      • dijit._Templated: (call)
    • mixins:
      • dijit._Templated.prototype: (prototype)
    • summary
  • highlight.Code.url

    • summary
  • highlight.Code.range

    • summary
  • highlight.Code.style

    • summary
  • highlight.Code.listType

    • summary
  • highlight.Code.lang

    • summary
  • highlight.Code.templateString

    • summary
  • highlight.Code.postCreate

    • type
      Function
    • source: [view]
        this.inherited(arguments);
        if(this.url){
         // load from a url
         dojo.xhrGet({
          url: this.url,
          // then poopulate:
          load: dojo.hitch(this,"_populate"),
          error: dojo.hitch(this,"_loadError")
         });
        }else{
         // or just populate from our internal content
         this._populate(this.containerNode.innerHTML);
        }
    • summary
  • highlight.Code._populate

    • type
      Function
    • parameters:
      • data: (typeof )
    • source: [view]
      dojo.provide("dojox.highlight.widget.Code");
      dojo.require("dijit._Widget");
      dojo.require("dijit._Templated");
      dojo.require("dojox.highlight");


      // A simple source code formatting widget that adds line numbering, alternating line colors
      // and line range support on top of dojox.highlight module.
      dojo.declare("highlight.Code",[dijit._Widget, dijit._Templated],{
       url: "",
       range:null,
       style:"",
       listType:"1",
       lang:"",
       // Note: If more control over formatting is required, the order list items can be replaced
       // with a table implementation instead... Excercise is left for those that need it...
       templateString:
        '
      '+
         '
      '+
         '
        ' +
           '
        ' +
          '
        ',

         
         postCreate: function(){
          this.inherited(arguments);
          if(this.url){
           // load from a url
           dojo.xhrGet({
            url: this.url,
            // then poopulate:
            load: dojo.hitch(this,"_populate"),
            error: dojo.hitch(this,"_loadError")
           });
          }else{
           // or just populate from our internal content
           this._populate(this.containerNode.innerHTML);
          }
         },

         
         _populate: function(data){
          // put the content in a common node
          this.containerNode.innerHTML =
           "
        " +
            data.replace(/\   "
        ";
          // highlight it
          dojo.query("pre > code",this.containerNode).forEach(dojox.highlight.init);
          // FIXME: in ie7, the innerHTML in a real
         isn't split by \n's ?
          // split the content into lines
          var lines = this.containerNode.innerHTML.split("\n");
          dojo.forEach(lines,function(line,i){
           // setup all the lines of the content as
      1. 's
           var li = dojo.doc.createElement('li');
           // add some style sugar:
           dojo.addClass(li, (i % 2 !== 0 ? "even" : "odd"));
           line = "
        " + line + " 
        ";
           line = line.replace(/\t/g,"   ");
           li.innerHTML = line;
           this.codeList.appendChild(li);
          },this);
          // save our data
          this._lines = dojo.query("li",this.codeList);
          this._updateView();
      2. summary
    • highlight.Code.setRange

      • type
        Function
      • parameters:
        • range: (typeof Array)
      • source: [view]
          if(dojo.isArray(range)){
           this.range = range;
           this._updateView();
          }
      • summary
        update the view to a new passed range
    • highlight.Code._updateView

      • type
        Function
      • source: [view]
          if(this.range){
           var r = this.range;
           this._lines
            // hide them all
            .style({ display:"none" })
            .filter(function(n,i){
             // remove nodes out of range
             return (i + 1 >= r[0] && i + 1 <= r[1]);
            })
            // set them visible again
            .style({ display:"" })
           ;
           // set the "start" attribute on the OL so numbering works
           dojo.attr(this.codeList,"start",r[0]);
          }
      • summary
        set the list to the current range
    • highlight.Code._loadError

      • type
        Function
      • parameters:
        • error: (typeof )
      • source: [view]
          console.warn("loading: ", this.url, " FAILED", error);
      • summary
        a generic error handler for the url=&quot;&quot;
    • highlight.Code.containerNode.innerHTML

      • summary
    • highlight.Code._lines

      • summary
    • dojox.highlight.widget.Code

      • type
        Object
      • summary
    • dojox.highlight.widget

      • type
        Object
      • summary
    • dojox.highlight

      • type
        Object
      • summary
    • dojox

      • type
        Object
      • summary