Last active
September 22, 2020 15:28
-
-
Save jenya239/86aa3afafb1a07e7ed7b4c92e5585559 to your computer and use it in GitHub Desktop.
virtual node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
virtual_node_context = | |
nodes_map: new WeakMap | |
class StringNode | |
constructor: (@parent, @string)-> | |
@context =@parent .context | |
try_render: ( container )-> | |
el =$ '<div>' | |
el .append @string | |
@contents =el .contents() | |
@contents .appendTo container | |
@jquery =@contents | |
@nodes =@contents .get() | |
for node in @nodes | |
@context .nodes_map .set node, @ | |
print: -> | |
debug @string | |
class VirtualNode | |
@create: (tree)-> | |
new VirtualNode null, null, tree | |
constructor: (@parent, @type, @params)-> | |
@context =if @parent then @parent .context else virtual_node_context | |
@children =[] | |
@process() | |
process: -> | |
debug @type, JSON .stringify @params, null, ' ' | |
@tagname =@type if utils .is_string( @type ) | |
if utils .is_function @type # if Field ! | |
@params =@type .apply null, @params | |
@type =null | |
@process() | |
else if utils .is_string @params #if field | |
@children .push new StringNode @, @params | |
else if utils .is_array @params | |
if utils .is_function @params[0] | |
@type =@params[ 0 ] | |
@params =@params .slice 1 | |
@process() | |
else | |
for v in @params | |
@process_array_item v | |
else if utils .is_object @params | |
for own k, v of @params | |
@process_object_pair k, v | |
process_object_pair: ( key, value, is_self =true )-> | |
switch key | |
when 'css' | |
@css =value | |
else | |
if is_self | |
@type =key | |
@params =value | |
@process() | |
else | |
@child key, value | |
process_array_item: ( v )-> | |
if utils .is_object v | |
for own k2, v2 of v | |
@process_object_pair k2, v2, false | |
else if utils .is_array v # check tuple | |
if utils .is_function v[ 0 ] | |
@child v[ 0 ], v .slice 1 | |
else | |
for v2 in v | |
@process_array_item v2 | |
else if utils .is_string v | |
@children .push new StringNode @, v | |
child: ( type, params )-> | |
@children .push new VirtualNode @, type, params | |
try_render: ( container )-> | |
if @tagname | |
container .append element = $ "<#{ @tagname }>" | |
@css and element .css @css | |
@jquery =element | |
@nodes =element .get() | |
for node in @nodes | |
@context .nodes_map .set node, @ | |
else | |
element =container | |
child .try_render( element ) for child in @children | |
print: -> | |
if @tagname then console .group( @tagname, @css or '' ) else console .group( '' ) | |
for child in @children | |
if utils .is_string child | |
console .log child | |
else | |
child .print() | |
console .groupEnd() | |
item =( parent_path, obj )-> | |
return div: obj.name unless obj .isDirectory() #field | |
path = modules .path .join( parent_path, obj .name ) | |
children =modules .fs .readdirSync( path, withFileTypes: true ) | |
.filter( (f)->f.name != 'node_modules' ) | |
.map( (file) -> [ item, path, file ] ) | |
[ { div: obj.name }, { div: [ { css: { marginLeft: '20px' } } ] .concat( children ) } ] | |
tabs =( names )-> | |
div: [ { css: { width: '100%', height: '18px' } } ] .concat names .map ( name )-> | |
div: [ { css: { float: 'left', width: '220px' } }, name ] | |
main = | |
init: -> | |
$ => | |
command('xdotool windowactivate $(xdotool search --name "dashboard2")') | |
document =window .document | |
r = VirtualNode .create [ item, '.', { name: '.', isDirectory: (-> true) } ] | |
debug r | |
r .print() | |
VirtualNode .create( [ tabs, [ 'asdf', '=2=1212', '11111' ] ] ) .try_render $ document .body | |
r .try_render $ document .body | |
$( document .body ) .on 'click', -> | |
#задача здесь менять только tabs_array | |
VirtualNode .create( [ tabs, [ Math .random() + '', '12212', '11111' ] ] ) .try_render $ document .body | |
main .init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment