heiho.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * Heihō: simple spreadsheet viewer
  3. * @link https://github.com/kktsvetkov/heiho
  4. */
  5. ;(function(root, factory)
  6. {
  7. if (typeof exports === 'object')
  8. {
  9. module.exports = factory(window, document)
  10. } else
  11. {
  12. root.Heiho = factory(window, document)
  13. }
  14. })(this, function(w, d)
  15. {
  16. /**
  17. * Count number of columns in array or object
  18. *
  19. * @param {Array|Object} data
  20. * @return {Integer}
  21. */
  22. function cols(data)
  23. {
  24. var cols = 0;
  25. for (var i in data)
  26. {
  27. var l = 0;
  28. if ('object' == typeof data[i])
  29. {
  30. if (Array.isArray(data[i]))
  31. {
  32. l = data[i].length;
  33. } else
  34. {
  35. l = Object.keys( data[i] ).length;
  36. }
  37. }
  38. if (l > cols)
  39. {
  40. cols = l;
  41. }
  42. }
  43. return cols;
  44. }
  45. /**
  46. * Creates the DOM elements of the preview
  47. *
  48. * @param {String} id common prefix for ids of DOM elements
  49. * @return {Object} collection of elements
  50. */
  51. function load(id)
  52. {
  53. var el = {};
  54. /* outter preview shell */
  55. el.shell = document.createElement('div');
  56. el.shell.setAttribute('id', id);
  57. el.shell.style.display = 'none';
  58. /* preview header */
  59. el.header = document.createElement('div');
  60. el.header.setAttribute('id', id + '-header');
  61. el.shell.appendChild(el.header);
  62. /* preview header close button */
  63. el.close = document.createElement('div');
  64. el.close.setAttribute('id', id + '-close');
  65. el.header.appendChild(el.close);
  66. /* preview header title caption */
  67. el.title = document.createElement('div');
  68. el.title.setAttribute('id', id + '-title');
  69. el.header.appendChild(el.title);
  70. /* scrollable wrap of the preview grid */
  71. el.scroll = document.createElement('div');
  72. el.scroll.setAttribute('id', id + '-scroll');
  73. el.shell.appendChild(el.scroll);
  74. /* preview table grid */
  75. el.table = document.createElement('table');
  76. el.table.setAttribute('id', id + '-table');
  77. el.scroll.appendChild(el.table);
  78. /* preview grid thead */
  79. el.thead = document.createElement('tr');
  80. el.thead.setAttribute('id', id + '-thead');
  81. el.table.appendChild(el.thead);
  82. /* preview grid tbody */
  83. el.tbody = document.createElement('tbody');
  84. el.tbody.setAttribute('id', id + '-tbody');
  85. el.table.appendChild(el.tbody);
  86. /* preview truncate warning */
  87. el.truncate = document.createElement('div');
  88. el.truncate.setAttribute('id', id + '-truncated');
  89. el.truncate.style.display = 'none';
  90. el.shell.appendChild(el.truncate);
  91. /*
  92. * shell
  93. * |
  94. * + header
  95. * | |
  96. * | + close
  97. * | |
  98. * | + title
  99. * |
  100. * + scroll
  101. * | |
  102. * | + table
  103. * | |
  104. * | + thead
  105. * | |
  106. * | + tbody
  107. * |
  108. * + truncate
  109. */
  110. document.body.appendChild(el.shell);
  111. return el;
  112. }
  113. /**
  114. * Return TH column title based on integer index
  115. *
  116. * Column titles are only alphabet letters, using the 26 ascii
  117. * uppercase chars, e.g. "B" for 2, "AA" for 27
  118. *
  119. * @param {Integer} i
  120. * @retun {String}
  121. */
  122. function label(i)
  123. {
  124. i = parseInt(i);
  125. if (!i)
  126. {
  127. return '';
  128. }
  129. var h = '', j = i, k = 0;
  130. while (j > 26)
  131. {
  132. k = j % 26;
  133. j = Math.floor(j / 26);
  134. h = String.fromCharCode(64 + k) + h;
  135. }
  136. h = String.fromCharCode(64 + j) + h;
  137. return h;
  138. }
  139. /**
  140. * Resise the preview
  141. *
  142. * @param {Array} el collection of preview elements
  143. */
  144. function resize(el)
  145. {
  146. /* span the grid to full page width */
  147. if (el.shell.offsetWidth > el.table.offsetWidth)
  148. {
  149. el.table.className += ' width100';
  150. }
  151. /* adjust height */
  152. el.scroll.style.height = '';
  153. var height = el.shell.offsetHeight
  154. - 2 /* bottom offset */
  155. - el.header.offsetHeight
  156. - el.truncate.offsetHeight;
  157. if (height < el.table.offsetHeight)
  158. {
  159. el.scroll.style.height = height + 'px';
  160. }
  161. }
  162. /**
  163. * @var {Object} default options
  164. */
  165. var options = {
  166. /**
  167. * @var {String} prefix for all HTML ids used in the preview
  168. */
  169. id: 'heiho-view',
  170. /**
  171. * @var {Integer} max number of rows to preview
  172. */
  173. max: 100,
  174. /**
  175. * @var {Bool} whether to use first row as header or not
  176. */
  177. header: null,
  178. /**
  179. * @var {Function} renders the preview title contents
  180. * @param {DomElement} el the preview element
  181. * @param {Object} o extra options
  182. */
  183. title: function(el, o)
  184. {
  185. var title = 'Preview';
  186. if ('title' in o)
  187. {
  188. title = o.title;
  189. }
  190. el.innerHTML = title;
  191. },
  192. /**
  193. * @var {Function} renders the truncate warning contents
  194. * @param {DomElement} el the truncate element
  195. * @param {Integer} max
  196. * @param {Array|Object} data
  197. * @param {Object} o extra options
  198. */
  199. truncate: function(el, max, data, o)
  200. {
  201. el.innerHTML = 'Showing only first ' + max + ' rows, ' + cols(data) + ' in total';
  202. el.style.display = '';
  203. }
  204. }
  205. /**
  206. * Constructor
  207. *
  208. * @param {Array|Object} data
  209. * @param {Object} o extra options
  210. */
  211. function hh(data, o)
  212. {
  213. /* read options */
  214. o = o || {}
  215. for (var i in options)
  216. {
  217. if (i in o)
  218. {
  219. continue;
  220. }
  221. o[i] = options[i];
  222. }
  223. /* get the preview elements */
  224. var el = load(o.id);
  225. /* header title */
  226. var t = o; delete t.title;
  227. (typeof o.title === 'function')
  228. ? o.title(el.title, t)
  229. : options.title(el.title, t);
  230. /* resize preview */
  231. var windowResize = function(event)
  232. {
  233. resize(el);
  234. }
  235. window.addEventListener('resize', windowResize);
  236. /* close button */
  237. const scrollReset = {top: 0, left: 0, behavior: 'auto'};
  238. var scrollTo = scrollReset;
  239. el.close.addEventListener('click', function (event)
  240. {
  241. document.body.classList.remove('heiho-body');
  242. document.body.removeChild(el.shell);
  243. window.removeEventListener('resize', windowResize);
  244. document.body.scrollTo( scrollTo );
  245. scrollTo = scrollReset;
  246. });
  247. var columns = cols(data);
  248. /* preview thead */
  249. el.thead.innerHTML = '';
  250. for (var i = 0; i <= columns; i++)
  251. {
  252. var th = document.createElement('th');
  253. th.innerHTML = label(i);
  254. el.thead.appendChild(th);
  255. }
  256. /* preview grid rows */
  257. el.tbody.innerHTML = '';
  258. el.truncate.innerHTML = '';
  259. var rows = 0;
  260. var header = [];
  261. for (var i in data)
  262. {
  263. if (o.max > 0 && ++rows > o.max)
  264. {
  265. (typeof o.truncate === 'function')
  266. ? o.truncate(el.truncate, o.max, data, o)
  267. : options.truncate(el.truncate, o.max, data, o);
  268. break;
  269. }
  270. if (1 === rows)
  271. {
  272. header = data[i];
  273. }
  274. var tr = document.createElement('tr');
  275. var td = document.createElement('td');
  276. td.innerHTML = rows;
  277. tr.appendChild(td);
  278. for (var j in data[i])
  279. {
  280. td = document.createElement('td');
  281. td.innerHTML = data[i][j];
  282. tr.appendChild(td);
  283. }
  284. /* pad missing columns */
  285. if (columns > tr.childNodes.length)
  286. {
  287. while (tr.childNodes.length <= columns)
  288. {
  289. td = document.createElement('td');
  290. tr.appendChild(td);
  291. }
  292. }
  293. el.tbody.appendChild(tr);
  294. }
  295. /* first row is a header or not */
  296. if (null === o.header)
  297. {
  298. o.header = true;
  299. var j = 0;
  300. for (var i in header)
  301. {
  302. j++;
  303. if (!header[i])
  304. {
  305. o.header = false; /* empty header column */
  306. break;
  307. }
  308. if (!isNaN( parseFloat(header[i]) ))
  309. {
  310. o.header = false; /* number in header */
  311. break;
  312. }
  313. }
  314. if (false !== o.header)
  315. {
  316. if (j < columns)
  317. {
  318. o.header = false; /* too short header row */
  319. }
  320. }
  321. }
  322. if (o.header)
  323. {
  324. el.tbody.firstChild.classList.add('heiho-header');
  325. }
  326. /* finally show the preview and hive everything else in the body */
  327. scrollTo.top = document.body.scrollTop;
  328. scrollTo.left = document.body.scrollLeft;
  329. document.body.classList.add('heiho-body');
  330. el.shell.style.display = '';
  331. resize(el);
  332. }
  333. var Heiho = hh;
  334. return Heiho;
  335. });