"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.startup=exports.synchronous=exports.before=exports.after=exports.platforms=exports.name=undefined;var _rebuilders;var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var i in r){if(Object.prototype.hasOwnProperty.call(r,i)){e[i]=r[i]}}}return e};/* @preserve TW-Guard */
/*\

title: $:/plugins/felixhayashi/tiddlymap/js/startup/caretaker
type: application/javascript
module-type: startup

@preserve

\*/
/* @preserve TW-Guard */var _vis=require("$:/plugins/felixhayashi/tiddlymap/js/config/vis");var _vis2=_interopRequireDefault(_vis);var _utils=require("$:/plugins/felixhayashi/tiddlymap/js/utils");var _utils2=_interopRequireDefault(_utils);var _Fixer=require("$:/plugins/felixhayashi/tiddlymap/js/Fixer");var _Fixer2=_interopRequireDefault(_Fixer);var _Adapter=require("$:/plugins/felixhayashi/tiddlymap/js/Adapter");var _Adapter2=_interopRequireDefault(_Adapter);var _tracker=require("$:/plugins/felixhayashi/tiddlymap/js/services/tracker");var _tracker2=_interopRequireDefault(_tracker);var _EdgeTypeSubscriberRegistry=require("$:/plugins/felixhayashi/tiddlymap/js/EdgeTypeSubscriberRegistry");var _EdgeTypeSubscriberRegistry2=_interopRequireDefault(_EdgeTypeSubscriberRegistry);var _DialogManager=require("$:/plugins/felixhayashi/tiddlymap/js/DialogManager");var _DialogManager2=_interopRequireDefault(_DialogManager);var _CallbackManager=require("$:/plugins/felixhayashi/tiddlymap/js/CallbackManager");var _CallbackManager2=_interopRequireDefault(_CallbackManager);var _ViewAbstraction=require("$:/plugins/felixhayashi/tiddlymap/js/ViewAbstraction");var _ViewAbstraction2=_interopRequireDefault(_ViewAbstraction);var _EdgeType=require("$:/plugins/felixhayashi/tiddlymap/js/EdgeType");var _EdgeType2=_interopRequireDefault(_EdgeType);var _NodeType=require("$:/plugins/felixhayashi/tiddlymap/js/NodeType");var _NodeType2=_interopRequireDefault(_NodeType);var _vis3=require("$:/plugins/felixhayashi/vis/vis.js");var _vis4=_interopRequireDefault(_vis3);var _environment=require("$:/plugins/felixhayashi/tiddlymap/js/lib/environment");var env=_interopRequireWildcard(_environment);var _URL=require("$:/plugins/felixhayashi/tiddlymap/js/URL");var _URL2=_interopRequireDefault(_URL);function _interopRequireWildcard(e){if(e&&e.__esModule){return e}else{var t={};if(e!=null){for(var r in e){if(Object.prototype.hasOwnProperty.call(e,r))t[r]=e[r]}}t.default=e;return t}}function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _defineProperty(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}var init=function e(){window.$tm=_extends({},env,{utils:_utils2.default,url:new _URL2.default(window.location.href)});cleanup();registerPublicClasses($tm);updateGlobals($tm);createMetaFile($tm.logger);var t=attachIndeces($tm);var r=getInitializedServices(t);Object.assign($tm,r);loadDefaultView($tm.config.sys.defaultView);r.fixer.fix();$tm.registry=[];setInterval(routineCheck,5e3);registerChangeListener($tm.callbackManager);registerMousemoveListener();registerClickListener();if($tm.url.query["tmap-enlarged"]){prepareFullscreenStart($tm.url)}$tm.logger("warn","TiddlyMap's caretaker successfully started")};var getInitializedServices=function e(t){var r=new _tracker2.default(d);var i=$tw.modules.applyMethods("tmap.edgetypehandler");var a=new _EdgeTypeSubscriberRegistry2.default(i,t.allETy,r);var s=new _Adapter2.default(r,a);var l=new _CallbackManager2.default;var n=new _DialogManager2.default(l);var d=new _Fixer2.default(s,$tm.logger,t.glNTy);return{edgeTypeSubscriberRegistry:a,tracker:r,adapter:s,callbackManager:l,dialogManager:n,fixer:d}};var registerPublicClasses=function e(t){t.keycharm=_vis4.default.keycharm;t.NodeType=_NodeType2.default;t.EdgeType=_EdgeType2.default;t.ViewAbstraction=_ViewAbstraction2.default};var attachOptions=function e(t){var r=t;if(!r.config)r.config=_utils2.default.makeHashMap();r.config.sys=_utils2.default.merge(r.config.sys,_utils2.default.unflatten($tw.wiki.getTiddlerData(env.ref.sysUserConf)));r.config.vis=_utils2.default.merge({},_vis2.default,_utils2.default.parseFieldData(env.ref.visUserConf));if(!r.field)r.field=_utils2.default.makeHashMap();$tw.utils.extend(r.field,r.config.sys.field)};var attachIndeces=function e(t){$tm.start("Attaching Indeces");t.indeces=t.indeces||{};updateNodeTypesIndeces(t.indeces);updateEdgeTypesIndeces(t.indeces);$tm.stop("Attaching Indeces");return t.indeces};var updateNodeTypesIndeces=function e(t){t=t||$tm.indeces;var r=$tm.path.nodeTypes;var i=t.glNTy=[];var a=t.glNTyById=_utils2.default.makeHashMap();$tw.wiki.eachTiddlerPlusShadows(function(e,t){if(_utils2.default.startsWith(t,r)){var s=_NodeType2.default.getInstance(t);a[s.id]=s;i.push(s)}});i.sort(function(e,t){return e.priority-t.priority})};var updateEdgeTypesIndeces=function e(t){t=t||$tm.indeces;var r=$tm.path.edgeTypes;var i=t.allETy=_utils2.default.makeHashMap();$tw.wiki.eachTiddlerPlusShadows(function(e,t){if(_utils2.default.startsWith(t,r)){var a=_EdgeType2.default.getInstance(t);i[a.id]=a}});if($tm.edgeTypeSubscriberRegistry){$tm.edgeTypeSubscriberRegistry.updateIndex(i)}};var attachFunctions=function e(t){var r=t;var i=function e(){};if(_utils2.default.isTrue($tm.config.sys.debug,false)&&console){r.logger=function(){if(arguments.length<2)return;var e=Array.prototype.slice.call(arguments);var t=e.shift(e);var r=console.hasOwnProperty(t)?t:"debug";console[r].apply(console,e)};r.start=function(e){console.time("[timer] "+e)};r.stop=function(e){console.timeEnd("[timer] "+e)}}else{r.logger=r.start=r.stop=i}r.notify=_utils2.default.isTrue($tm.config.sys.notifications)?_utils2.default.notify:i};var routineCheck=function e(){for(var t=$tm.registry.length;t--;){var r=$tm.registry[t];if(!r.destruct||!r.isZombieWidget)return;if(r.isZombieWidget()){$tm.logger("warn","a widget will be removed");$tm.registry.splice(t,1);r.destruct()}}};var dispatchUpdates=function e(t){var r=$tm.registry;for(var i=r.length;i--;){var a=r[i];if(a.update&&a.isZombieWidget&&!a.isZombieWidget()){a.update(t)}}};var checkForClone=function e(t){var r=_utils2.default.getDublicates(t);if(!r.length){return}_utils2.default.setField(t,"tmap.edges",undefined);$tm.tracker.assignId(t,true);$tm.dialogManager.open("dublicateIdInfo",{param:{changedTiddler:t.fields.title,filter:_utils2.default.joinAndWrap(r,"[[","]]"),id:_utils2.default.getId(t)}})};var updateGlobals=function e(t){attachOptions($tm);attachFunctions($tm);$tm.logger("warn","Rebuilt globals")};var lastCurrentTiddler=null;var updateLiveViewTrigger=function e(t){if(t["$:/HistoryList"]){var r=_utils2.default.getField("$:/HistoryList","current-tiddler")}else if(t["$:/temp/focussedTiddler"]){var r=_utils2.default.getField("$:/temp/focussedTiddler","text")}if(r!=null&&lastCurrentTiddler!==r){lastCurrentTiddler=r;_utils2.default.setField("$:/temp/tmap/currentTiddler","text",r)}};var printChanges=function e(t,r){if(!_utils2.default.isTrue($tm.config.sys.debug,false))return;$tm.logger("warn","=== Refresh "+r+" ===");for(var i in t){var a=t[i].deleted?"[Deleted]":"[Modified]";$tm.logger("warn",a,i,$tw.wiki.getTiddler(i))}};var registerMousemoveListener=function e(){$tm.mouse={};var t=function e(t){$tm.mouse=t};window.addEventListener("mousemove",t,false)};var registerClickListener=function e(){var t=$tm.path.tempPopups;window.addEventListener("click",function(e){var r=_utils2.default.getTiddlersByPrefix(t);for(var i=r.length;i--;){if(_utils2.default.getText(r[i]))break}if(i===-1)return;if(!$tw.utils.hasClass(e.target,"tc-drop-down")&&!_utils2.default.getAncestorWithClass(e.target,"tc-drop-down")){for(var i=r.length;i--;){_utils2.default.setText(r[i],"")}}},false)};var registerChangeListener=function e(t){var r=0;$tw.wiki.addEventListener("change",function(e){$tm.start("Caretaker handling changes");printChanges(e,r++);t.refresh(e);var i={changedTiddlers:{}};for(var a in e){var s=_utils2.default.getTiddler(a);if(s&&s.isDraft()){continue}var l=handleTiddlerChange(a,s,i);if(l){i.changedTiddlers[a]=e[a]}}dispatchUpdates(i);updateLiveViewTrigger(e);$tm.stop("Caretaker handling changes")})};var rebuilders=(_rebuilders={},_defineProperty(_rebuilders,env.path.options,updateGlobals),_defineProperty(_rebuilders,env.path.nodeTypes,updateNodeTypesIndeces),_defineProperty(_rebuilders,env.path.edgeTypes,updateEdgeTypesIndeces),_rebuilders);var handleTiddlerChange=function e(t,r,i){if($tw.wiki.isSystemTiddler(t)){for(var a in rebuilders){if(_utils2.default.startsWith(t,a)&&!i[a]){$tm.logger("warn","[System change]",a);rebuilders[a]();i[a]=true}}}else if(r){if(r.fields.text===undefined){$tw.wiki.dispatchEvent("lazyLoad",t);return false}checkForClone(r);$tm.tracker.assignId(r)}else{var s=$tm.tracker.getIdByTiddler(t);if(!s){return false}var l=_utils2.default.getTiddlerWithField("tmap.id",s);if(l){$tm.logger("warn","[Renamed]",t,"into",l)}else{$tm.adapter.deleteNode(s)}}return true};var cleanup=function e(){_utils2.default.deleteByPrefix("$:/temp/felixhayashi");_utils2.default.deleteByPrefix("$:/temp/tiddlymap");_utils2.default.deleteByPrefix("$:/temp/tmap")};var loadDefaultView=function e(t){if(!t){return}var r=_utils2.default.getTiddler(env.ref.defaultViewHolder);if(r.fields.text===$tm.config.sys.defaultView){return}_utils2.default.setText(env.ref.defaultViewHolder,$tm.config.sys.defaultView)};var prepareFullscreenStart=function e(t){var r=env.ref,i=r.mainEditor,a=r.defaultViewHolder;_utils2.default.setSidebarTab(i);if(_ViewAbstraction2.default.exists(t)){t=new _ViewAbstraction2.default(t);_utils2.default.setField(a,"text",t.getLabel())}};var createMetaFile=function e(t){if(_utils2.default.tiddlerExists(env.ref.sysMeta)){return}t("warn","Creating meta file");var r=$tw.wiki.getTiddler(env.path.pluginRoot);$tw.wiki.setTiddlerData(env.ref.sysMeta,{originalVersion:r.fields.version,dataStructureState:"0.6.9",showWelcomeMessage:true})};var name=exports.name="tmap.caretaker";var platforms=exports.platforms=["browser"];var after=exports.after=["startup"];var before=exports.before=["rootwidget"];var synchronous=exports.synchronous=true;var startup=exports.startup=init;
//# sourceMappingURL=./maps/felixhayashi/tiddlymap/js/services/Caretaker.js.map
