Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. var DomWriter = function (target, namespace) {
  2. var instance = this;
  3. var template = namespace;
  4.  
  5. var coalesce = function( value, defaultValue ) {
  6. return typeof(value) != 'undefined' ?
  7. value : defaultValue;
  8. };
  9.  
  10. this.isObject = function( value ) {
  11. return typeof(value) === 'object';
  12. };
  13.  
  14. this.addSubscription = function( path, element ) {
  15. postal.subscribe( "binding", path, function( msg ) {
  16. instance.write( path, msg );
  17. });
  18. if( _(instance[path]).isUndefined() ) {
  19. instance[path] = element;
  20. }
  21. };
  22.  
  23. this.crawl = function( namespace, element, callback ) {
  24. _(element)
  25. .chain()
  26. .keys()
  27. .each( function( key ) {
  28. var child = element[key];
  29. var fqn = namespace === "" ?
  30. key : namespace + "." + key;
  31. callback( namespace, key, child );
  32. if( !_.isNull( child ) && instance.isObject( child ) ) {
  33. instance.crawl( fqn, child, callback );
  34. }
  35. });
  36. };
  37.  
  38. this.domCrawl = function( namespace, element, callback ) {
  39. var id = coalesce( element["id"], "" );
  40. var fqn = namespace == "" ?
  41. id : ( id === "" ? namespace : namespace + "." + id );
  42. callback( fqn, id, element );
  43. if( element.children !== undefined &&
  44. element.children.length > 0 ) {
  45. _(element.children)
  46. .chain()
  47. .each( function( child ) {
  48. instance.domCrawl(
  49. fqn,
  50. child,
  51. callback );
  52. });
  53. }
  54. };
  55.  
  56. this.domCrawl( "", target[0], function( namespace, key, child ) {
  57. var prefix = template + ".";
  58. var member = namespace === template ?
  59. namespace : namespace.replace( prefix, "", "gi");
  60. instance.addSubscription( member, child );
  61. });
  62.  
  63. this.write = function( path, value ) {
  64. element = this[path];
  65. this[$(element)[0].tagName.toLowerCase()]
  66. ( path, element, value );
  67. };
  68.  
  69. this.div = function( fqn, element, value ) {
  70. if( instance.isObject( value ) ) {
  71. instance.crawl( "", value, function( namespace, key, child ) {
  72. instance.write(
  73. namespace === "" ? key : namespace + "." + key,
  74. child );
  75. });
  76. }
  77. else {
  78. $(element).text( value );
  79. }
  80. };
  81.  
  82. this.span = function( fqn, element, value ) {
  83. $(element).text( value );
  84. };
  85.  
  86. this.input = function( fqn, element, value ) {
  87. $(element).val( value );
  88. };
  89.  
  90. this.ul = function( fqn, element, value ) {
  91. var children = $(element).children("li");
  92. if( children.length < value.length ) {
  93. $(element).empty();
  94. var li = children[0];
  95. var indx = 0;
  96. _(value)
  97. .each( function( child ) {
  98. var newLi = $(li)
  99. .clone()
  100. .attr( "id", indx )
  101. .appendTo( element );
  102. instance[ fqn + "." + indx ] = newLi[0];
  103.  
  104. instance.domCrawl(
  105. fqn,
  106. newLi[0],
  107. function( namespace, key, child ) {
  108. var prefix = template + ".";
  109. var member = namespace === template ?
  110. namespace : namespace.replace( prefix, "", "gi");
  111. instance.addSubscription( member, child );
  112. }
  113. );
  114.  
  115. indx++;
  116. });
  117. }
  118. };
  119.  
  120. this.li = function( fqn, element, value ) {
  121. $(element).text( value );
  122. };
  123. };
Add Comment
Please, Sign In to add comment