table-test.js (3164B)
1 /* License CC0 */ 2 3 function htmlSpecialChars(s) { 4 return s 5 .replace(/&/g, '&') 6 .replace(/</g, '<') 7 .replace(/>/g, '>') 8 .replace(/"/g, '"') 9 } 10 11 var test_customization = { 12 id_prefix: 'mytable', 13 content_to_html: function (content, yx) { 14 var ta = document.createElement('textarea'); 15 ta.innerHTML = htmlSpecialChars(content); 16 return ta; 17 }, 18 html_to_content: function (elem) { 19 return elem.getElementsByTagName('textarea')[0].value 20 }, 21 merge_contents: function (dir, original, discarded) { 22 var cells = [original, ...discarded]; 23 switch (dir) { 24 case "l": return cells.reverse().join(' '); break; 25 case "u": return cells.reverse().join(' '); break; 26 case "r": return cells.join(' '); break; 27 case "d": return cells.join(' '); break; 28 default: throw "invalid direction;" 29 } 30 31 }, 32 postprocess: function(el) { 33 var ta = el.getElementsByTagName('textarea')[0]; 34 if (ta) { ta.style.height = el.clientHeight + 'px'; } 35 }, 36 init: yx=>""+yx.x+"-"+yx.y, 37 init_new_column: yx=>"new col "+yx.x+"-"+yx.y, 38 init_new_row: yx=>"new row "+yx.x+"-"+yx.y, 39 deep_copy_content: function (content) { /* deep copy of string */ return content; }, 40 } 41 42 function table_test( 43 { 44 // protected 45 make_model, 46 delete_column, 47 delete_row, 48 insert_column, 49 insert_row, 50 fuse_left, 51 fuse_up, 52 fuse_right, 53 fuse_down, 54 create_state_from_mod, 55 // public 56 create_state, 57 serialize, 58 deserialize, 59 update_state 60 } 61 ) { 62 var moad; 63 moad = make_model(6,6); 64 moad = delete_column(moad, 1, false); 65 moad = delete_column(moad, 1, false); 66 moad = delete_row(moad, 1, false); 67 moad = insert_column(moad, 1, yx => 'new col ' + yx.x + "-" + yx.y); 68 moad = insert_row(moad, 1, yx => 'new row ' + yx.x + "-" + yx.y); 69 moad = fuse_right(moad, 2, 2); 70 moad = fuse_right(moad, 2, 2); 71 moad = fuse_down(moad, 2, 2); 72 moad = fuse_down(moad, 4, 2); 73 moad = fuse_down(moad, 4, 3); 74 moad = fuse_down(moad, 2, 2); 75 moad = fuse_down(moad, 4, 4); 76 moad = fuse_down(moad, 2, 2); 77 moad = fuse_down(moad, 2, 2); 78 moad = insert_row(moad, 6, yx => 'new bot row ' + yx.x + "-" + yx.y); 79 moad = fuse_left(moad, 2, 2); 80 moad = fuse_up(moad, 2, 2); 81 moad = fuse_up(moad, 2, 1); 82 moad = insert_column(moad, 1, yx => 'new col ' + yx.x + "-" + yx.y); 83 moad = insert_column(moad, 3, yx => 'new col ' + yx.x + "-" + yx.y); 84 moad = insert_row(moad, 6, yx => 'new row ' + yx.x + "-" + yx.y); 85 86 moad = insert_column(moad, 8, yx => 'new col ' + yx.x + "-" + yx.y); 87 moad = fuse_right(moad, 0, 7); 88 moad = fuse_right(moad, 6, 7); 89 moad = fuse_right(moad, 7, 7); 90 moad = fuse_right(moad, 1, 2); 91 92 var state = create_state_from_mod(moad); 93 94 update_state(state, function(mod) { return mod; }, [], false, false, true); 95 } 96 97 table_test(table_js(test_customization));