* Groups given highlights by timestamp. * @param {Array} highlights * @returns {Array} Grouped highlights.
(highlights)
| 149 | * @returns {Array} Grouped highlights. |
| 150 | */ |
| 151 | function groupHighlights(highlights) { |
| 152 | var order = [], |
| 153 | chunks = {}, |
| 154 | grouped = []; |
| 155 | |
| 156 | highlights.forEach(function (hl) { |
| 157 | var timestamp = hl.getAttribute(TIMESTAMP_ATTR); |
| 158 | |
| 159 | if (typeof chunks[timestamp] === 'undefined') { |
| 160 | chunks[timestamp] = []; |
| 161 | order.push(timestamp); |
| 162 | } |
| 163 | |
| 164 | chunks[timestamp].push(hl); |
| 165 | }); |
| 166 | |
| 167 | order.forEach(function (timestamp) { |
| 168 | var group = chunks[timestamp]; |
| 169 | |
| 170 | grouped.push({ |
| 171 | chunks: group, |
| 172 | timestamp: timestamp, |
| 173 | toString: function () { |
| 174 | return group.map(function (h) { |
| 175 | return h.textContent; |
| 176 | }).join(''); |
| 177 | } |
| 178 | }); |
| 179 | }); |
| 180 | |
| 181 | return grouped; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Utility functions to make DOM manipulation easier. |