Class: JsTreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/jstreebuilder.rb

Defined Under Namespace

Classes: TreeBuilder

Constant Summary collapse

TREE_CSS =
%q[
/* Remove default bullets */
ul, #myUL {
  list-style-type: none;
}

/* Remove margins and padding from the parent ul */
#myUL {
  margin: 0;
  padding: 0;
}

/* Style the caret/arrow */
.caret {
  cursor: pointer; 
  user-select: none; /* Prevent text selection */
}

/* Create the caret/arrow with a unicode, and style it */
.caret::before {
  content: "\25B6";
  color: black;
  display: inline-block;
  margin-right: 6px;
}

/* Rotate the caret/arrow icon when clicked on (using JavaScript) */
.caret-down::before {
  transform: rotate(90deg); 
}

/* Hide the nested list */
.nested {
  display: none;
}

/* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
.active {
  display: block;
}
]
TREE_JS =
"var toggler = document.getElementsByClassName(\"caret\");\nvar i;\n\nfor (i = 0; i < toggler.length; i++) {\n  toggler[i].addEventListener(\"click\", function() {\n    this.parentElement.querySelector(\".nested\").classList.toggle(\"active\");\n    this.classList.toggle(\"caret-down\");\n  });\n}\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unknown = nil, options = {}) ⇒ JsTreeBuilder

Returns a new instance of JsTreeBuilder.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/jstreebuilder.rb', line 208

def initialize(unknown=nil, options={})
  
  if unknown.is_a? String or unknown.is_a? Symbol then
    type = unkown.to_sym
  elsif unknown.is_a? Hash
    options = {type: :tree}.merge(unknown)
    type = options[:type]
  end
  
  @debug = options[:debug]

  @types = i(tree)
  
  build(type, options) if type

end

Instance Attribute Details

#cssObject (readonly)

Returns the value of attribute css.



206
207
208
# File 'lib/jstreebuilder.rb', line 206

def css
  @css
end

#htmlObject (readonly)

Returns the value of attribute html.



206
207
208
# File 'lib/jstreebuilder.rb', line 206

def html
  @html
end

#jsObject (readonly)

Returns the value of attribute js.



206
207
208
# File 'lib/jstreebuilder.rb', line 206

def js
  @js
end

Instance Method Details

#to_cssObject



225
226
227
# File 'lib/jstreebuilder.rb', line 225

def to_css()
  @css
end

#to_htmlObject



229
230
231
# File 'lib/jstreebuilder.rb', line 229

def to_html()
  @html
end

#to_jsObject



233
234
235
# File 'lib/jstreebuilder.rb', line 233

def to_js()
  @js
end

#to_ulObject



237
238
239
# File 'lib/jstreebuilder.rb', line 237

def to_ul()
  @ul
end

#to_webpageObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/jstreebuilder.rb', line 241

def to_webpage()

  a = RexleBuilder.build do |xml|
    xml.html do 
      xml.head do
        xml.meta name: "viewport", content: \
            "width=device-width, initial-scale=1"
        xml.style "\nbody {font-family: Arial;}\n\n" + @css
      end
      xml.body @ul
    end
  end

  doc = Rexle.new(a)    
  
  doc.root.element('body').add \
      Rexle::Element.new('script').add_text "\n" + 
      @js.gsub(/^ +\/\/[^\n]+\n/,'')
  
  "<!DOCTYPE html>\n" + doc.xml(pretty: true, declaration: false)\
      .gsub(/<\/div>/,'\0' + "\n").gsub(/\n *<!--[^>]+>/,'')
  
end

#to_xmlObject



265
266
267
# File 'lib/jstreebuilder.rb', line 265

def to_xml()
  @xml
end