index-d3.js.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: index-d3.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: index-d3.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * chatUI constructor
  21. * @constructor
  22. * @param {d3-selection} container - Container for the chat interface.
  23. * @return {object} chatUI object
  24. */
  25. var chatUI = (function (container) {
  26. var module = {};
  27. module.container = container.append('div').attr('id', 'cb-container');
  28. module.config = null;
  29. module.bubbles = [];
  30. module.ID = 0;
  31. module.keys = {};
  32. module.types = {};
  33. module.inputState = false;
  34. module.height = 0;
  35. module.scroll = module.container.append('div').attr('id', 'cb-flow');
  36. module.flow = module.scroll.append('div').attr('class', 'cb-inner');
  37. module.input = module.container.append('div').attr('id', 'cb-input').style('display', 'none');
  38. module.input.append('div').attr('id','cb-input-container').append('input').attr('type', 'text');
  39. module.input.append('button').text('+');
  40. /**
  41. * updateContainer should be called when height or width changes of the container changes
  42. * @memberof chatUI
  43. */
  44. module.updateContainer = function(){
  45. module.height = module.container.node().offsetHeight;
  46. module.flow.style('padding-top', module.height+'px');
  47. module.scroll.style('height', (module.height-((module.inputState==true)?77:0))+'px');
  48. module.scrollTo('end');
  49. };
  50. /**
  51. * @memberof chatUI
  52. * @param {object} options - object containing configs {type:string (e.g. 'text' or 'select'), class:string ('human' || 'bot'), value:depends on type}
  53. * @param {function} callback - function to be called after everything is done
  54. * @return {integer} id - id of the bubble
  55. */
  56. module.addBubble = function (options, callback) {
  57. callback = callback || function () { };
  58. if (!(options.type in module.types)) {
  59. throw 'Unknown bubble type';
  60. } else {
  61. module.ID++;
  62. var id = module.ID;
  63. module.bubbles.push({
  64. id: id,
  65. type: options.type
  66. //additional info
  67. });
  68. module.keys[id] = module.bubbles.length - 1;
  69. //segment container
  70. var outer = module.flow.append('div')
  71. .attr('class', 'cb-segment cb-' + options.class + ' cb-bubble-type-' + options.type)
  72. .attr('id', 'cb-segment-' + id);
  73. //speaker icon
  74. outer.append('div').attr('class', 'cb-icon');
  75. var bubble = outer.append('div')
  76. .attr('class', 'cb-bubble ' + options.class)
  77. // .style("height", "50px")
  78. .append('div')
  79. .attr('class', 'cb-inner');
  80. outer.append('hr');
  81. module.types[options.type](bubble, options, callback);
  82. module.scrollTo('end');
  83. return module.ID;
  84. }
  85. };
  86. /**
  87. * @memberof chatUI
  88. * @param {d3-selection} bubble - d3 selection of the bubble container
  89. * @param {object} options - object containing configs {type:'text', class:string ('human' || 'bot'), value:array of objects (e.g. [{label:'yes'}])}
  90. * @param {function} callback - function to be called after everything is done
  91. */
  92. module.types.select = function(bubble, options, callback){
  93. bubble.selectAll('.cb-choice').data(options.value).enter().append('div')
  94. .attr('class', 'cb-choice')
  95. .text(function(d){ return d.label; })
  96. .on('click', function(d){
  97. d3.select(this).classed('cb-active', true);
  98. d3.select(this.parentNode).selectAll('.cb-choice').on('click', function(){});
  99. callback(d);
  100. });
  101. };
  102. /**
  103. * @memberof chatUI
  104. * @param {d3-selection} bubble - d3 selection of the bubble container
  105. * @param {object} options - object containing configs {type:'text', class:string ('human' || 'bot'), value:string (e.g. 'Hello World')}
  106. * @param {function} callback - function to be called after everything is done
  107. */
  108. module.types.text = function (bubble, options, callback) {
  109. if (('delay' in options) &amp;&amp; options.delay) {
  110. var animatedCircles = '&lt;div class="circle">&lt;/div>&lt;div class="circle">&lt;/div>&lt;div class="circle">&lt;/div>';
  111. bubble.append('div')
  112. .attr('class', 'cb-waiting')
  113. .html(animatedCircles);
  114. setTimeout(function () {
  115. bubble.select(".cb-waiting").remove();
  116. module.appendText(bubble, options, callback);
  117. }, (isNaN(options.delay) ? 1000 : options.delay));
  118. } else {
  119. module.appendText(bubble, options, callback);
  120. }
  121. };
  122. /**
  123. * Helper Function for adding text to a bubble
  124. * @memberof chatUI
  125. * @param {d3-selection} bubble - d3 selection of the bubble container
  126. * @param {object} options - object containing configs {type:'text', class:string ('human' || 'bot'), value:string (e.g. 'Hello World')}
  127. * @param {function} callback - function to be called after everything is done
  128. */
  129. module.appendText = function(bubble, options, callback) {
  130. bubble.attr('class', 'bubble-ctn-' + options.class).append('p')
  131. .html(options.value)
  132. .transition()
  133. .duration(200)
  134. .style("width", "auto")
  135. .style('opacity', 1);
  136. chat.scrollTo('end');
  137. callback();
  138. };
  139. /**
  140. * Showing the input module and set cursor into input field
  141. * @memberof chatUI
  142. * @param {function} submitCallback - function to be called when user presses enter or submits through the submit-button
  143. * @param {function} typeCallback - function to when user enters text (on change)
  144. */
  145. module.showInput = function (submitCallback, typeCallback) {
  146. module.inputState = true;
  147. if (typeCallback) {
  148. module.input.select('input')
  149. .on('change', function () {
  150. typeCallback(d3.select(this).node().value);
  151. });
  152. } else {
  153. module.input.select('input').on('change', function () { });
  154. }
  155. module.input.select('input').on('keyup', function () {
  156. if (d3.event.keyCode == 13) {
  157. submitCallback(module.input.select('input').node().value);
  158. module.input.select('input').node().value = '';
  159. }
  160. });
  161. module.input.select('button')
  162. .on('click', function () {
  163. submitCallback(module.input.select('input').node().value);
  164. module.input.select('input').node().value = '';
  165. });
  166. module.input.style('display', 'block');
  167. module.updateContainer();
  168. module.input.select('input').node().focus();
  169. module.scrollTo('end');
  170. };
  171. /**
  172. * Hide the input module
  173. */
  174. module.hideInput = function () {
  175. module.input.select('input').node().blur();
  176. module.input.style('display', 'none');
  177. module.inputState = false;
  178. module.updateContainer();
  179. module.scrollTo('end');
  180. };
  181. /**
  182. * Remove a bubble from the chat
  183. * @memberof chatUI
  184. * @param {integer} id - id of bubble provided by addBubble
  185. */
  186. module.removeBubble = function (id) {
  187. module.flow.select('#cb-segment-' + id).remove();
  188. module.bubbles.splice(module.keys[id], 1);
  189. delete module.keys[id];
  190. };
  191. /**
  192. * Remove all bubbles until the bubble with 'id' from the chat
  193. * @memberof chatUI
  194. * @param {integer} id - id of bubble provided by addBubble
  195. */
  196. module.removeBubbles = function (id) {
  197. for (var i = module.bubbles.length - 1; i >= module.keys[id]; i--) {
  198. module.removeBubble(module.bubbles[i].id);
  199. }
  200. };
  201. /**
  202. * Remove all bubbles until the bubble with 'id' from the chat
  203. * @memberof chatUI
  204. * @param {integer} id - id of bubble provided by addBubble
  205. * @return {object} obj - {el:d3-selection, obj:bubble-data}
  206. */
  207. module.getBubble = function (id) {
  208. return {
  209. el: module.flow.select('#cb-segment-' + id),
  210. obj: module.bubbles[module.keys[id]]
  211. };
  212. };
  213. /**
  214. * Scroll chat flow
  215. * @memberof chatUI
  216. * @param {string} position - where to scroll either 'start' or 'end'
  217. */
  218. module.scrollTo = function (position) {
  219. //start
  220. var s = 0;
  221. //end
  222. if (position == 'end') {
  223. s = module.scroll.property('scrollHeight') - (window.innerHeight-77);
  224. }
  225. d3.select('#cb-flow').transition()
  226. .duration(300)
  227. .tween("scroll", scrollTween(s));
  228. };
  229. function scrollTween(offset) {
  230. return function () {
  231. var i = d3.interpolateNumber(module.scroll.property('scrollTop'), offset);
  232. return function (t) { module.scroll.property('scrollTop', i(t)); };
  233. };
  234. }
  235. function debouncer( func , _timeout ) {
  236. var timeoutID , timeout = _timeout || 200;
  237. return function () {
  238. var scope = this , args = arguments;
  239. clearTimeout( timeoutID );
  240. timeoutID = setTimeout( function () {
  241. func.apply( scope , Array.prototype.slice.call( args ) );
  242. } , timeout );
  243. };
  244. }
  245. //On Resize scroll to end
  246. d3.select(window).on('resize', debouncer(function(e){
  247. module.updateContainer();
  248. }, 200));
  249. module.updateContainer();
  250. return module;
  251. });</code></pre>
  252. </article>
  253. </section>
  254. </div>
  255. <nav>
  256. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="chatUI.html">chatUI</a></li></ul>
  257. </nav>
  258. <br class="clear">
  259. <footer>
  260. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Nov 20 2017 08:30:15 GMT+0100 (CET)
  261. </footer>
  262. <script> prettyPrint(); </script>
  263. <script src="scripts/linenumber.js"> </script>
  264. </body>
  265. </html>