jsmind.js 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027
  1. /*
  2. * Released under BSD License
  3. * Copyright (c) 2014-2021 hizzgdev@163.com
  4. *
  5. * Project Home:
  6. * https://github.com/hizzgdev/jsmind/
  7. */
  8. ; (function ($w) {
  9. 'use strict';
  10. // set 'jsMind' as the library name.
  11. // __name__ should be a const value, Never try to change it easily.
  12. var __name__ = 'jsMind';
  13. // library version
  14. var __version__ = '0.4.6';
  15. // author
  16. var __author__ = 'hizzgdev@163.com';
  17. // an noop function define
  18. var _noop = function () { };
  19. var logger = (typeof console === 'undefined') ? {
  20. log: _noop, debug: _noop, error: _noop, warn: _noop, info: _noop
  21. } : console;
  22. // check global variables
  23. if (typeof module === 'undefined' || !module.exports) {
  24. if (typeof $w[__name__] != 'undefined') {
  25. logger.log(__name__ + ' has been already exist.');
  26. return;
  27. }
  28. }
  29. // shortcut of methods in dom
  30. var $d = $w.document;
  31. var $g = function (id) { return $d.getElementById(id); };
  32. var $c = function (tag) { return $d.createElement(tag); };
  33. var $t = function (n, t) { if (n.hasChildNodes()) { n.firstChild.nodeValue = t; } else { n.appendChild($d.createTextNode(t)); } };
  34. var $h = function (n, t) {
  35. if (t instanceof HTMLElement) {
  36. n.innerHTML = '';
  37. n.appendChild(t);
  38. } else {
  39. n.innerHTML = t;
  40. }
  41. };
  42. // detect isElement
  43. var $i = function (el) { return !!el && (typeof el === 'object') && (el.nodeType === 1) && (typeof el.style === 'object') && (typeof el.ownerDocument === 'object'); };
  44. if (typeof String.prototype.startsWith != 'function') { String.prototype.startsWith = function (p) { return this.slice(0, p.length) === p; }; }
  45. var DEFAULT_OPTIONS = {
  46. container: '', // id of the container
  47. editable: false, // you can change it in your options
  48. theme: null,
  49. mode: 'full', // full or side
  50. support_html: true,
  51. view: {
  52. engine: 'canvas',
  53. hmargin: 100,
  54. vmargin: 50,
  55. line_width: 2,
  56. line_color: '#555'
  57. },
  58. layout: {
  59. hspace: 30,
  60. vspace: 20,
  61. pspace: 13
  62. },
  63. default_event_handle: {
  64. enable_mousedown_handle: true,
  65. enable_click_handle: true,
  66. enable_dblclick_handle: true
  67. },
  68. shortcut: {
  69. enable: true,
  70. handles: {
  71. },
  72. mapping: {
  73. addchild: 45, // Insert
  74. addbrother: 13, // Enter
  75. editnode: 113,// F2
  76. delnode: 46, // Delete
  77. toggle: 32, // Space
  78. left: 37, // Left
  79. up: 38, // Up
  80. right: 39, // Right
  81. down: 40, // Down
  82. }
  83. },
  84. };
  85. // core object
  86. var jm = function (options) {
  87. jm.current = this;
  88. this.version = __version__;
  89. var opts = {};
  90. jm.util.json.merge(opts, DEFAULT_OPTIONS);
  91. jm.util.json.merge(opts, options);
  92. if (!opts.container) {
  93. logger.error('the options.container should not be null or empty.');
  94. return;
  95. }
  96. this.options = opts;
  97. this.inited = false;
  98. this.mind = null;
  99. this.event_handles = [];
  100. this.init();
  101. };
  102. // ============= static object =============================================
  103. jm.direction = { left: -1, center: 0, right: 1 };
  104. jm.event_type = { show: 1, resize: 2, edit: 3, select: 4 };
  105. jm.key = { meta: 1 << 13, ctrl: 1 << 12, alt: 1 << 11, shift: 1 << 10 };
  106. jm.node = function (sId, iIndex, sTopic, oData, bIsRoot, oParent, eDirection, bExpanded) {
  107. if (!sId) { logger.error('invalid nodeid'); return; }
  108. if (typeof iIndex != 'number') { logger.error('invalid node index'); return; }
  109. if (typeof bExpanded === 'undefined') { bExpanded = true; }
  110. this.id = sId;
  111. this.index = iIndex;
  112. this.topic = sTopic;
  113. this.data = oData || {};
  114. this.isroot = bIsRoot;
  115. this.parent = oParent;
  116. this.direction = eDirection;
  117. this.expanded = !!bExpanded;
  118. this.children = [];
  119. this._data = {};
  120. };
  121. jm.node.compare = function (node1, node2) {
  122. // '-1' is alwary the last
  123. var r = 0;
  124. var i1 = node1.index;
  125. var i2 = node2.index;
  126. if (i1 >= 0 && i2 >= 0) {
  127. r = i1 - i2;
  128. } else if (i1 == -1 && i2 == -1) {
  129. r = 0;
  130. } else if (i1 == -1) {
  131. r = 1;
  132. } else if (i2 == -1) {
  133. r = -1;
  134. } else {
  135. r = 0;
  136. }
  137. //logger.debug(i1+' <> '+i2+' = '+r);
  138. return r;
  139. };
  140. jm.node.inherited = function (pnode, node) {
  141. if (!!pnode && !!node) {
  142. if (pnode.id === node.id) {
  143. return true;
  144. }
  145. if (pnode.isroot) {
  146. return true;
  147. }
  148. var pid = pnode.id;
  149. var p = node;
  150. while (!p.isroot) {
  151. p = p.parent;
  152. if (p.id === pid) {
  153. return true;
  154. }
  155. }
  156. }
  157. return false;
  158. };
  159. jm.node.prototype = {
  160. get_location: function () {
  161. var vd = this._data.view;
  162. return {
  163. x: vd.abs_x,
  164. y: vd.abs_y
  165. };
  166. },
  167. get_size: function () {
  168. var vd = this._data.view;
  169. return {
  170. w: vd.width,
  171. h: vd.height
  172. }
  173. }
  174. };
  175. jm.mind = function () {
  176. this.name = null;
  177. this.author = null;
  178. this.version = null;
  179. this.root = null;
  180. this.selected = null;
  181. this.nodes = {};
  182. };
  183. jm.mind.prototype = {
  184. get_node: function (nodeid) {
  185. if (nodeid in this.nodes) {
  186. return this.nodes[nodeid];
  187. } else {
  188. logger.warn('the node[id=' + nodeid + '] can not be found');
  189. return null;
  190. }
  191. },
  192. set_root: function (nodeid, topic, data) {
  193. if (this.root == null) {
  194. this.root = new jm.node(nodeid, 0, topic, data, true);
  195. this._put_node(this.root);
  196. } else {
  197. logger.error('root node is already exist');
  198. }
  199. },
  200. add_node: function (parent_node, nodeid, topic, data, idx, direction, expanded) {
  201. if (!jm.util.is_node(parent_node)) {
  202. var the_parent_node = this.get_node(parent_node);
  203. if (!the_parent_node) {
  204. logger.error('the parent_node[id=' + parent_node + '] can not be found.');
  205. return null;
  206. } else {
  207. return this.add_node(the_parent_node, nodeid, topic, data, idx, direction, expanded);
  208. }
  209. }
  210. var nodeindex = idx || -1;
  211. var node = null;
  212. if (parent_node.isroot) {
  213. var d = jm.direction.right;
  214. if (isNaN(direction)) {
  215. var children = parent_node.children;
  216. var children_len = children.length;
  217. var r = 0;
  218. for (var i = 0; i < children_len; i++) { if (children[i].direction === jm.direction.left) { r--; } else { r++; } }
  219. d = (children_len > 1 && r > 0) ? jm.direction.left : jm.direction.right;
  220. } else {
  221. d = (direction != jm.direction.left) ? jm.direction.right : jm.direction.left;
  222. }
  223. node = new jm.node(nodeid, nodeindex, topic, data, false, parent_node, d, expanded);
  224. } else {
  225. node = new jm.node(nodeid, nodeindex, topic, data, false, parent_node, parent_node.direction, expanded);
  226. }
  227. if (this._put_node(node)) {
  228. parent_node.children.push(node);
  229. this._reindex(parent_node);
  230. } else {
  231. logger.error('fail, the nodeid \'' + node.id + '\' has been already exist.');
  232. node = null;
  233. }
  234. return node;
  235. },
  236. insert_node_before: function (node_before, nodeid, topic, data) {
  237. if (!jm.util.is_node(node_before)) {
  238. var the_node_before = this.get_node(node_before);
  239. if (!the_node_before) {
  240. logger.error('the node_before[id=' + node_before + '] can not be found.');
  241. return null;
  242. } else {
  243. return this.insert_node_before(the_node_before, nodeid, topic, data);
  244. }
  245. }
  246. var node_index = node_before.index - 0.5;
  247. return this.add_node(node_before.parent, nodeid, topic, data, node_index);
  248. },
  249. get_node_before: function (node) {
  250. if (!jm.util.is_node(node)) {
  251. var the_node = this.get_node(node);
  252. if (!the_node) {
  253. logger.error('the node[id=' + node + '] can not be found.');
  254. return null;
  255. } else {
  256. return this.get_node_before(the_node);
  257. }
  258. }
  259. if (node.isroot) { return null; }
  260. var idx = node.index - 2;
  261. if (idx >= 0) {
  262. return node.parent.children[idx];
  263. } else {
  264. return null;
  265. }
  266. },
  267. insert_node_after: function (node_after, nodeid, topic, data) {
  268. if (!jm.util.is_node(node_after)) {
  269. var the_node_after = this.get_node(node_after);
  270. if (!the_node_after) {
  271. logger.error('the node_after[id=' + node_after + '] can not be found.');
  272. return null;
  273. } else {
  274. return this.insert_node_after(the_node_after, nodeid, topic, data);
  275. }
  276. }
  277. var node_index = node_after.index + 0.5;
  278. return this.add_node(node_after.parent, nodeid, topic, data, node_index);
  279. },
  280. get_node_after: function (node) {
  281. if (!jm.util.is_node(node)) {
  282. var the_node = this.get_node(node);
  283. if (!the_node) {
  284. logger.error('the node[id=' + node + '] can not be found.');
  285. return null;
  286. } else {
  287. return this.get_node_after(the_node);
  288. }
  289. }
  290. if (node.isroot) { return null; }
  291. var idx = node.index;
  292. var brothers = node.parent.children;
  293. if (brothers.length >= idx) {
  294. return node.parent.children[idx];
  295. } else {
  296. return null;
  297. }
  298. },
  299. move_node: function (node, beforeid, parentid, direction) {
  300. if (!jm.util.is_node(node)) {
  301. var the_node = this.get_node(node);
  302. if (!the_node) {
  303. logger.error('the node[id=' + node + '] can not be found.');
  304. return null;
  305. } else {
  306. return this.move_node(the_node, beforeid, parentid, direction);
  307. }
  308. }
  309. if (!parentid) {
  310. parentid = node.parent.id;
  311. }
  312. return this._move_node(node, beforeid, parentid, direction);
  313. },
  314. _flow_node_direction: function (node, direction) {
  315. if (typeof direction === 'undefined') {
  316. direction = node.direction;
  317. } else {
  318. node.direction = direction;
  319. }
  320. var len = node.children.length;
  321. while (len--) {
  322. this._flow_node_direction(node.children[len], direction);
  323. }
  324. },
  325. _move_node_internal: function (node, beforeid) {
  326. if (!!node && !!beforeid) {
  327. if (beforeid == '_last_') {
  328. node.index = -1;
  329. this._reindex(node.parent);
  330. } else if (beforeid == '_first_') {
  331. node.index = 0;
  332. this._reindex(node.parent);
  333. } else {
  334. var node_before = (!!beforeid) ? this.get_node(beforeid) : null;
  335. if (node_before != null && node_before.parent != null && node_before.parent.id == node.parent.id) {
  336. node.index = node_before.index - 0.5;
  337. this._reindex(node.parent);
  338. }
  339. }
  340. }
  341. return node;
  342. },
  343. _move_node: function (node, beforeid, parentid, direction) {
  344. if (!!node && !!parentid) {
  345. if (node.parent.id != parentid) {
  346. // remove from parent's children
  347. var sibling = node.parent.children;
  348. var si = sibling.length;
  349. while (si--) {
  350. if (sibling[si].id == node.id) {
  351. sibling.splice(si, 1);
  352. break;
  353. }
  354. }
  355. node.parent = this.get_node(parentid);
  356. node.parent.children.push(node);
  357. }
  358. if (node.parent.isroot) {
  359. if (direction == jm.direction.left) {
  360. node.direction = direction;
  361. } else {
  362. node.direction = jm.direction.right;
  363. }
  364. } else {
  365. node.direction = node.parent.direction;
  366. }
  367. this._move_node_internal(node, beforeid);
  368. this._flow_node_direction(node);
  369. }
  370. return node;
  371. },
  372. remove_node: function (node) {
  373. if (!jm.util.is_node(node)) {
  374. var the_node = this.get_node(node);
  375. if (!the_node) {
  376. logger.error('the node[id=' + node + '] can not be found.');
  377. return false;
  378. } else {
  379. return this.remove_node(the_node);
  380. }
  381. }
  382. if (!node) {
  383. logger.error('fail, the node can not be found');
  384. return false;
  385. }
  386. if (node.isroot) {
  387. logger.error('fail, can not remove root node');
  388. return false;
  389. }
  390. if (this.selected != null && this.selected.id == node.id) {
  391. this.selected = null;
  392. }
  393. // clean all subordinate nodes
  394. var children = node.children;
  395. var ci = children.length;
  396. while (ci--) {
  397. this.remove_node(children[ci]);
  398. }
  399. // clean all children
  400. children.length = 0;
  401. // remove from parent's children
  402. var sibling = node.parent.children;
  403. var si = sibling.length;
  404. while (si--) {
  405. if (sibling[si].id == node.id) {
  406. sibling.splice(si, 1);
  407. break;
  408. }
  409. }
  410. // remove from global nodes
  411. delete this.nodes[node.id];
  412. // clean all properties
  413. for (var k in node) {
  414. delete node[k];
  415. }
  416. // remove it's self
  417. node = null;
  418. //delete node;
  419. return true;
  420. },
  421. _put_node: function (node) {
  422. if (node.id in this.nodes) {
  423. logger.warn('the nodeid \'' + node.id + '\' has been already exist.');
  424. return false;
  425. } else {
  426. this.nodes[node.id] = node;
  427. return true;
  428. }
  429. },
  430. _reindex: function (node) {
  431. if (node instanceof jm.node) {
  432. node.children.sort(jm.node.compare);
  433. for (var i = 0; i < node.children.length; i++) {
  434. node.children[i].index = i + 1;
  435. }
  436. }
  437. },
  438. };
  439. jm.format = {
  440. node_tree: {
  441. example: {
  442. "meta": {
  443. "name": __name__,
  444. "author": __author__,
  445. "version": __version__
  446. },
  447. "format": "node_tree",
  448. "data": { "id": "root", "topic": "jsMind Example" }
  449. },
  450. get_mind: function (source) {
  451. var df = jm.format.node_tree;
  452. var mind = new jm.mind();
  453. mind.name = source.meta.name;
  454. mind.author = source.meta.author;
  455. mind.version = source.meta.version;
  456. df._parse(mind, source.data);
  457. return mind;
  458. },
  459. get_data: function (mind) {
  460. var df = jm.format.node_tree;
  461. var json = {};
  462. json.meta = {
  463. name: mind.name,
  464. author: mind.author,
  465. version: mind.version
  466. };
  467. json.format = 'node_tree';
  468. json.data = df._buildnode(mind.root);
  469. return json;
  470. },
  471. _parse: function (mind, node_root) {
  472. var df = jm.format.node_tree;
  473. var data = df._extract_data(node_root);
  474. mind.set_root(node_root.id, node_root.topic, data);
  475. if ('children' in node_root) {
  476. var children = node_root.children;
  477. for (var i = 0; i < children.length; i++) {
  478. df._extract_subnode(mind, mind.root, children[i]);
  479. }
  480. }
  481. },
  482. _extract_data: function (node_json) {
  483. var data = {};
  484. for (var k in node_json) {
  485. if (k == 'id' || k == 'topic' || k == 'children' || k == 'direction' || k == 'expanded') {
  486. continue;
  487. }
  488. data[k] = node_json[k];
  489. }
  490. return data;
  491. },
  492. _extract_subnode: function (mind, node_parent, node_json) {
  493. var df = jm.format.node_tree;
  494. var data = df._extract_data(node_json);
  495. var d = null;
  496. if (node_parent.isroot) {
  497. d = node_json.direction == 'left' ? jm.direction.left : jm.direction.right;
  498. }
  499. var node = mind.add_node(node_parent, node_json.id, node_json.topic, data, null, d, node_json.expanded);
  500. if (!!node_json['children']) {
  501. var children = node_json.children;
  502. for (var i = 0; i < children.length; i++) {
  503. df._extract_subnode(mind, node, children[i]);
  504. }
  505. }
  506. },
  507. _buildnode: function (node) {
  508. var df = jm.format.node_tree;
  509. if (!(node instanceof jm.node)) { return; }
  510. var o = {
  511. id: node.id,
  512. topic: node.topic,
  513. expanded: node.expanded
  514. };
  515. if (!!node.parent && node.parent.isroot) {
  516. o.direction = node.direction == jm.direction.left ? 'left' : 'right';
  517. }
  518. if (node.data != null) {
  519. var node_data = node.data;
  520. for (var k in node_data) {
  521. o[k] = node_data[k];
  522. }
  523. }
  524. var children = node.children;
  525. if (children.length > 0) {
  526. o.children = [];
  527. for (var i = 0; i < children.length; i++) {
  528. o.children.push(df._buildnode(children[i]));
  529. }
  530. }
  531. return o;
  532. }
  533. },
  534. node_array: {
  535. example: {
  536. "meta": {
  537. "name": __name__,
  538. "author": __author__,
  539. "version": __version__
  540. },
  541. "format": "node_array",
  542. "data": [
  543. { "id": "root", "topic": "jsMind Example", "isroot": true }
  544. ]
  545. },
  546. get_mind: function (source) {
  547. var df = jm.format.node_array;
  548. var mind = new jm.mind();
  549. mind.name = source.meta.name;
  550. mind.author = source.meta.author;
  551. mind.version = source.meta.version;
  552. df._parse(mind, source.data);
  553. return mind;
  554. },
  555. get_data: function (mind) {
  556. var df = jm.format.node_array;
  557. var json = {};
  558. json.meta = {
  559. name: mind.name,
  560. author: mind.author,
  561. version: mind.version
  562. };
  563. json.format = 'node_array';
  564. json.data = [];
  565. df._array(mind, json.data);
  566. return json;
  567. },
  568. _parse: function (mind, node_array) {
  569. var df = jm.format.node_array;
  570. var narray = node_array.slice(0);
  571. // reverse array for improving looping performance
  572. narray.reverse();
  573. var root_id = df._extract_root(mind, narray);
  574. if (!!root_id) {
  575. df._extract_subnode(mind, root_id, narray);
  576. } else {
  577. logger.error('root node can not be found');
  578. }
  579. },
  580. _extract_root: function (mind, node_array) {
  581. var df = jm.format.node_array;
  582. var i = node_array.length;
  583. while (i--) {
  584. if ('isroot' in node_array[i] && node_array[i].isroot) {
  585. var root_json = node_array[i];
  586. var data = df._extract_data(root_json);
  587. mind.set_root(root_json.id, root_json.topic, data);
  588. node_array.splice(i, 1);
  589. return root_json.id;
  590. }
  591. }
  592. return null;
  593. },
  594. _extract_subnode: function (mind, parentid, node_array) {
  595. var df = jm.format.node_array;
  596. var i = node_array.length;
  597. var node_json = null;
  598. var data = null;
  599. var extract_count = 0;
  600. while (i--) {
  601. node_json = node_array[i];
  602. if (node_json.parentid == parentid) {
  603. data = df._extract_data(node_json);
  604. var d = null;
  605. var node_direction = node_json.direction;
  606. if (!!node_direction) {
  607. d = node_direction == 'left' ? jm.direction.left : jm.direction.right;
  608. }
  609. mind.add_node(parentid, node_json.id, node_json.topic, data, null, d, node_json.expanded);
  610. node_array.splice(i, 1);
  611. extract_count++;
  612. var sub_extract_count = df._extract_subnode(mind, node_json.id, node_array);
  613. if (sub_extract_count > 0) {
  614. // reset loop index after extract subordinate node
  615. i = node_array.length;
  616. extract_count += sub_extract_count;
  617. }
  618. }
  619. }
  620. return extract_count;
  621. },
  622. _extract_data: function (node_json) {
  623. var data = {};
  624. for (var k in node_json) {
  625. if (k == 'id' || k == 'topic' || k == 'parentid' || k == 'isroot' || k == 'direction' || k == 'expanded') {
  626. continue;
  627. }
  628. data[k] = node_json[k];
  629. }
  630. return data;
  631. },
  632. _array: function (mind, node_array) {
  633. var df = jm.format.node_array;
  634. df._array_node(mind.root, node_array);
  635. },
  636. _array_node: function (node, node_array) {
  637. var df = jm.format.node_array;
  638. if (!(node instanceof jm.node)) { return; }
  639. var o = {
  640. id: node.id,
  641. topic: node.topic,
  642. expanded: node.expanded
  643. };
  644. if (!!node.parent) {
  645. o.parentid = node.parent.id;
  646. }
  647. if (node.isroot) {
  648. o.isroot = true;
  649. }
  650. if (!!node.parent && node.parent.isroot) {
  651. o.direction = node.direction == jm.direction.left ? 'left' : 'right';
  652. }
  653. if (node.data != null) {
  654. var node_data = node.data;
  655. for (var k in node_data) {
  656. o[k] = node_data[k];
  657. }
  658. }
  659. node_array.push(o);
  660. var ci = node.children.length;
  661. for (var i = 0; i < ci; i++) {
  662. df._array_node(node.children[i], node_array);
  663. }
  664. },
  665. },
  666. freemind: {
  667. example: {
  668. "meta": {
  669. "name": __name__,
  670. "author": __author__,
  671. "version": __version__
  672. },
  673. "format": "freemind",
  674. "data": "<map version=\"1.0.1\"><node ID=\"root\" TEXT=\"freemind Example\"/></map>"
  675. },
  676. get_mind: function (source) {
  677. var df = jm.format.freemind;
  678. var mind = new jm.mind();
  679. mind.name = source.meta.name;
  680. mind.author = source.meta.author;
  681. mind.version = source.meta.version;
  682. var xml = source.data;
  683. var xml_doc = df._parse_xml(xml);
  684. var xml_root = df._find_root(xml_doc);
  685. df._load_node(mind, null, xml_root);
  686. return mind;
  687. },
  688. get_data: function (mind) {
  689. var df = jm.format.freemind;
  690. var json = {};
  691. json.meta = {
  692. name: mind.name,
  693. author: mind.author,
  694. version: mind.version
  695. };
  696. json.format = 'freemind';
  697. var xmllines = [];
  698. xmllines.push('<map version=\"1.0.1\">');
  699. df._buildmap(mind.root, xmllines);
  700. xmllines.push('</map>');
  701. json.data = xmllines.join(' ');
  702. return json;
  703. },
  704. _parse_xml: function (xml) {
  705. var xml_doc = null;
  706. if (window.DOMParser) {
  707. var parser = new DOMParser();
  708. xml_doc = parser.parseFromString(xml, 'text/xml');
  709. } else { // Internet Explorer
  710. xml_doc = new ActiveXObject('Microsoft.XMLDOM');
  711. xml_doc.async = false;
  712. xml_doc.loadXML(xml);
  713. }
  714. return xml_doc;
  715. },
  716. _find_root: function (xml_doc) {
  717. var nodes = xml_doc.childNodes;
  718. var node = null;
  719. var root = null;
  720. var n = null;
  721. for (var i = 0; i < nodes.length; i++) {
  722. n = nodes[i];
  723. if (n.nodeType == 1 && n.tagName == 'map') {
  724. node = n;
  725. break;
  726. }
  727. }
  728. if (!!node) {
  729. var ns = node.childNodes;
  730. node = null;
  731. for (var i = 0; i < ns.length; i++) {
  732. n = ns[i];
  733. if (n.nodeType == 1 && n.tagName == 'node') {
  734. node = n;
  735. break;
  736. }
  737. }
  738. }
  739. return node;
  740. },
  741. _load_node: function (mind, parent_id, xml_node) {
  742. var df = jm.format.freemind;
  743. var node_id = xml_node.getAttribute('ID');
  744. var node_topic = xml_node.getAttribute('TEXT');
  745. // look for richcontent
  746. if (node_topic == null) {
  747. var topic_children = xml_node.childNodes;
  748. var topic_child = null;
  749. for (var i = 0; i < topic_children.length; i++) {
  750. topic_child = topic_children[i];
  751. //logger.debug(topic_child.tagName);
  752. if (topic_child.nodeType == 1 && topic_child.tagName === 'richcontent') {
  753. node_topic = topic_child.textContent;
  754. break;
  755. }
  756. }
  757. }
  758. var node_data = df._load_attributes(xml_node);
  759. var node_expanded = ('expanded' in node_data) ? (node_data.expanded == 'true') : true;
  760. delete node_data.expanded;
  761. var node_position = xml_node.getAttribute('POSITION');
  762. var node_direction = null;
  763. if (!!node_position) {
  764. node_direction = node_position == 'left' ? jm.direction.left : jm.direction.right;
  765. }
  766. //logger.debug(node_position +':'+ node_direction);
  767. if (!!parent_id) {
  768. mind.add_node(parent_id, node_id, node_topic, node_data, null, node_direction, node_expanded);
  769. } else {
  770. mind.set_root(node_id, node_topic, node_data);
  771. }
  772. var children = xml_node.childNodes;
  773. var child = null;
  774. for (var i = 0; i < children.length; i++) {
  775. child = children[i];
  776. if (child.nodeType == 1 && child.tagName == 'node') {
  777. df._load_node(mind, node_id, child);
  778. }
  779. }
  780. },
  781. _load_attributes: function (xml_node) {
  782. var children = xml_node.childNodes;
  783. var attr = null;
  784. var attr_data = {};
  785. for (var i = 0; i < children.length; i++) {
  786. attr = children[i];
  787. if (attr.nodeType == 1 && attr.tagName === 'attribute') {
  788. attr_data[attr.getAttribute('NAME')] = attr.getAttribute('VALUE');
  789. }
  790. }
  791. return attr_data;
  792. },
  793. _buildmap: function (node, xmllines) {
  794. var df = jm.format.freemind;
  795. var pos = null;
  796. if (!!node.parent && node.parent.isroot) {
  797. pos = node.direction === jm.direction.left ? 'left' : 'right';
  798. }
  799. xmllines.push('<node');
  800. xmllines.push('ID=\"' + node.id + '\"');
  801. if (!!pos) {
  802. xmllines.push('POSITION=\"' + pos + '\"');
  803. }
  804. xmllines.push('TEXT=\"' + node.topic + '\">');
  805. // store expanded status as an attribute
  806. xmllines.push('<attribute NAME=\"expanded\" VALUE=\"' + node.expanded + '\"/>');
  807. // for attributes
  808. var node_data = node.data;
  809. if (node_data != null) {
  810. for (var k in node_data) {
  811. xmllines.push('<attribute NAME=\"' + k + '\" VALUE=\"' + node_data[k] + '\"/>');
  812. }
  813. }
  814. // for children
  815. var children = node.children;
  816. for (var i = 0; i < children.length; i++) {
  817. df._buildmap(children[i], xmllines);
  818. }
  819. xmllines.push('</node>');
  820. },
  821. },
  822. };
  823. // ============= utility object =============================================
  824. jm.util = {
  825. is_node: function (node) {
  826. return !!node && node instanceof jm.node;
  827. },
  828. ajax: {
  829. _xhr: function () {
  830. var xhr = null;
  831. if (window.XMLHttpRequest) {
  832. xhr = new XMLHttpRequest();
  833. } else {
  834. try {
  835. xhr = new ActiveXObject('Microsoft.XMLHTTP');
  836. } catch (e) { }
  837. }
  838. return xhr;
  839. },
  840. _eurl: function (url) {
  841. return encodeURIComponent(url);
  842. },
  843. request: function (url, param, method, callback, fail_callback) {
  844. var a = jm.util.ajax;
  845. var p = null;
  846. var tmp_param = [];
  847. for (var k in param) {
  848. tmp_param.push(a._eurl(k) + '=' + a._eurl(param[k]));
  849. }
  850. if (tmp_param.length > 0) {
  851. p = tmp_param.join('&');
  852. }
  853. var xhr = a._xhr();
  854. if (!xhr) { return; }
  855. xhr.onreadystatechange = function () {
  856. if (xhr.readyState == 4) {
  857. if (xhr.status == 200 || xhr.status == 0) {
  858. if (typeof callback === 'function') {
  859. var data = jm.util.json.string2json(xhr.responseText);
  860. if (data != null) {
  861. callback(data);
  862. } else {
  863. callback(xhr.responseText);
  864. }
  865. }
  866. } else {
  867. if (typeof fail_callback === 'function') {
  868. fail_callback(xhr);
  869. } else {
  870. logger.error('xhr request failed.', xhr);
  871. }
  872. }
  873. }
  874. }
  875. method = method || 'GET';
  876. xhr.open(method, url, true);
  877. xhr.setRequestHeader('If-Modified-Since', '0');
  878. if (method == 'POST') {
  879. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
  880. xhr.send(p);
  881. } else {
  882. xhr.send();
  883. }
  884. },
  885. get: function (url, callback) {
  886. return jm.util.ajax.request(url, {}, 'GET', callback);
  887. },
  888. post: function (url, param, callback) {
  889. return jm.util.ajax.request(url, param, 'POST', callback);
  890. }
  891. },
  892. dom: {
  893. //target,eventType,handler
  894. add_event: function (t, e, h) {
  895. if (!!t.addEventListener) {
  896. t.addEventListener(e, h, false);
  897. } else {
  898. t.attachEvent('on' + e, h);
  899. }
  900. }
  901. },
  902. file: {
  903. read: function (file_data, fn_callback) {
  904. var reader = new FileReader();
  905. reader.onload = function () {
  906. if (typeof fn_callback === 'function') {
  907. fn_callback(this.result, file_data.name);
  908. }
  909. };
  910. reader.readAsText(file_data);
  911. },
  912. save: function (file_data, type, name) {
  913. var blob;
  914. if (typeof $w.Blob === 'function') {
  915. blob = new Blob([file_data], { type: type });
  916. } else {
  917. var BlobBuilder = $w.BlobBuilder || $w.MozBlobBuilder || $w.WebKitBlobBuilder || $w.MSBlobBuilder;
  918. var bb = new BlobBuilder();
  919. bb.append(file_data);
  920. blob = bb.getBlob(type);
  921. }
  922. if (navigator.msSaveBlob) {
  923. navigator.msSaveBlob(blob, name);
  924. } else {
  925. var URL = $w.URL || $w.webkitURL;
  926. var bloburl = URL.createObjectURL(blob);
  927. var anchor = $c('a');
  928. if ('download' in anchor) {
  929. anchor.style.visibility = 'hidden';
  930. anchor.href = bloburl;
  931. anchor.download = name;
  932. $d.body.appendChild(anchor);
  933. var evt = $d.createEvent('MouseEvents');
  934. evt.initEvent('click', true, true);
  935. anchor.dispatchEvent(evt);
  936. $d.body.removeChild(anchor);
  937. } else {
  938. location.href = bloburl;
  939. }
  940. }
  941. }
  942. },
  943. json: {
  944. json2string: function (json) {
  945. if (!!JSON) {
  946. try {
  947. var json_str = JSON.stringify(json);
  948. return json_str;
  949. } catch (e) {
  950. logger.warn(e);
  951. logger.warn('can not convert to string');
  952. return null;
  953. }
  954. }
  955. },
  956. string2json: function (json_str) {
  957. if (!!JSON) {
  958. try {
  959. var json = JSON.parse(json_str);
  960. return json;
  961. } catch (e) {
  962. logger.warn(e);
  963. logger.warn('can not parse to json');
  964. return null;
  965. }
  966. }
  967. },
  968. merge: function (b, a) {
  969. for (var o in a) {
  970. if (o in b) {
  971. if (typeof b[o] === 'object' &&
  972. Object.prototype.toString.call(b[o]).toLowerCase() == '[object object]' &&
  973. !b[o].length) {
  974. jm.util.json.merge(b[o], a[o]);
  975. } else {
  976. b[o] = a[o];
  977. }
  978. } else {
  979. b[o] = a[o];
  980. }
  981. }
  982. return b;
  983. }
  984. },
  985. uuid: {
  986. newid: function () {
  987. return (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16);
  988. }
  989. },
  990. text: {
  991. is_empty: function (s) {
  992. if (!s) { return true; }
  993. return s.replace(/\s*/, '').length == 0;
  994. }
  995. }
  996. };
  997. jm.prototype = {
  998. init: function () {
  999. if (this.inited) { return; }
  1000. this.inited = true;
  1001. var opts = this.options;
  1002. var opts_layout = {
  1003. mode: opts.mode,
  1004. hspace: opts.layout.hspace,
  1005. vspace: opts.layout.vspace,
  1006. pspace: opts.layout.pspace
  1007. }
  1008. var opts_view = {
  1009. container: opts.container,
  1010. support_html: opts.support_html,
  1011. engine: opts.view.engine,
  1012. hmargin: opts.view.hmargin,
  1013. vmargin: opts.view.vmargin,
  1014. line_width: opts.view.line_width,
  1015. line_color: opts.view.line_color
  1016. };
  1017. // create instance of function provider
  1018. this.data = new jm.data_provider(this);
  1019. this.layout = new jm.layout_provider(this, opts_layout);
  1020. this.view = new jm.view_provider(this, opts_view);
  1021. this.shortcut = new jm.shortcut_provider(this, opts.shortcut);
  1022. this.data.init();
  1023. this.layout.init();
  1024. this.view.init();
  1025. this.shortcut.init();
  1026. this._event_bind();
  1027. jm.init_plugins(this);
  1028. },
  1029. enable_edit: function () {
  1030. this.options.editable = true;
  1031. },
  1032. disable_edit: function () {
  1033. this.options.editable = false;
  1034. },
  1035. // call enable_event_handle('dblclick')
  1036. // options are 'mousedown', 'click', 'dblclick'
  1037. enable_event_handle: function (event_handle) {
  1038. this.options.default_event_handle['enable_' + event_handle + '_handle'] = true;
  1039. },
  1040. // call disable_event_handle('dblclick')
  1041. // options are 'mousedown', 'click', 'dblclick'
  1042. disable_event_handle: function (event_handle) {
  1043. this.options.default_event_handle['enable_' + event_handle + '_handle'] = false;
  1044. },
  1045. get_editable: function () {
  1046. return this.options.editable;
  1047. },
  1048. set_theme: function (theme) {
  1049. var theme_old = this.options.theme;
  1050. this.options.theme = (!!theme) ? theme : null;
  1051. if (theme_old != this.options.theme) {
  1052. this.view.reset_theme();
  1053. this.view.reset_custom_style();
  1054. }
  1055. },
  1056. _event_bind: function () {
  1057. this.view.add_event(this, 'mousedown', this.mousedown_handle);
  1058. this.view.add_event(this, 'click', this.click_handle);
  1059. this.view.add_event(this, 'dblclick', this.dblclick_handle);
  1060. },
  1061. mousedown_handle: function (e) {
  1062. if (!this.options.default_event_handle['enable_mousedown_handle']) {
  1063. return;
  1064. }
  1065. var element = e.target || event.srcElement;
  1066. var nodeid = this.view.get_binded_nodeid(element);
  1067. if (!!nodeid) {
  1068. if (element.tagName.toLowerCase() == 'jmnode') {
  1069. this.select_node(nodeid);
  1070. }
  1071. } else {
  1072. this.select_clear();
  1073. }
  1074. },
  1075. click_handle: function (e) {
  1076. if (!this.options.default_event_handle['enable_click_handle']) {
  1077. return;
  1078. }
  1079. var element = e.target || event.srcElement;
  1080. var isexpander = this.view.is_expander(element);
  1081. if (isexpander) {
  1082. var nodeid = this.view.get_binded_nodeid(element);
  1083. if (!!nodeid) {
  1084. this.toggle_node(nodeid);
  1085. }
  1086. }
  1087. },
  1088. dblclick_handle: function (e) {
  1089. if (!this.options.default_event_handle['enable_dblclick_handle']) {
  1090. return;
  1091. }
  1092. if (this.get_editable()) {
  1093. var element = e.target || event.srcElement;
  1094. var nodeid = this.view.get_binded_nodeid(element);
  1095. if (!!nodeid) {
  1096. this.begin_edit(nodeid);
  1097. }
  1098. }
  1099. },
  1100. begin_edit: function (node) {
  1101. if (!jm.util.is_node(node)) {
  1102. var the_node = this.get_node(node);
  1103. if (!the_node) {
  1104. logger.error('the node[id=' + node + '] can not be found.');
  1105. return false;
  1106. } else {
  1107. return this.begin_edit(the_node);
  1108. }
  1109. }
  1110. if (this.get_editable()) {
  1111. this.view.edit_node_begin(node);
  1112. } else {
  1113. logger.error('fail, this mind map is not editable.');
  1114. return;
  1115. }
  1116. },
  1117. end_edit: function () {
  1118. this.view.edit_node_end();
  1119. },
  1120. toggle_node: function (node) {
  1121. if (!jm.util.is_node(node)) {
  1122. var the_node = this.get_node(node);
  1123. if (!the_node) {
  1124. logger.error('the node[id=' + node + '] can not be found.');
  1125. return;
  1126. } else {
  1127. return this.toggle_node(the_node);
  1128. }
  1129. }
  1130. if (node.isroot) { return; }
  1131. this.view.save_location(node);
  1132. this.layout.toggle_node(node);
  1133. this.view.relayout();
  1134. this.view.restore_location(node);
  1135. },
  1136. expand_node: function (node) {
  1137. if (!jm.util.is_node(node)) {
  1138. var the_node = this.get_node(node);
  1139. if (!the_node) {
  1140. logger.error('the node[id=' + node + '] can not be found.');
  1141. return;
  1142. } else {
  1143. return this.expand_node(the_node);
  1144. }
  1145. }
  1146. if (node.isroot) { return; }
  1147. this.view.save_location(node);
  1148. this.layout.expand_node(node);
  1149. this.view.relayout();
  1150. this.view.restore_location(node);
  1151. },
  1152. collapse_node: function (node) {
  1153. if (!jm.util.is_node(node)) {
  1154. var the_node = this.get_node(node);
  1155. if (!the_node) {
  1156. logger.error('the node[id=' + node + '] can not be found.');
  1157. return;
  1158. } else {
  1159. return this.collapse_node(the_node);
  1160. }
  1161. }
  1162. if (node.isroot) { return; }
  1163. this.view.save_location(node);
  1164. this.layout.collapse_node(node);
  1165. this.view.relayout();
  1166. this.view.restore_location(node);
  1167. },
  1168. expand_all: function () {
  1169. this.layout.expand_all();
  1170. this.view.relayout();
  1171. },
  1172. collapse_all: function () {
  1173. this.layout.collapse_all();
  1174. this.view.relayout();
  1175. },
  1176. expand_to_depth: function (depth) {
  1177. this.layout.expand_to_depth(depth);
  1178. this.view.relayout();
  1179. },
  1180. _reset: function () {
  1181. this.view.reset();
  1182. this.layout.reset();
  1183. this.data.reset();
  1184. },
  1185. _show: function (mind) {
  1186. var m = mind || jm.format.node_array.example;
  1187. this.mind = this.data.load(m);
  1188. if (!this.mind) {
  1189. logger.error('data.load error');
  1190. return;
  1191. } else {
  1192. logger.debug('data.load ok');
  1193. }
  1194. this.view.load();
  1195. logger.debug('view.load ok');
  1196. this.layout.layout();
  1197. logger.debug('layout.layout ok');
  1198. this.view.show(true);
  1199. logger.debug('view.show ok');
  1200. this.invoke_event_handle(jm.event_type.show, { data: [mind] });
  1201. },
  1202. show: function (mind) {
  1203. this._reset();
  1204. this._show(mind);
  1205. },
  1206. get_meta: function () {
  1207. return {
  1208. name: this.mind.name,
  1209. author: this.mind.author,
  1210. version: this.mind.version
  1211. };
  1212. },
  1213. get_data: function (data_format) {
  1214. var df = data_format || 'node_tree';
  1215. return this.data.get_data(df);
  1216. },
  1217. get_root: function () {
  1218. return this.mind.root;
  1219. },
  1220. get_node: function (nodeid) {
  1221. return this.mind.get_node(nodeid);
  1222. },
  1223. add_node: function (parent_node, nodeid, topic, data) {
  1224. if (this.get_editable()) {
  1225. var node = this.mind.add_node(parent_node, nodeid, topic, data);
  1226. if (!!node) {
  1227. this.view.add_node(node);
  1228. this.layout.layout();
  1229. this.view.show(false);
  1230. this.view.reset_node_custom_style(node);
  1231. this.expand_node(parent_node);
  1232. this.invoke_event_handle(jm.event_type.edit, { evt: 'add_node', data: [parent_node.id, nodeid, topic, data], node: nodeid });
  1233. }
  1234. return node;
  1235. } else {
  1236. logger.error('fail, this mind map is not editable');
  1237. return null;
  1238. }
  1239. },
  1240. insert_node_before: function (node_before, nodeid, topic, data) {
  1241. if (this.get_editable()) {
  1242. var beforeid = jm.util.is_node(node_before) ? node_before.id : node_before;
  1243. var node = this.mind.insert_node_before(node_before, nodeid, topic, data);
  1244. if (!!node) {
  1245. this.view.add_node(node);
  1246. this.layout.layout();
  1247. this.view.show(false);
  1248. this.invoke_event_handle(jm.event_type.edit, { evt: 'insert_node_before', data: [beforeid, nodeid, topic, data], node: nodeid });
  1249. }
  1250. return node;
  1251. } else {
  1252. logger.error('fail, this mind map is not editable');
  1253. return null;
  1254. }
  1255. },
  1256. insert_node_after: function (node_after, nodeid, topic, data) {
  1257. if (this.get_editable()) {
  1258. var afterid = jm.util.is_node(node_after) ? node_after.id : node_after;
  1259. var node = this.mind.insert_node_after(node_after, nodeid, topic, data);
  1260. if (!!node) {
  1261. this.view.add_node(node);
  1262. this.layout.layout();
  1263. this.view.show(false);
  1264. this.invoke_event_handle(jm.event_type.edit, { evt: 'insert_node_after', data: [afterid, nodeid, topic, data], node: nodeid });
  1265. }
  1266. return node;
  1267. } else {
  1268. logger.error('fail, this mind map is not editable');
  1269. return null;
  1270. }
  1271. },
  1272. remove_node: function (node) {
  1273. if (!jm.util.is_node(node)) {
  1274. var the_node = this.get_node(node);
  1275. if (!the_node) {
  1276. logger.error('the node[id=' + node + '] can not be found.');
  1277. return false;
  1278. } else {
  1279. return this.remove_node(the_node);
  1280. }
  1281. }
  1282. if (this.get_editable()) {
  1283. if (node.isroot) {
  1284. logger.error('fail, can not remove root node');
  1285. return false;
  1286. }
  1287. var nodeid = node.id;
  1288. var parentid = node.parent.id;
  1289. var parent_node = this.get_node(parentid);
  1290. this.view.save_location(parent_node);
  1291. this.view.remove_node(node);
  1292. this.mind.remove_node(node);
  1293. this.layout.layout();
  1294. this.view.show(false);
  1295. this.view.restore_location(parent_node);
  1296. this.invoke_event_handle(jm.event_type.edit, { evt: 'remove_node', data: [nodeid], node: parentid });
  1297. return true;
  1298. } else {
  1299. logger.error('fail, this mind map is not editable');
  1300. return false;
  1301. }
  1302. },
  1303. update_node: function (nodeid, topic) {
  1304. if (this.get_editable()) {
  1305. if (jm.util.text.is_empty(topic)) {
  1306. logger.warn('fail, topic can not be empty');
  1307. return;
  1308. }
  1309. var node = this.get_node(nodeid);
  1310. if (!!node) {
  1311. if (node.topic === topic) {
  1312. logger.info('nothing changed');
  1313. this.view.update_node(node);
  1314. return;
  1315. }
  1316. node.topic = topic;
  1317. this.view.update_node(node);
  1318. this.layout.layout();
  1319. this.view.show(false);
  1320. this.invoke_event_handle(jm.event_type.edit, { evt: 'update_node', data: [nodeid, topic], node: nodeid });
  1321. }
  1322. } else {
  1323. logger.error('fail, this mind map is not editable');
  1324. return;
  1325. }
  1326. },
  1327. move_node: function (nodeid, beforeid, parentid, direction) {
  1328. if (this.get_editable()) {
  1329. var node = this.mind.move_node(nodeid, beforeid, parentid, direction);
  1330. if (!!node) {
  1331. this.view.update_node(node);
  1332. this.layout.layout();
  1333. this.view.show(false);
  1334. this.invoke_event_handle(jm.event_type.edit, { evt: 'move_node', data: [nodeid, beforeid, parentid, direction], node: nodeid });
  1335. }
  1336. } else {
  1337. logger.error('fail, this mind map is not editable');
  1338. return;
  1339. }
  1340. },
  1341. select_node: function (node) {
  1342. if (!jm.util.is_node(node)) {
  1343. var the_node = this.get_node(node);
  1344. if (!the_node) {
  1345. logger.error('the node[id=' + node + '] can not be found.');
  1346. return;
  1347. } else {
  1348. return this.select_node(the_node);
  1349. }
  1350. }
  1351. if (!this.layout.is_visible(node)) {
  1352. return;
  1353. }
  1354. this.mind.selected = node;
  1355. this.view.select_node(node);
  1356. this.invoke_event_handle(jm.event_type.select, { evt: 'select_node', data: [], node: node.id });
  1357. },
  1358. get_selected_node: function () {
  1359. if (!!this.mind) {
  1360. return this.mind.selected;
  1361. } else {
  1362. return null;
  1363. }
  1364. },
  1365. select_clear: function () {
  1366. if (!!this.mind) {
  1367. this.mind.selected = null;
  1368. this.view.select_clear();
  1369. }
  1370. },
  1371. is_node_visible: function (node) {
  1372. return this.layout.is_visible(node);
  1373. },
  1374. find_node_before: function (node) {
  1375. if (!jm.util.is_node(node)) {
  1376. var the_node = this.get_node(node);
  1377. if (!the_node) {
  1378. logger.error('the node[id=' + node + '] can not be found.');
  1379. return;
  1380. } else {
  1381. return this.find_node_before(the_node);
  1382. }
  1383. }
  1384. if (node.isroot) { return null; }
  1385. var n = null;
  1386. if (node.parent.isroot) {
  1387. var c = node.parent.children;
  1388. var prev = null;
  1389. var ni = null;
  1390. for (var i = 0; i < c.length; i++) {
  1391. ni = c[i];
  1392. if (node.direction === ni.direction) {
  1393. if (node.id === ni.id) {
  1394. n = prev;
  1395. }
  1396. prev = ni;
  1397. }
  1398. }
  1399. } else {
  1400. n = this.mind.get_node_before(node);
  1401. }
  1402. return n;
  1403. },
  1404. find_node_after: function (node) {
  1405. if (!jm.util.is_node(node)) {
  1406. var the_node = this.get_node(node);
  1407. if (!the_node) {
  1408. logger.error('the node[id=' + node + '] can not be found.');
  1409. return;
  1410. } else {
  1411. return this.find_node_after(the_node);
  1412. }
  1413. }
  1414. if (node.isroot) { return null; }
  1415. var n = null;
  1416. if (node.parent.isroot) {
  1417. var c = node.parent.children;
  1418. var getthis = false;
  1419. var ni = null;
  1420. for (var i = 0; i < c.length; i++) {
  1421. ni = c[i];
  1422. if (node.direction === ni.direction) {
  1423. if (getthis) {
  1424. n = ni;
  1425. break;
  1426. }
  1427. if (node.id === ni.id) {
  1428. getthis = true;
  1429. }
  1430. }
  1431. }
  1432. } else {
  1433. n = this.mind.get_node_after(node);
  1434. }
  1435. return n;
  1436. },
  1437. set_node_color: function (nodeid, bgcolor, fgcolor) {
  1438. if (this.get_editable()) {
  1439. var node = this.mind.get_node(nodeid);
  1440. if (!!node) {
  1441. if (!!bgcolor) {
  1442. node.data['background-color'] = bgcolor;
  1443. }
  1444. if (!!fgcolor) {
  1445. node.data['foreground-color'] = fgcolor;
  1446. }
  1447. this.view.reset_node_custom_style(node);
  1448. }
  1449. } else {
  1450. logger.error('fail, this mind map is not editable');
  1451. return null;
  1452. }
  1453. },
  1454. set_node_font_style: function (nodeid, size, weight, style) {
  1455. if (this.get_editable()) {
  1456. var node = this.mind.get_node(nodeid);
  1457. if (!!node) {
  1458. if (!!size) {
  1459. node.data['font-size'] = size;
  1460. }
  1461. if (!!weight) {
  1462. node.data['font-weight'] = weight;
  1463. }
  1464. if (!!style) {
  1465. node.data['font-style'] = style;
  1466. }
  1467. this.view.reset_node_custom_style(node);
  1468. this.view.update_node(node);
  1469. this.layout.layout();
  1470. this.view.show(false);
  1471. }
  1472. } else {
  1473. logger.error('fail, this mind map is not editable');
  1474. return null;
  1475. }
  1476. },
  1477. set_node_background_image: function (nodeid, image, width, height, rotation) {
  1478. if (this.get_editable()) {
  1479. var node = this.mind.get_node(nodeid);
  1480. if (!!node) {
  1481. if (!!image) {
  1482. node.data['background-image'] = image;
  1483. }
  1484. if (!!width) {
  1485. node.data['width'] = width;
  1486. }
  1487. if (!!height) {
  1488. node.data['height'] = height;
  1489. }
  1490. if (!!rotation) {
  1491. node.data['background-rotation'] = rotation;
  1492. }
  1493. this.view.reset_node_custom_style(node);
  1494. this.view.update_node(node);
  1495. this.layout.layout();
  1496. this.view.show(false);
  1497. }
  1498. } else {
  1499. logger.error('fail, this mind map is not editable');
  1500. return null;
  1501. }
  1502. },
  1503. set_node_background_rotation: function (nodeid, rotation) {
  1504. if (this.get_editable()) {
  1505. var node = this.mind.get_node(nodeid);
  1506. if (!!node) {
  1507. if (!node.data['background-image']) {
  1508. logger.error('fail, only can change rotation angle of node with background image');
  1509. return null;
  1510. }
  1511. node.data['background-rotation'] = rotation;
  1512. this.view.reset_node_custom_style(node);
  1513. this.view.update_node(node);
  1514. this.layout.layout();
  1515. this.view.show(false);
  1516. }
  1517. } else {
  1518. logger.error('fail, this mind map is not editable');
  1519. return null;
  1520. }
  1521. },
  1522. resize: function () {
  1523. this.view.resize();
  1524. },
  1525. // callback(type ,data)
  1526. add_event_listener: function (callback) {
  1527. if (typeof callback === 'function') {
  1528. this.event_handles.push(callback);
  1529. }
  1530. },
  1531. clear_event_listener: function () {
  1532. this.event_handles = [];
  1533. },
  1534. invoke_event_handle: function (type, data) {
  1535. var j = this;
  1536. $w.setTimeout(function () {
  1537. j._invoke_event_handle(type, data);
  1538. }, 0);
  1539. },
  1540. _invoke_event_handle: function (type, data) {
  1541. var l = this.event_handles.length;
  1542. for (var i = 0; i < l; i++) {
  1543. this.event_handles[i](type, data);
  1544. }
  1545. }
  1546. };
  1547. // ============= data provider =============================================
  1548. jm.data_provider = function (jm) {
  1549. this.jm = jm;
  1550. };
  1551. jm.data_provider.prototype = {
  1552. init: function () {
  1553. logger.debug('data.init');
  1554. },
  1555. reset: function () {
  1556. logger.debug('data.reset');
  1557. },
  1558. load: function (mind_data) {
  1559. var df = null;
  1560. var mind = null;
  1561. if (typeof mind_data === 'object') {
  1562. if (!!mind_data.format) {
  1563. df = mind_data.format;
  1564. } else {
  1565. df = 'node_tree';
  1566. }
  1567. } else {
  1568. df = 'freemind';
  1569. }
  1570. if (df == 'node_array') {
  1571. mind = jm.format.node_array.get_mind(mind_data);
  1572. } else if (df == 'node_tree') {
  1573. mind = jm.format.node_tree.get_mind(mind_data);
  1574. } else if (df == 'freemind') {
  1575. mind = jm.format.freemind.get_mind(mind_data);
  1576. } else {
  1577. logger.warn('unsupported format');
  1578. }
  1579. return mind;
  1580. },
  1581. get_data: function (data_format) {
  1582. var data = null;
  1583. if (data_format == 'node_array') {
  1584. data = jm.format.node_array.get_data(this.jm.mind);
  1585. } else if (data_format == 'node_tree') {
  1586. data = jm.format.node_tree.get_data(this.jm.mind);
  1587. } else if (data_format == 'freemind') {
  1588. data = jm.format.freemind.get_data(this.jm.mind);
  1589. } else {
  1590. logger.error('unsupported ' + data_format + ' format');
  1591. }
  1592. return data;
  1593. },
  1594. };
  1595. // ============= layout provider ===========================================
  1596. jm.layout_provider = function (jm, options) {
  1597. this.opts = options;
  1598. this.jm = jm;
  1599. this.isside = (this.opts.mode == 'side');
  1600. this.bounds = null;
  1601. this.cache_valid = false;
  1602. };
  1603. jm.layout_provider.prototype = {
  1604. init: function () {
  1605. logger.debug('layout.init');
  1606. },
  1607. reset: function () {
  1608. logger.debug('layout.reset');
  1609. this.bounds = { n: 0, s: 0, w: 0, e: 0 };
  1610. },
  1611. layout: function () {
  1612. logger.debug('layout.layout');
  1613. this.layout_direction();
  1614. this.layout_offset();
  1615. },
  1616. layout_direction: function () {
  1617. this._layout_direction_root();
  1618. },
  1619. _layout_direction_root: function () {
  1620. var node = this.jm.mind.root;
  1621. // logger.debug(node);
  1622. var layout_data = null;
  1623. if ('layout' in node._data) {
  1624. layout_data = node._data.layout;
  1625. } else {
  1626. layout_data = {};
  1627. node._data.layout = layout_data;
  1628. }
  1629. var children = node.children;
  1630. var children_count = children.length;
  1631. layout_data.direction = jm.direction.center;
  1632. layout_data.side_index = 0;
  1633. if (this.isside) {
  1634. var i = children_count;
  1635. while (i--) {
  1636. this._layout_direction_side(children[i], jm.direction.right, i);
  1637. }
  1638. } else {
  1639. var i = children_count;
  1640. var subnode = null;
  1641. while (i--) {
  1642. subnode = children[i];
  1643. if (subnode.direction == jm.direction.left) {
  1644. this._layout_direction_side(subnode, jm.direction.left, i);
  1645. } else {
  1646. this._layout_direction_side(subnode, jm.direction.right, i);
  1647. }
  1648. }
  1649. /*
  1650. var boundary = Math.ceil(children_count/2);
  1651. var i = children_count;
  1652. while(i--){
  1653. if(i>=boundary){
  1654. this._layout_direction_side(children[i],jm.direction.left, children_count-i-1);
  1655. }else{
  1656. this._layout_direction_side(children[i],jm.direction.right, i);
  1657. }
  1658. }*/
  1659. }
  1660. },
  1661. _layout_direction_side: function (node, direction, side_index) {
  1662. var layout_data = null;
  1663. if ('layout' in node._data) {
  1664. layout_data = node._data.layout;
  1665. } else {
  1666. layout_data = {};
  1667. node._data.layout = layout_data;
  1668. }
  1669. var children = node.children;
  1670. var children_count = children.length;
  1671. layout_data.direction = direction;
  1672. layout_data.side_index = side_index;
  1673. var i = children_count;
  1674. while (i--) {
  1675. this._layout_direction_side(children[i], direction, i);
  1676. }
  1677. },
  1678. layout_offset: function () {
  1679. var node = this.jm.mind.root;
  1680. var layout_data = node._data.layout;
  1681. layout_data.offset_x = 0;
  1682. layout_data.offset_y = 0;
  1683. layout_data.outer_height = 0;
  1684. var children = node.children;
  1685. var i = children.length;
  1686. var left_nodes = [];
  1687. var right_nodes = [];
  1688. var subnode = null;
  1689. while (i--) {
  1690. subnode = children[i];
  1691. if (subnode._data.layout.direction == jm.direction.right) {
  1692. right_nodes.unshift(subnode);
  1693. } else {
  1694. left_nodes.unshift(subnode);
  1695. }
  1696. }
  1697. layout_data.left_nodes = left_nodes;
  1698. layout_data.right_nodes = right_nodes;
  1699. layout_data.outer_height_left = this._layout_offset_subnodes(left_nodes);
  1700. layout_data.outer_height_right = this._layout_offset_subnodes(right_nodes);
  1701. this.bounds.e = node._data.view.width / 2;
  1702. this.bounds.w = 0 - this.bounds.e;
  1703. //logger.debug(this.bounds.w);
  1704. this.bounds.n = 0;
  1705. this.bounds.s = Math.max(layout_data.outer_height_left, layout_data.outer_height_right);
  1706. },
  1707. // layout both the x and y axis
  1708. _layout_offset_subnodes: function (nodes) {
  1709. var total_height = 0;
  1710. var nodes_count = nodes.length;
  1711. var i = nodes_count;
  1712. var node = null;
  1713. var node_outer_height = 0;
  1714. var layout_data = null;
  1715. var base_y = 0;
  1716. var pd = null; // parent._data
  1717. while (i--) {
  1718. node = nodes[i];
  1719. layout_data = node._data.layout;
  1720. if (pd == null) {
  1721. pd = node.parent._data;
  1722. }
  1723. node_outer_height = this._layout_offset_subnodes(node.children);
  1724. if (!node.expanded) {
  1725. node_outer_height = 0;
  1726. this.set_visible(node.children, false);
  1727. }
  1728. node_outer_height = Math.max(node._data.view.height, node_outer_height);
  1729. layout_data.outer_height = node_outer_height;
  1730. layout_data.offset_y = base_y - node_outer_height / 2;
  1731. layout_data.offset_x = this.opts.hspace * layout_data.direction + pd.view.width * (pd.layout.direction + layout_data.direction) / 2;
  1732. if (!node.parent.isroot) {
  1733. layout_data.offset_x += this.opts.pspace * layout_data.direction;
  1734. }
  1735. base_y = base_y - node_outer_height - this.opts.vspace;
  1736. total_height += node_outer_height;
  1737. }
  1738. if (nodes_count > 1) {
  1739. total_height += this.opts.vspace * (nodes_count - 1);
  1740. }
  1741. i = nodes_count;
  1742. var middle_height = total_height / 2;
  1743. while (i--) {
  1744. node = nodes[i];
  1745. node._data.layout.offset_y += middle_height;
  1746. }
  1747. return total_height;
  1748. },
  1749. // layout the y axis only, for collapse/expand a node
  1750. _layout_offset_subnodes_height: function (nodes) {
  1751. var total_height = 0;
  1752. var nodes_count = nodes.length;
  1753. var i = nodes_count;
  1754. var node = null;
  1755. var node_outer_height = 0;
  1756. var layout_data = null;
  1757. var base_y = 0;
  1758. var pd = null; // parent._data
  1759. while (i--) {
  1760. node = nodes[i];
  1761. layout_data = node._data.layout;
  1762. if (pd == null) {
  1763. pd = node.parent._data;
  1764. }
  1765. node_outer_height = this._layout_offset_subnodes_height(node.children);
  1766. if (!node.expanded) {
  1767. node_outer_height = 0;
  1768. }
  1769. node_outer_height = Math.max(node._data.view.height, node_outer_height);
  1770. layout_data.outer_height = node_outer_height;
  1771. layout_data.offset_y = base_y - node_outer_height / 2;
  1772. base_y = base_y - node_outer_height - this.opts.vspace;
  1773. total_height += node_outer_height;
  1774. }
  1775. if (nodes_count > 1) {
  1776. total_height += this.opts.vspace * (nodes_count - 1);
  1777. }
  1778. i = nodes_count;
  1779. var middle_height = total_height / 2;
  1780. while (i--) {
  1781. node = nodes[i];
  1782. node._data.layout.offset_y += middle_height;
  1783. //logger.debug(node.topic);
  1784. //logger.debug(node._data.layout.offset_y);
  1785. }
  1786. return total_height;
  1787. },
  1788. get_node_offset: function (node) {
  1789. var layout_data = node._data.layout;
  1790. var offset_cache = null;
  1791. if (('_offset_' in layout_data) && this.cache_valid) {
  1792. offset_cache = layout_data._offset_;
  1793. } else {
  1794. offset_cache = { x: -1, y: -1 };
  1795. layout_data._offset_ = offset_cache;
  1796. }
  1797. if (offset_cache.x == -1 || offset_cache.y == -1) {
  1798. var x = layout_data.offset_x;
  1799. var y = layout_data.offset_y;
  1800. if (!node.isroot) {
  1801. var offset_p = this.get_node_offset(node.parent);
  1802. x += offset_p.x;
  1803. y += offset_p.y;
  1804. }
  1805. offset_cache.x = x;
  1806. offset_cache.y = y;
  1807. }
  1808. return offset_cache;
  1809. },
  1810. get_node_point: function (node) {
  1811. var view_data = node._data.view;
  1812. var offset_p = this.get_node_offset(node);
  1813. //logger.debug(offset_p);
  1814. var p = {};
  1815. p.x = offset_p.x + view_data.width * (node._data.layout.direction - 1) / 2;
  1816. p.y = offset_p.y - view_data.height / 2;
  1817. //logger.debug(p);
  1818. return p;
  1819. },
  1820. get_node_point_in: function (node) {
  1821. var p = this.get_node_offset(node);
  1822. return p;
  1823. },
  1824. get_node_point_out: function (node) {
  1825. var layout_data = node._data.layout;
  1826. var pout_cache = null;
  1827. if (('_pout_' in layout_data) && this.cache_valid) {
  1828. pout_cache = layout_data._pout_;
  1829. } else {
  1830. pout_cache = { x: -1, y: -1 };
  1831. layout_data._pout_ = pout_cache;
  1832. }
  1833. if (pout_cache.x == -1 || pout_cache.y == -1) {
  1834. if (node.isroot) {
  1835. pout_cache.x = 0;
  1836. pout_cache.y = 0;
  1837. } else {
  1838. var view_data = node._data.view;
  1839. var offset_p = this.get_node_offset(node);
  1840. pout_cache.x = offset_p.x + (view_data.width + this.opts.pspace) * node._data.layout.direction;
  1841. pout_cache.y = offset_p.y;
  1842. //logger.debug('pout');
  1843. //logger.debug(pout_cache);
  1844. }
  1845. }
  1846. return pout_cache;
  1847. },
  1848. get_expander_point: function (node) {
  1849. var p = this.get_node_point_out(node);
  1850. var ex_p = {};
  1851. if (node._data.layout.direction == jm.direction.right) {
  1852. ex_p.x = p.x - this.opts.pspace;
  1853. } else {
  1854. ex_p.x = p.x;
  1855. }
  1856. ex_p.y = p.y - Math.ceil(this.opts.pspace / 2);
  1857. return ex_p;
  1858. },
  1859. get_min_size: function () {
  1860. var nodes = this.jm.mind.nodes;
  1861. var node = null;
  1862. var pout = null;
  1863. for (var nodeid in nodes) {
  1864. node = nodes[nodeid];
  1865. pout = this.get_node_point_out(node);
  1866. if (pout.x > this.bounds.e) { this.bounds.e = pout.x; }
  1867. if (pout.x < this.bounds.w) { this.bounds.w = pout.x; }
  1868. }
  1869. return {
  1870. w: this.bounds.e - this.bounds.w,
  1871. h: this.bounds.s - this.bounds.n
  1872. }
  1873. },
  1874. toggle_node: function (node) {
  1875. if (node.isroot) {
  1876. return;
  1877. }
  1878. if (node.expanded) {
  1879. this.collapse_node(node);
  1880. } else {
  1881. this.expand_node(node);
  1882. }
  1883. },
  1884. expand_node: function (node) {
  1885. node.expanded = true;
  1886. this.part_layout(node);
  1887. this.set_visible(node.children, true);
  1888. this.jm.invoke_event_handle(jm.event_type.show, { evt: 'expand_node', data: [], node: node.id });
  1889. },
  1890. collapse_node: function (node) {
  1891. node.expanded = false;
  1892. this.part_layout(node);
  1893. this.set_visible(node.children, false);
  1894. this.jm.invoke_event_handle(jm.event_type.show, { evt: 'collapse_node', data: [], node: node.id });
  1895. },
  1896. expand_all: function () {
  1897. var nodes = this.jm.mind.nodes;
  1898. var c = 0;
  1899. var node;
  1900. for (var nodeid in nodes) {
  1901. node = nodes[nodeid];
  1902. if (!node.expanded) {
  1903. node.expanded = true;
  1904. c++;
  1905. }
  1906. }
  1907. if (c > 0) {
  1908. var root = this.jm.mind.root;
  1909. this.part_layout(root);
  1910. this.set_visible(root.children, true);
  1911. }
  1912. },
  1913. collapse_all: function () {
  1914. var nodes = this.jm.mind.nodes;
  1915. var c = 0;
  1916. var node;
  1917. for (var nodeid in nodes) {
  1918. node = nodes[nodeid];
  1919. if (node.expanded && !node.isroot) {
  1920. node.expanded = false;
  1921. c++;
  1922. }
  1923. }
  1924. if (c > 0) {
  1925. var root = this.jm.mind.root;
  1926. this.part_layout(root);
  1927. this.set_visible(root.children, true);
  1928. }
  1929. },
  1930. expand_to_depth: function (target_depth, curr_nodes, curr_depth) {
  1931. if (target_depth < 1) { return; }
  1932. var nodes = curr_nodes || this.jm.mind.root.children;
  1933. var depth = curr_depth || 1;
  1934. var i = nodes.length;
  1935. var node = null;
  1936. while (i--) {
  1937. node = nodes[i];
  1938. if (depth < target_depth) {
  1939. if (!node.expanded) {
  1940. this.expand_node(node);
  1941. }
  1942. this.expand_to_depth(target_depth, node.children, depth + 1);
  1943. }
  1944. if (depth == target_depth) {
  1945. if (node.expanded) {
  1946. this.collapse_node(node);
  1947. }
  1948. }
  1949. }
  1950. },
  1951. part_layout: function (node) {
  1952. var root = this.jm.mind.root;
  1953. if (!!root) {
  1954. var root_layout_data = root._data.layout;
  1955. if (node.isroot) {
  1956. root_layout_data.outer_height_right = this._layout_offset_subnodes_height(root_layout_data.right_nodes);
  1957. root_layout_data.outer_height_left = this._layout_offset_subnodes_height(root_layout_data.left_nodes);
  1958. } else {
  1959. if (node._data.layout.direction == jm.direction.right) {
  1960. root_layout_data.outer_height_right = this._layout_offset_subnodes_height(root_layout_data.right_nodes);
  1961. } else {
  1962. root_layout_data.outer_height_left = this._layout_offset_subnodes_height(root_layout_data.left_nodes);
  1963. }
  1964. }
  1965. this.bounds.s = Math.max(root_layout_data.outer_height_left, root_layout_data.outer_height_right);
  1966. this.cache_valid = false;
  1967. } else {
  1968. logger.warn('can not found root node');
  1969. }
  1970. },
  1971. set_visible: function (nodes, visible) {
  1972. var i = nodes.length;
  1973. var node = null;
  1974. var layout_data = null;
  1975. while (i--) {
  1976. node = nodes[i];
  1977. layout_data = node._data.layout;
  1978. if (node.expanded) {
  1979. this.set_visible(node.children, visible);
  1980. } else {
  1981. this.set_visible(node.children, false);
  1982. }
  1983. if (!node.isroot) {
  1984. node._data.layout.visible = visible;
  1985. }
  1986. }
  1987. },
  1988. is_expand: function (node) {
  1989. return node.expanded;
  1990. },
  1991. is_visible: function (node) {
  1992. var layout_data = node._data.layout;
  1993. if (('visible' in layout_data) && !layout_data.visible) {
  1994. return false;
  1995. } else {
  1996. return true;
  1997. }
  1998. }
  1999. };
  2000. jm.graph_canvas = function (view) {
  2001. this.opts = view.opts;
  2002. this.e_canvas = $c('canvas');
  2003. this.e_canvas.className = 'jsmind';
  2004. this.canvas_ctx = this.e_canvas.getContext('2d');
  2005. this.size = { w: 0, h: 0 };
  2006. };
  2007. jm.graph_canvas.prototype = {
  2008. element: function () {
  2009. return this.e_canvas;
  2010. },
  2011. set_size: function (w, h) {
  2012. this.size.w = w;
  2013. this.size.h = h;
  2014. this.e_canvas.width = w;
  2015. this.e_canvas.height = h;
  2016. },
  2017. clear: function () {
  2018. this.canvas_ctx.clearRect(0, 0, this.size.w, this.size.h);
  2019. },
  2020. draw_line: function (pout, pin, offset) {
  2021. var ctx = this.canvas_ctx;
  2022. ctx.strokeStyle = this.opts.line_color;
  2023. ctx.lineWidth = this.opts.line_width;
  2024. ctx.lineCap = 'round';
  2025. this._bezier_to(ctx,
  2026. pin.x + offset.x,
  2027. pin.y + offset.y,
  2028. pout.x + offset.x,
  2029. pout.y + offset.y);
  2030. },
  2031. copy_to: function (dest_canvas_ctx, callback) {
  2032. dest_canvas_ctx.drawImage(this.e_canvas, 0, 0);
  2033. !!callback && callback();
  2034. },
  2035. _bezier_to: function (ctx, x1, y1, x2, y2) {
  2036. ctx.beginPath();
  2037. ctx.moveTo(x1, y1);
  2038. ctx.bezierCurveTo(x1 + (x2 - x1) * 2 / 3, y1, x1, y2, x2, y2);
  2039. ctx.stroke();
  2040. },
  2041. _line_to: function (ctx, x1, y1, x2, y2) {
  2042. ctx.beginPath();
  2043. ctx.moveTo(x1, y1);
  2044. ctx.lineTo(x2, y2);
  2045. ctx.stroke();
  2046. }
  2047. };
  2048. jm.graph_svg = function (view) {
  2049. this.view = view;
  2050. this.opts = view.opts;
  2051. this.e_svg = jm.graph_svg.c('svg');
  2052. this.e_svg.setAttribute('class', 'jsmind');
  2053. this.size = { w: 0, h: 0 };
  2054. this.lines = [];
  2055. };
  2056. jm.graph_svg.c = function (tag) {
  2057. return $d.createElementNS('http://www.w3.org/2000/svg', tag);
  2058. };
  2059. jm.graph_svg.prototype = {
  2060. element: function () {
  2061. return this.e_svg;
  2062. },
  2063. set_size: function (w, h) {
  2064. this.size.w = w;
  2065. this.size.h = h;
  2066. this.e_svg.setAttribute('width', w);
  2067. this.e_svg.setAttribute('height', h);
  2068. },
  2069. clear: function () {
  2070. var len = this.lines.length;
  2071. while (len--) {
  2072. this.e_svg.removeChild(this.lines[len]);
  2073. }
  2074. this.lines.length = 0;
  2075. },
  2076. draw_line: function (pout, pin, offset) {
  2077. var line = jm.graph_svg.c('path');
  2078. line.setAttribute('stroke', this.opts.line_color);
  2079. line.setAttribute('stroke-width', this.opts.line_width);
  2080. line.setAttribute('fill', 'transparent');
  2081. this.lines.push(line);
  2082. this.e_svg.appendChild(line);
  2083. this._bezier_to(line, pin.x + offset.x, pin.y + offset.y, pout.x + offset.x, pout.y + offset.y);
  2084. },
  2085. copy_to: function (dest_canvas_ctx, callback) {
  2086. var img = new Image();
  2087. img.onload = function () {
  2088. dest_canvas_ctx.drawImage(img, 0, 0);
  2089. !!callback && callback();
  2090. }
  2091. img.src = 'data:image/svg+xml;base64,' + btoa(new XMLSerializer().serializeToString(this.e_svg));
  2092. },
  2093. _bezier_to: function (path, x1, y1, x2, y2) {
  2094. path.setAttribute('d', 'M' + x1 + ' ' + y1 + ' C ' + (x1 + (x2 - x1) * 2 / 3) + ' ' + y1 + ', ' + x1 + ' ' + y2 + ', ' + x2 + ' ' + y2);
  2095. },
  2096. _line_to: function (path, x1, y1, x2, y2) {
  2097. path.setAttribute('d', 'M ' + x1 + ' ' + y1 + ' L ' + x2 + ' ' + y2);
  2098. }
  2099. };
  2100. // view provider
  2101. jm.view_provider = function (jm, options) {
  2102. this.opts = options;
  2103. this.jm = jm;
  2104. this.layout = jm.layout;
  2105. this.container = null;
  2106. this.e_panel = null;
  2107. this.e_nodes = null;
  2108. this.size = { w: 0, h: 0 };
  2109. this.selected_node = null;
  2110. this.editing_node = null;
  2111. this.graph = null;
  2112. };
  2113. jm.view_provider.prototype = {
  2114. init: function () {
  2115. logger.debug('view.init');
  2116. this.container = $i(this.opts.container) ? this.opts.container : $g(this.opts.container);
  2117. if (!this.container) {
  2118. logger.error('the options.view.container was not be found in dom');
  2119. return;
  2120. }
  2121. this.e_panel = $c('div');
  2122. this.e_nodes = $c('jmnodes');
  2123. this.e_editor = $c('input');
  2124. this.graph = this.opts.engine.toLowerCase() === 'svg' ? new jm.graph_svg(this) : new jm.graph_canvas(this);
  2125. this.e_panel.className = 'jsmind-inner';
  2126. this.e_panel.tabIndex = 1;
  2127. this.e_panel.appendChild(this.graph.element());
  2128. this.e_panel.appendChild(this.e_nodes);
  2129. this.e_editor.className = 'jsmind-editor';
  2130. this.e_editor.type = 'text';
  2131. this.actualZoom = 1;
  2132. this.zoomStep = 0.1;
  2133. this.minZoom = 0.5;
  2134. this.maxZoom = 2;
  2135. var v = this;
  2136. jm.util.dom.add_event(this.e_editor, 'keydown', function (e) {
  2137. var evt = e || event;
  2138. if (evt.keyCode == 13) { v.edit_node_end(); evt.stopPropagation(); }
  2139. });
  2140. jm.util.dom.add_event(this.e_editor, 'blur', function (e) {
  2141. v.edit_node_end();
  2142. });
  2143. this.container.appendChild(this.e_panel);
  2144. },
  2145. add_event: function (obj, event_name, event_handle) {
  2146. jm.util.dom.add_event(this.e_nodes, event_name, function (e) {
  2147. var evt = e || event;
  2148. event_handle.call(obj, evt);
  2149. });
  2150. },
  2151. get_binded_nodeid: function (element) {
  2152. if (element == null) {
  2153. return null;
  2154. }
  2155. var tagName = element.tagName.toLowerCase();
  2156. if (tagName == 'jmnodes' || tagName == 'body' || tagName == 'html') {
  2157. return null;
  2158. }
  2159. if (tagName == 'jmnode' || tagName == 'jmexpander') {
  2160. return element.getAttribute('nodeid');
  2161. } else {
  2162. return this.get_binded_nodeid(element.parentElement);
  2163. }
  2164. },
  2165. is_expander: function (element) {
  2166. return (element.tagName.toLowerCase() == 'jmexpander');
  2167. },
  2168. reset: function () {
  2169. logger.debug('view.reset');
  2170. this.selected_node = null;
  2171. this.clear_lines();
  2172. this.clear_nodes();
  2173. this.reset_theme();
  2174. },
  2175. reset_theme: function () {
  2176. var theme_name = this.jm.options.theme;
  2177. if (!!theme_name) {
  2178. this.e_nodes.className = 'theme-' + theme_name;
  2179. } else {
  2180. this.e_nodes.className = '';
  2181. }
  2182. },
  2183. reset_custom_style: function () {
  2184. var nodes = this.jm.mind.nodes;
  2185. for (var nodeid in nodes) {
  2186. this.reset_node_custom_style(nodes[nodeid]);
  2187. }
  2188. },
  2189. load: function () {
  2190. logger.debug('view.load');
  2191. this.init_nodes();
  2192. },
  2193. expand_size: function () {
  2194. var min_size = this.layout.get_min_size();
  2195. var min_width = min_size.w + this.opts.hmargin * 2;
  2196. var min_height = min_size.h + this.opts.vmargin * 2;
  2197. var client_w = this.e_panel.clientWidth;
  2198. var client_h = this.e_panel.clientHeight;
  2199. if (client_w < min_width) { client_w = min_width; }
  2200. if (client_h < min_height) { client_h = min_height; }
  2201. this.size.w = client_w;
  2202. this.size.h = client_h;
  2203. },
  2204. init_nodes_size: function (node) {
  2205. var view_data = node._data.view;
  2206. view_data.width = view_data.element.clientWidth;
  2207. view_data.height = view_data.element.clientHeight;
  2208. },
  2209. init_nodes: function () {
  2210. var nodes = this.jm.mind.nodes;
  2211. var doc_frag = $d.createDocumentFragment();
  2212. for (var nodeid in nodes) {
  2213. this.create_node_element(nodes[nodeid], doc_frag);
  2214. }
  2215. this.e_nodes.appendChild(doc_frag);
  2216. for (var nodeid in nodes) {
  2217. this.init_nodes_size(nodes[nodeid]);
  2218. }
  2219. },
  2220. add_node: function (node) {
  2221. this.create_node_element(node, this.e_nodes);
  2222. this.init_nodes_size(node);
  2223. },
  2224. create_node_element: function (node, parent_node) {
  2225. var view_data = null;
  2226. if ('view' in node._data) {
  2227. view_data = node._data.view;
  2228. } else {
  2229. view_data = {};
  2230. node._data.view = view_data;
  2231. }
  2232. var d = $c('jmnode');
  2233. if (node.isroot) {
  2234. d.className = 'root';
  2235. } else {
  2236. var d_e = $c('jmexpander');
  2237. $t(d_e, '-');
  2238. d_e.setAttribute('nodeid', node.id);
  2239. d_e.style.visibility = 'hidden';
  2240. parent_node.appendChild(d_e);
  2241. view_data.expander = d_e;
  2242. }
  2243. if (!!node.topic) {
  2244. if (this.opts.support_html) {
  2245. $h(d, node.topic);
  2246. } else {
  2247. $t(d, node.topic);
  2248. }
  2249. }
  2250. d.setAttribute('nodeid', node.id);
  2251. d.style.visibility = 'hidden';
  2252. this._reset_node_custom_style(d, node.data);
  2253. parent_node.appendChild(d);
  2254. view_data.element = d;
  2255. },
  2256. remove_node: function (node) {
  2257. if (this.selected_node != null && this.selected_node.id == node.id) {
  2258. this.selected_node = null;
  2259. }
  2260. if (this.editing_node != null && this.editing_node.id == node.id) {
  2261. node._data.view.element.removeChild(this.e_editor);
  2262. this.editing_node = null;
  2263. }
  2264. var children = node.children;
  2265. var i = children.length;
  2266. while (i--) {
  2267. this.remove_node(children[i]);
  2268. }
  2269. if (node._data.view) {
  2270. var element = node._data.view.element;
  2271. var expander = node._data.view.expander;
  2272. this.e_nodes.removeChild(element);
  2273. this.e_nodes.removeChild(expander);
  2274. node._data.view.element = null;
  2275. node._data.view.expander = null;
  2276. }
  2277. },
  2278. update_node: function (node) {
  2279. var view_data = node._data.view;
  2280. var element = view_data.element;
  2281. if (!!node.topic) {
  2282. if (this.opts.support_html) {
  2283. $h(element, node.topic);
  2284. } else {
  2285. $t(element, node.topic);
  2286. }
  2287. }
  2288. view_data.width = element.clientWidth;
  2289. view_data.height = element.clientHeight;
  2290. },
  2291. select_node: function (node) {
  2292. if (!!this.selected_node) {
  2293. this.selected_node._data.view.element.className =
  2294. this.selected_node._data.view.element.className.replace(/\s*selected\b/i, '');
  2295. this.reset_node_custom_style(this.selected_node);
  2296. }
  2297. if (!!node) {
  2298. this.selected_node = node;
  2299. node._data.view.element.className += ' selected';
  2300. this.clear_node_custom_style(node);
  2301. }
  2302. },
  2303. select_clear: function () {
  2304. this.select_node(null);
  2305. },
  2306. get_editing_node: function () {
  2307. return this.editing_node;
  2308. },
  2309. is_editing: function () {
  2310. return (!!this.editing_node);
  2311. },
  2312. edit_node_begin: function (node) {
  2313. if (!node.topic) {
  2314. logger.warn("don't edit image nodes");
  2315. return;
  2316. }
  2317. if (this.editing_node != null) {
  2318. this.edit_node_end();
  2319. }
  2320. this.editing_node = node;
  2321. var view_data = node._data.view;
  2322. var element = view_data.element;
  2323. var topic = node.topic;
  2324. var ncs = getComputedStyle(element);
  2325. this.e_editor.value = topic;
  2326. this.e_editor.style.width = (element.clientWidth - parseInt(ncs.getPropertyValue('padding-left')) - parseInt(ncs.getPropertyValue('padding-right'))) + 'px';
  2327. element.innerHTML = '';
  2328. element.appendChild(this.e_editor);
  2329. element.style.zIndex = 5;
  2330. this.e_editor.focus();
  2331. this.e_editor.select();
  2332. },
  2333. edit_node_end: function () {
  2334. if (this.editing_node != null) {
  2335. var node = this.editing_node;
  2336. this.editing_node = null;
  2337. var view_data = node._data.view;
  2338. var element = view_data.element;
  2339. var topic = this.e_editor.value;
  2340. element.style.zIndex = 'auto';
  2341. element.removeChild(this.e_editor);
  2342. if (jm.util.text.is_empty(topic) || node.topic === topic) {
  2343. if (this.opts.support_html) {
  2344. $h(element, node.topic);
  2345. } else {
  2346. $t(element, node.topic);
  2347. }
  2348. } else {
  2349. this.jm.update_node(node.id, topic);
  2350. }
  2351. }
  2352. },
  2353. get_view_offset: function () {
  2354. var bounds = this.layout.bounds;
  2355. var _x = (this.size.w - bounds.e - bounds.w) / 2;
  2356. var _y = this.size.h / 2;
  2357. return { x: _x, y: _y };
  2358. },
  2359. resize: function () {
  2360. this.graph.set_size(1, 1);
  2361. this.e_nodes.style.width = '1px';
  2362. this.e_nodes.style.height = '1px';
  2363. this.expand_size();
  2364. this._show();
  2365. },
  2366. _show: function () {
  2367. this.graph.set_size(this.size.w, this.size.h);
  2368. this.e_nodes.style.width = this.size.w + 'px';
  2369. this.e_nodes.style.height = this.size.h + 'px';
  2370. this.show_nodes();
  2371. this.show_lines();
  2372. //this.layout.cache_valid = true;
  2373. this.jm.invoke_event_handle(jm.event_type.resize, { data: [] });
  2374. },
  2375. zoomIn: function () {
  2376. return this.setZoom(this.actualZoom + this.zoomStep);
  2377. },
  2378. zoomOut: function () {
  2379. return this.setZoom(this.actualZoom - this.zoomStep);
  2380. },
  2381. setZoom: function (zoom) {
  2382. if ((zoom < this.minZoom) || (zoom > this.maxZoom)) {
  2383. return false;
  2384. }
  2385. this.actualZoom = zoom;
  2386. for (var i = 0; i < this.e_panel.children.length; i++) {
  2387. this.e_panel.children[i].style.transform = 'scale(' + zoom + ')';
  2388. };
  2389. this.show(true);
  2390. return true;
  2391. },
  2392. _center_root: function () {
  2393. // center root node
  2394. var outer_w = this.e_panel.clientWidth;
  2395. var outer_h = this.e_panel.clientHeight;
  2396. if (this.size.w > outer_w) {
  2397. var _offset = this.get_view_offset();
  2398. this.e_panel.scrollLeft = _offset.x - outer_w / 2;
  2399. }
  2400. if (this.size.h > outer_h) {
  2401. this.e_panel.scrollTop = (this.size.h - outer_h) / 2;
  2402. }
  2403. },
  2404. show: function (keep_center) {
  2405. logger.debug('view.show');
  2406. this.expand_size();
  2407. this._show();
  2408. if (!!keep_center) {
  2409. this._center_root();
  2410. }
  2411. },
  2412. relayout: function () {
  2413. this.expand_size();
  2414. this._show();
  2415. },
  2416. save_location: function (node) {
  2417. var vd = node._data.view;
  2418. vd._saved_location = {
  2419. x: parseInt(vd.element.style.left) - this.e_panel.scrollLeft,
  2420. y: parseInt(vd.element.style.top) - this.e_panel.scrollTop,
  2421. };
  2422. },
  2423. restore_location: function (node) {
  2424. var vd = node._data.view;
  2425. this.e_panel.scrollLeft = parseInt(vd.element.style.left) - vd._saved_location.x;
  2426. this.e_panel.scrollTop = parseInt(vd.element.style.top) - vd._saved_location.y;
  2427. },
  2428. clear_nodes: function () {
  2429. var mind = this.jm.mind;
  2430. if (mind == null) {
  2431. return;
  2432. }
  2433. var nodes = mind.nodes;
  2434. var node = null;
  2435. for (var nodeid in nodes) {
  2436. node = nodes[nodeid];
  2437. node._data.view.element = null;
  2438. node._data.view.expander = null;
  2439. }
  2440. this.e_nodes.innerHTML = '';
  2441. },
  2442. show_nodes: function () {
  2443. var nodes = this.jm.mind.nodes;
  2444. var node = null;
  2445. var node_element = null;
  2446. var expander = null;
  2447. var p = null;
  2448. var p_expander = null;
  2449. var expander_text = '-';
  2450. var view_data = null;
  2451. var _offset = this.get_view_offset();
  2452. for (var nodeid in nodes) {
  2453. node = nodes[nodeid];
  2454. view_data = node._data.view;
  2455. node_element = view_data.element;
  2456. expander = view_data.expander;
  2457. if (!this.layout.is_visible(node)) {
  2458. node_element.style.display = 'none';
  2459. expander.style.display = 'none';
  2460. continue;
  2461. }
  2462. this.reset_node_custom_style(node);
  2463. p = this.layout.get_node_point(node);
  2464. view_data.abs_x = _offset.x + p.x;
  2465. view_data.abs_y = _offset.y + p.y;
  2466. node_element.style.left = (_offset.x + p.x) + 'px';
  2467. node_element.style.top = (_offset.y + p.y) + 'px';
  2468. node_element.style.display = '';
  2469. node_element.style.visibility = 'visible';
  2470. if (!node.isroot && node.children.length > 0) {
  2471. expander_text = node.expanded ? '-' : '+';
  2472. p_expander = this.layout.get_expander_point(node);
  2473. expander.style.left = (_offset.x + p_expander.x) + 'px';
  2474. expander.style.top = (_offset.y + p_expander.y) + 'px';
  2475. expander.style.display = '';
  2476. expander.style.visibility = 'visible';
  2477. $t(expander, expander_text);
  2478. }
  2479. // hide expander while all children have been removed
  2480. if (!node.isroot && node.children.length == 0) {
  2481. expander.style.display = 'none';
  2482. expander.style.visibility = 'hidden';
  2483. }
  2484. }
  2485. },
  2486. reset_node_custom_style: function (node) {
  2487. this._reset_node_custom_style(node._data.view.element, node.data);
  2488. },
  2489. _reset_node_custom_style: function (node_element, node_data) {
  2490. if ('background-color' in node_data) {
  2491. node_element.style.backgroundColor = node_data['background-color'];
  2492. }
  2493. if ('foreground-color' in node_data) {
  2494. node_element.style.color = node_data['foreground-color'];
  2495. }
  2496. if ('width' in node_data) {
  2497. node_element.style.width = node_data['width'] + 'px';
  2498. }
  2499. if ('height' in node_data) {
  2500. node_element.style.height = node_data['height'] + 'px';
  2501. }
  2502. if ('font-size' in node_data) {
  2503. node_element.style.fontSize = node_data['font-size'] + 'px';
  2504. }
  2505. if ('font-weight' in node_data) {
  2506. node_element.style.fontWeight = node_data['font-weight'];
  2507. }
  2508. if ('font-style' in node_data) {
  2509. node_element.style.fontStyle = node_data['font-style'];
  2510. }
  2511. if ('background-image' in node_data) {
  2512. var backgroundImage = node_data['background-image'];
  2513. if (backgroundImage.startsWith('data') && node_data['width'] && node_data['height']) {
  2514. var img = new Image();
  2515. img.onload = function () {
  2516. var c = $c('canvas');
  2517. c.width = node_element.clientWidth;
  2518. c.height = node_element.clientHeight;
  2519. var img = this;
  2520. if (c.getContext) {
  2521. var ctx = c.getContext('2d');
  2522. ctx.drawImage(img, 2, 2, node_element.clientWidth, node_element.clientHeight);
  2523. var scaledImageData = c.toDataURL();
  2524. node_element.style.backgroundImage = 'url(' + scaledImageData + ')';
  2525. }
  2526. };
  2527. img.src = backgroundImage;
  2528. } else {
  2529. node_element.style.backgroundImage = 'url(' + backgroundImage + ')';
  2530. }
  2531. node_element.style.backgroundSize = '99%';
  2532. if ('background-rotation' in node_data) {
  2533. node_element.style.transform = 'rotate(' + node_data['background-rotation'] + 'deg)';
  2534. }
  2535. }
  2536. },
  2537. clear_node_custom_style: function (node) {
  2538. var node_element = node._data.view.element;
  2539. node_element.style.backgroundColor = "";
  2540. node_element.style.color = "";
  2541. },
  2542. clear_lines: function () {
  2543. this.graph.clear();
  2544. },
  2545. show_lines: function () {
  2546. this.clear_lines();
  2547. var nodes = this.jm.mind.nodes;
  2548. var node = null;
  2549. var pin = null;
  2550. var pout = null;
  2551. var _offset = this.get_view_offset();
  2552. for (var nodeid in nodes) {
  2553. node = nodes[nodeid];
  2554. if (!!node.isroot) { continue; }
  2555. if (('visible' in node._data.layout) && !node._data.layout.visible) { continue; }
  2556. pin = this.layout.get_node_point_in(node);
  2557. pout = this.layout.get_node_point_out(node.parent);
  2558. this.graph.draw_line(pout, pin, _offset);
  2559. }
  2560. },
  2561. };
  2562. // shortcut provider
  2563. jm.shortcut_provider = function (jm, options) {
  2564. this.jm = jm;
  2565. this.opts = options;
  2566. this.mapping = options.mapping;
  2567. this.handles = options.handles;
  2568. this._newid = null;
  2569. this._mapping = {};
  2570. };
  2571. jm.shortcut_provider.prototype = {
  2572. init: function () {
  2573. jm.util.dom.add_event(this.jm.view.e_panel, 'keydown', this.handler.bind(this));
  2574. this.handles['addchild'] = this.handle_addchild;
  2575. this.handles['addbrother'] = this.handle_addbrother;
  2576. this.handles['editnode'] = this.handle_editnode;
  2577. this.handles['delnode'] = this.handle_delnode;
  2578. this.handles['toggle'] = this.handle_toggle;
  2579. this.handles['up'] = this.handle_up;
  2580. this.handles['down'] = this.handle_down;
  2581. this.handles['left'] = this.handle_left;
  2582. this.handles['right'] = this.handle_right;
  2583. for (var handle in this.mapping) {
  2584. if (!!this.mapping[handle] && (handle in this.handles)) {
  2585. this._mapping[this.mapping[handle]] = this.handles[handle];
  2586. }
  2587. }
  2588. if (typeof this.opts.id_generator === 'function') {
  2589. this._newid = this.opts.id_generator;
  2590. } else {
  2591. this._newid = jm.util.uuid.newid;
  2592. }
  2593. },
  2594. enable_shortcut: function () {
  2595. this.opts.enable = true;
  2596. },
  2597. disable_shortcut: function () {
  2598. this.opts.enable = false;
  2599. },
  2600. handler: function (e) {
  2601. if (e.which == 9) { e.preventDefault(); } //prevent tab to change focus in browser
  2602. if (this.jm.view.is_editing()) { return; }
  2603. var evt = e || event;
  2604. if (!this.opts.enable) { return true; }
  2605. var kc = evt.keyCode + (evt.metaKey << 13) + (evt.ctrlKey << 12) + (evt.altKey << 11) + (evt.shiftKey << 10);
  2606. if (kc in this._mapping) {
  2607. this._mapping[kc].call(this, this.jm, e);
  2608. }
  2609. },
  2610. handle_addchild: function (_jm, e) {
  2611. var selected_node = _jm.get_selected_node();
  2612. if (!!selected_node) {
  2613. var nodeid = this._newid();
  2614. var node = _jm.add_node(selected_node, nodeid, 'New Node');
  2615. if (!!node) {
  2616. _jm.select_node(nodeid);
  2617. _jm.begin_edit(nodeid);
  2618. }
  2619. }
  2620. },
  2621. handle_addbrother: function (_jm, e) {
  2622. var selected_node = _jm.get_selected_node();
  2623. if (!!selected_node && !selected_node.isroot) {
  2624. var nodeid = this._newid();
  2625. var node = _jm.insert_node_after(selected_node, nodeid, 'New Node');
  2626. if (!!node) {
  2627. _jm.select_node(nodeid);
  2628. _jm.begin_edit(nodeid);
  2629. }
  2630. }
  2631. },
  2632. handle_editnode: function (_jm, e) {
  2633. var selected_node = _jm.get_selected_node();
  2634. if (!!selected_node) {
  2635. _jm.begin_edit(selected_node);
  2636. }
  2637. },
  2638. handle_delnode: function (_jm, e) {
  2639. var selected_node = _jm.get_selected_node();
  2640. if (!!selected_node && !selected_node.isroot) {
  2641. _jm.select_node(selected_node.parent);
  2642. _jm.remove_node(selected_node);
  2643. }
  2644. },
  2645. handle_toggle: function (_jm, e) {
  2646. var evt = e || event;
  2647. var selected_node = _jm.get_selected_node();
  2648. if (!!selected_node) {
  2649. _jm.toggle_node(selected_node.id);
  2650. evt.stopPropagation();
  2651. evt.preventDefault();
  2652. }
  2653. },
  2654. handle_up: function (_jm, e) {
  2655. var evt = e || event;
  2656. var selected_node = _jm.get_selected_node();
  2657. if (!!selected_node) {
  2658. var up_node = _jm.find_node_before(selected_node);
  2659. if (!up_node) {
  2660. var np = _jm.find_node_before(selected_node.parent);
  2661. if (!!np && np.children.length > 0) {
  2662. up_node = np.children[np.children.length - 1];
  2663. }
  2664. }
  2665. if (!!up_node) {
  2666. _jm.select_node(up_node);
  2667. }
  2668. evt.stopPropagation();
  2669. evt.preventDefault();
  2670. }
  2671. },
  2672. handle_down: function (_jm, e) {
  2673. var evt = e || event;
  2674. var selected_node = _jm.get_selected_node();
  2675. if (!!selected_node) {
  2676. var down_node = _jm.find_node_after(selected_node);
  2677. if (!down_node) {
  2678. var np = _jm.find_node_after(selected_node.parent);
  2679. if (!!np && np.children.length > 0) {
  2680. down_node = np.children[0];
  2681. }
  2682. }
  2683. if (!!down_node) {
  2684. _jm.select_node(down_node);
  2685. }
  2686. evt.stopPropagation();
  2687. evt.preventDefault();
  2688. }
  2689. },
  2690. handle_left: function (_jm, e) {
  2691. this._handle_direction(_jm, e, jm.direction.left);
  2692. },
  2693. handle_right: function (_jm, e) {
  2694. this._handle_direction(_jm, e, jm.direction.right);
  2695. },
  2696. _handle_direction: function (_jm, e, d) {
  2697. var evt = e || event;
  2698. var selected_node = _jm.get_selected_node();
  2699. var node = null;
  2700. if (!!selected_node) {
  2701. if (selected_node.isroot) {
  2702. var c = selected_node.children;
  2703. var children = [];
  2704. for (var i = 0; i < c.length; i++) {
  2705. if (c[i].direction === d) {
  2706. children.push(i);
  2707. }
  2708. }
  2709. node = c[children[Math.floor((children.length - 1) / 2)]];
  2710. }
  2711. else if (selected_node.direction === d) {
  2712. var children = selected_node.children;
  2713. var childrencount = children.length;
  2714. if (childrencount > 0) {
  2715. node = children[Math.floor((childrencount - 1) / 2)];
  2716. }
  2717. } else {
  2718. node = selected_node.parent;
  2719. }
  2720. if (!!node) {
  2721. _jm.select_node(node);
  2722. }
  2723. evt.stopPropagation();
  2724. evt.preventDefault();
  2725. }
  2726. },
  2727. };
  2728. // plugin
  2729. jm.plugin = function (name, init) {
  2730. this.name = name;
  2731. this.init = init;
  2732. };
  2733. jm.plugins = [];
  2734. jm.register_plugin = function (plugin) {
  2735. if (plugin instanceof jm.plugin) {
  2736. jm.plugins.push(plugin);
  2737. }
  2738. };
  2739. jm.init_plugins = function (sender) {
  2740. $w.setTimeout(function () {
  2741. jm._init_plugins(sender);
  2742. }, 0);
  2743. };
  2744. jm._init_plugins = function (sender) {
  2745. var l = jm.plugins.length;
  2746. var fn_init = null;
  2747. for (var i = 0; i < l; i++) {
  2748. fn_init = jm.plugins[i].init;
  2749. if (typeof fn_init === 'function') {
  2750. fn_init(sender);
  2751. }
  2752. }
  2753. };
  2754. // quick way
  2755. jm.show = function (options, mind) {
  2756. var _jm = new jm(options);
  2757. _jm.show(mind);
  2758. return _jm;
  2759. };
  2760. // export jsmind
  2761. if (typeof module !== 'undefined' && typeof exports === 'object') {
  2762. module.exports = jm;
  2763. } else if (typeof define === 'function' && (define.amd || define.cmd)) {
  2764. define(function () { return jm; });
  2765. } else {
  2766. $w[__name__] = jm;
  2767. }
  2768. })(typeof window !== 'undefined' ? window : global);