Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. var HelloWorld = (function () { 'use strict';
  2.  
  3. var template = (function () {
  4. return {
  5. methods: {
  6. foo: function ( event ) {
  7. this.set({count: Math.random()})
  8. },
  9. bar: function(event) {
  10. console.log('bar', event)
  11. }
  12. }
  13. };
  14. }());
  15.  
  16. function renderMainFragment ( root, component ) {
  17. var h1 = createElement( 'h1' );
  18.  
  19. appendNode( createText( "Hello " ), h1 );
  20. var text1 = createText( root.name );
  21. appendNode( text1, h1 );
  22. var text2 = createText( "\n" );
  23.  
  24. var p = createElement( 'p' );
  25.  
  26. appendNode( createText( "Count: " ), p );
  27. var text4 = createText( root.count );
  28. appendNode( text4, p );
  29. var text5 = createText( "\n" );
  30.  
  31. var button = createElement( 'button' );
  32.  
  33. function clickHandler ( event ) {
  34. var root = this.__svelte.root;
  35.  
  36. component.set({ count: root.count + 1 });
  37. }
  38.  
  39. addEventListener( button, 'click', clickHandler );
  40.  
  41. button.__svelte = {
  42. root: root
  43. };
  44.  
  45. appendNode( createText( "+1" ), button );
  46. var text7 = createText( "\n" );
  47. var ifBlock_anchor = createComment();
  48.  
  49. function getBlock ( root ) {
  50. if ( root.count ) return renderIfBlock_0;
  51. return null;
  52. }
  53.  
  54. var currentBlock = getBlock( root );
  55. var ifBlock = currentBlock && currentBlock( root, component );
  56.  
  57. var text8 = createText( "\n" );
  58.  
  59. var input = createElement( 'input' );
  60.  
  61. var input_updating = false;
  62.  
  63. function inputChangeHandler () {
  64. input_updating = true;
  65. component.set({ name: input.value });
  66. input_updating = false;
  67. }
  68.  
  69. addEventListener( input, 'input', inputChangeHandler );
  70. input.value = root.name;
  71.  
  72. function keyupHandler ( event ) {
  73. component.bar(event);
  74. }
  75.  
  76. addEventListener( input, 'keyup', keyupHandler );
  77.  
  78. input.placeholder = "enter your name";
  79.  
  80. var text9 = createText( "\n" );
  81.  
  82. var p1 = createElement( 'p' );
  83.  
  84. appendNode( createText( "Hello " ), p1 );
  85. var text11 = createText( root.name || 'stranger' );
  86. appendNode( text11, p1 );
  87. appendNode( createText( "!" ), p1 );
  88.  
  89. return {
  90. mount: function ( target, anchor ) {
  91. insertNode( h1, target, anchor );
  92. insertNode( text2, target, anchor );
  93. insertNode( p, target, anchor );
  94. insertNode( text5, target, anchor );
  95. insertNode( button, target, anchor );
  96. insertNode( text7, target, anchor );
  97. insertNode( ifBlock_anchor, target, anchor );
  98. if ( ifBlock ) ifBlock.mount( ifBlock_anchor.parentNode, ifBlock_anchor );
  99. insertNode( text8, target, anchor );
  100. insertNode( input, target, anchor );
  101. insertNode( text9, target, anchor );
  102. insertNode( p1, target, anchor );
  103. },
  104.  
  105. update: function ( changed, root ) {
  106. text1.data = root.name;
  107.  
  108. text4.data = root.count;
  109.  
  110. button.__svelte.root = root;
  111.  
  112. var _currentBlock = currentBlock;
  113. currentBlock = getBlock( root );
  114. if ( _currentBlock === currentBlock && ifBlock) {
  115. ifBlock.update( changed, root );
  116. } else {
  117. if ( ifBlock ) ifBlock.teardown( true );
  118. ifBlock = currentBlock && currentBlock( root, component );
  119. if ( ifBlock ) ifBlock.mount( ifBlock_anchor.parentNode, ifBlock_anchor );
  120. }
  121.  
  122. if ( !input_updating ) input.value = root.name;
  123.  
  124. text11.data = root.name || 'stranger';
  125. },
  126.  
  127. teardown: function ( detach ) {
  128. removeEventListener( button, 'click', clickHandler );
  129. if ( ifBlock ) ifBlock.teardown( detach );
  130. removeEventListener( input, 'input', inputChangeHandler );
  131. removeEventListener( input, 'keyup', keyupHandler );
  132.  
  133. if ( detach ) {
  134. detachNode( h1 );
  135. detachNode( text2 );
  136. detachNode( p );
  137. detachNode( text5 );
  138. detachNode( button );
  139. detachNode( text7 );
  140. detachNode( ifBlock_anchor );
  141. detachNode( text8 );
  142. detachNode( input );
  143. detachNode( text9 );
  144. detachNode( p1 );
  145. }
  146. }
  147. };
  148. }
  149.  
  150. function renderIfBlock_0 ( root, component ) {
  151. var button = createElement( 'button' );
  152.  
  153. function clickHandler ( event ) {
  154. component.foo(event);
  155. }
  156.  
  157. addEventListener( button, 'click', clickHandler );
  158.  
  159. appendNode( createText( "foo" ), button );
  160.  
  161. return {
  162. mount: function ( target, anchor ) {
  163. insertNode( button, target, anchor );
  164. },
  165.  
  166. update: noop,
  167.  
  168. teardown: function ( detach ) {
  169. removeEventListener( button, 'click', clickHandler );
  170.  
  171. if ( detach ) {
  172. detachNode( button );
  173. }
  174. }
  175. };
  176. }
  177.  
  178. function HelloWorld ( options ) {
  179. options = options || {};
  180.  
  181. this._state = options.data || {};
  182.  
  183. this._observers = {
  184. pre: Object.create( null ),
  185. post: Object.create( null )
  186. };
  187.  
  188. this._handlers = Object.create( null );
  189.  
  190. this._root = options._root;
  191. this._yield = options._yield;
  192.  
  193. this._fragment = renderMainFragment( this._state, this );
  194. if ( options.target ) this._fragment.mount( options.target, null );
  195. }
  196.  
  197. HelloWorld.prototype = template.methods;
  198.  
  199. HelloWorld.prototype.get = function get( key ) {
  200. return key ? this._state[ key ] : this._state;
  201. };
  202.  
  203. HelloWorld.prototype.fire = function fire( eventName, data ) {
  204. var handlers = eventName in this._handlers && this._handlers[ eventName ].slice();
  205. if ( !handlers ) return;
  206.  
  207. for ( var i = 0; i < handlers.length; i += 1 ) {
  208. handlers[i].call( this, data );
  209. }
  210. };
  211.  
  212. HelloWorld.prototype.observe = function observe( key, callback, options ) {
  213. var group = ( options && options.defer ) ? this._observers.pre : this._observers.post;
  214.  
  215. ( group[ key ] || ( group[ key ] = [] ) ).push( callback );
  216.  
  217. if ( !options || options.init !== false ) {
  218. callback.__calling = true;
  219. callback.call( this, this._state[ key ] );
  220. callback.__calling = false;
  221. }
  222.  
  223. return {
  224. cancel: function () {
  225. var index = group[ key ].indexOf( callback );
  226. if ( ~index ) group[ key ].splice( index, 1 );
  227. }
  228. };
  229. };
  230.  
  231. HelloWorld.prototype.on = function on( eventName, handler ) {
  232. var handlers = this._handlers[ eventName ] || ( this._handlers[ eventName ] = [] );
  233. handlers.push( handler );
  234.  
  235. return {
  236. cancel: function () {
  237. var index = handlers.indexOf( handler );
  238. if ( ~index ) handlers.splice( index, 1 );
  239. }
  240. };
  241. };
  242.  
  243. HelloWorld.prototype.set = function set( newState ) {
  244. this._set( newState );
  245. ( this._root || this )._flush();
  246. };
  247.  
  248. HelloWorld.prototype._flush = function _flush() {
  249. if ( !this._renderHooks ) return;
  250.  
  251. while ( this._renderHooks.length ) {
  252. var hook = this._renderHooks.pop();
  253. hook.fn.call( hook.context );
  254. }
  255. };
  256.  
  257. HelloWorld.prototype._set = function _set ( newState ) {
  258. var oldState = this._state;
  259. this._state = Object.assign( {}, oldState, newState );
  260.  
  261. dispatchObservers( this, this._observers.pre, newState, oldState );
  262. if ( this._fragment ) this._fragment.update( newState, this._state );
  263. dispatchObservers( this, this._observers.post, newState, oldState );
  264. };
  265.  
  266. HelloWorld.prototype.teardown = function teardown ( detach ) {
  267. this.fire( 'teardown' );
  268.  
  269. this._fragment.teardown( detach !== false );
  270. this._fragment = null;
  271.  
  272. this._state = {};
  273. };
  274.  
  275. function dispatchObservers( component, group, newState, oldState ) {
  276. for ( var key in group ) {
  277. if ( !( key in newState ) ) continue;
  278.  
  279. var newValue = newState[ key ];
  280. var oldValue = oldState[ key ];
  281.  
  282. if ( newValue === oldValue && typeof newValue !== 'object' ) continue;
  283.  
  284. var callbacks = group[ key ];
  285. if ( !callbacks ) continue;
  286.  
  287. for ( var i = 0; i < callbacks.length; i += 1 ) {
  288. var callback = callbacks[i];
  289. if ( callback.__calling ) continue;
  290.  
  291. callback.__calling = true;
  292. callback.call( component, newValue, oldValue );
  293. callback.__calling = false;
  294. }
  295. }
  296. }
  297.  
  298. function createElement( name ) {
  299. return document.createElement( name );
  300. }
  301.  
  302. function detachNode( node ) {
  303. node.parentNode.removeChild( node );
  304. }
  305.  
  306. function insertNode( node, target, anchor ) {
  307. target.insertBefore( node, anchor );
  308. }
  309.  
  310. function appendNode( node, target ) {
  311. target.appendChild( node );
  312. }
  313.  
  314. function createText( data ) {
  315. return document.createTextNode( data );
  316. }
  317.  
  318. function addEventListener( node, event, handler ) {
  319. node.addEventListener ( event, handler, false );
  320. }
  321.  
  322. function removeEventListener( node, event, handler ) {
  323. node.removeEventListener ( event, handler, false );
  324. }
  325.  
  326. function noop() {}
  327.  
  328. function createComment() {
  329. return document.createComment( '' );
  330. }
  331.  
  332. return HelloWorld;
  333.  
  334. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement