Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1.  
  2. // fg and bg are HTML colors, bg can (and usually will) be null
  3. // fg can be 'reset', which resets the colors back to default
  4. function linechunk_color(fg, bg) {
  5. var chunk = linechunk_base();
  6. chunk._fg = fg;
  7. chunk._bg = bg;
  8.  
  9. chunk.type = function() { return 'color'; }
  10. chunk.formatted = function(chunks) {
  11. if (this._fg === 'reset') {
  12. var res = chunks.suffix;
  13. chunks.suffix = '';
  14. return res;
  15. }
  16.  
  17. var style = '';
  18. if (this._fg != null) style += 'color: ' + this._fg + '; ';
  19. if (this._bg != null) style += 'background-color: ' + this._bg + ' ';
  20. if (style == '') return '';
  21. var res = '';
  22. // close previous color tags
  23. // IMPORTANT: if we use span suffix tags for anything else, this will fail!
  24. // Disabling this, it's causing weird problems
  25. // while (chunks.suffix.substr(0, 7) === '</span>') {
  26. // res += '</span>';
  27. // chunks.suffix = chunks.suffix.substr(7);
  28. // }
  29. chunks.suffix += '</span>';
  30. return res + '<span style="' + style + '">';
  31. }
  32. chunk.update_state = function(state) {
  33. state.fg = this._fg;
  34. if (this._fg == 'reset')
  35. delete state.bg;
  36. else
  37. state.bg = this._bg;
  38. return state;
  39. }
  40. return chunk;
  41. }
  42.  
  43. function linechunk_mxp_send(color, commands, text) {
  44. if (client.reverted) {
  45. if (color == '#FFFFFF') color = '#000000';
  46. if (color == '#C0C0C0') color = '#333333';
  47. }
  48.  
  49. var chunk = linechunk_text(text, false);
  50. chunk._color = color;
  51. chunk._commands = commands;
  52. chunk.type = function() { return 'link'; }
  53. chunk.formatted = function(chunks) {
  54. var res = '<a class="mxp_send" href="' + this._commands + '"';
  55. if (this._color) res += ' style="color: ' + this._color + '"';
  56. res += '>' + this._txt + '</a>';
  57. return res;
  58. }
  59. return chunk;
  60. }
  61.  
  62. function linechunks_create(chunks) {
  63. var res = {};
  64. res.chunks = chunks;
  65.  
  66. // helper function for colorize / replace. Ensures that there exists no chunk that spans this position.
  67. // Chunks starting -at- this position are fine. Chunks -ending- at this position are not (last letter will be split off).
  68. res.split_chunk_at_pos = function(pos)
  69. {
  70. if (pos <= 0) return; // nothing to do
  71.  
  72. var at = 0;
  73. for (var idx = 0; idx < this.chunks.length; ++idx) {
  74. // if we got exactly to the desired position, there is no need to split anything; bail out
  75. if (at == pos) break;
  76. var len = this.chunks[idx].length();
  77. if (at + len <= pos) {
  78. at += len;
  79. continue;
  80. }
  81.  
  82. // If we got here, at < pos and at + len > pos, meaning we need to split
  83. var splitlen = pos - at;
  84. var chunk2 = $.extend(true, {}, this.chunks[idx]); // create a deep copy of the chunk
  85. this.chunks[idx].resize(0, splitlen);
  86. chunk2.resize(splitlen, len);
  87. this.chunks.splice(idx + 1, 0, chunk2); // insert chunk2 after the current chunk
  88. break;
  89. }
  90. }
  91.  
  92.  
  93. res.colorize = function(start, end, color, bgcolor) {
  94. if ((this.chunks == null) || (this.chunks.length == 0))
  95. return;
  96. if (end < start) end = start;
  97. // ensure that no chunk spans the thresholds (simplifies the logic)
  98. this.split_chunk_at_pos(start);
  99. if (end != start) this.split_chunk_at_pos(end);
  100.  
  101. // grab all the chunks between start and end, except colorization ones
  102. var chunks = [];
  103. chunks.push(linechunk_color(color, bgcolor));
  104.  
  105. var pos = 0;
  106. var copying = false;
  107. for (var idx = 0; idx < this.chunks.length; ++idx) {
  108. if (pos >= end) break;
  109. if (pos >= start) copying = true;
  110. if (copying && this.chunks[idx].type() != 'color') chunks.push(this.chunks[idx]);
  111. var len = this.chunks[idx].length();
  112. pos += len;
  113. }
  114.  
  115. return this.replace_with_linechunks(start, end, chunks);
  116. }
  117. res.replace = function(start, end, replacement, color, bgcolor) {
  118. var chunks = [];
  119. if ((replacement != null) && replacement.length) {
  120. if ((color != undefined) || (bgcolor != undefined))
  121. chunks.push(linechunk_color(color, bgcolor));
  122. chunks.push(linechunk_text(replacement, true));
  123. }
  124. return this.replace_with_linechunks(start, end, chunks);
  125. }
  126. res.linkify = function(start, end, color, link_command, link_text) {
  127. if (!link_command) return;
  128. if (!link_text)
  129. link_text = this.text().substr(start, end - start + 1);
  130. var chunks = [];
  131. chunks.push(linechunk_mxp_send(color, link_command, link_text));
  132. return this.replace_with_linechunks(start, end, chunks);
  133. }
  134.  
  135.  
  136. // start = first index to replace, end = first index to -not- replace (i.e. end-start = length to replace)
  137. res.replace_with_linechunks = function(start, end, linechunks)
  138. {
  139. if ((this.chunks == null) || (this.chunks.length == 0)) {
  140. this.chunks = linechunks;
  141. return;
  142. }
  143. if (end < start) end = start;
  144. // ensure that no chunk spans the thresholds (simplifies the logic)
  145. this.split_chunk_at_pos(start);
  146. if (end != start) this.split_chunk_at_pos(end);
  147.  
  148. // remove old chunks, remember state at the end (so that colors match)
  149. var pos = 0;
  150. var removeidx = -1;
  151. var removecount = 0;
  152. var state = {};
  153. var removing = false;
  154. for (var idx = 0; idx < this.chunks.length; ++idx) {
  155. if (pos >= end) break;
  156. if ((pos >= start) && (removeidx < 0)) removeidx = idx;
  157. if (removeidx >= 0) removecount += 1;
  158.  
  159. var len = this.chunks[idx].length();
  160. pos += len;
  161. state = this.chunks[idx].update_state(state);
  162. }
  163. if (removeidx < 0) removeidx = 0;
  164.  
  165. linechunks = linechunks || []; // linechunks must exist, as we'll be adding the new state
  166. var state_chunks = client.apply_line_state(state);
  167. linechunks.push(linechunk_color('reset', null)); // reset the color -- fixes background color leakage
  168. for (var s = 0; s < state_chunks.length; ++s)
  169. linechunks.push(state_chunks[s]);
  170. // this calls splice with (removeidx, removecount, (contents of linechunks)) as params
  171. Array.prototype.splice.apply(this.chunks, [removeidx, removecount].concat(linechunks));
  172. }
  173.  
  174. res.remove = function(start, end) {
  175. return this.replace_with_linechunks(startd, end, null);
  176. }
  177. res.formatted = function() {
  178. var res = '';
  179. this.prefix = '';
  180. this.suffix = '';
  181. for (var i = 0; i < this.chunks.length; ++i)
  182. res += this.chunks[i].formatted(this);
  183. return this.prefix + res + this.suffix;
  184. }
  185. res.text = function() {
  186. var res = '';
  187. for (var i = 0; i < this.chunks.length; ++i)
  188. res += this.chunks[i].text(this);
  189. return res;
  190. }
  191.  
  192. return res;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement