"use strict";(self.webpackChunkjupyterlab_autosave_on_focus_change=self.webpackChunkjupyterlab_autosave_on_focus_change||[]).push([[673],{673:(e,t,s)=>{s.r(t),s.d(t,{default:()=>_});var n=s(228),i=s(338),o=s(810),a=s(148),c=s(589),r=s(53),d=s(28);class g{constructor(e){this._nodes=new Map;const t={debug:!1,...e};this._shell=t.shell,this._docManager=t.docManager,this._notebookTracker=t.notebookTracker,this._editorTracker=t.editorTracker,this._debug_printer=(t.debug,(...e)=>{})}documentWidgets(e=!0){const t=[];for(const s of(0,r.toArray)(this._shell.widgets("main")))s.node.classList.contains("saves-on-lose-focus")&&e||this.isDocumentWidget(s)&&t.push(s);return t}isDocumentWidget(e){return void 0!==this._docManager.contextForWidget(e)}trackWidgets(){for(const e of this.documentWidgets())this._nodes.set(e.node,e),e.node.classList.add("saves-on-lose-focus"),e.node.addEventListener("focusout",this)}unTrackWidgets(){for(const e of this.documentWidgets(!1))e.node.classList.remove("saves-on-lose-focus"),e.node.removeEventListener("focusout",this);this._nodes.clear()}handleEvent(e){let t;"focusout"===e.type&&(t=this.getWidgetFromEvent(e),this._debug_printer("FocusEvent: ",e),void 0!==t&&this.saveDocumentWidget(t))}getWidgetFromEvent(e){const t=e.currentTarget;if(!1===this._saveOnCellFocusChange){const s=e.relatedTarget;if(null!==s&&t.contains(s))return}return this._nodes.get(t)}saveDocumentWidget(e){const t=this._docManager.contextForWidget(e);!1===this._excludeMatcher.match(t.path)&&!0===t.model.dirty&&!1===t.isDisposed&&t.save().then((()=>{this._debug_printer("Saving: ",t.path)})).catch((e=>{this._debug_printer(`Error saving: ${t.path}`,e)}))}saveAllDocumentWidgets(){for(const e of this.documentWidgets(!1))this.saveDocumentWidget(e)}updateSettings(e){this._excludeMatcher=new d.Minimatch(`{${e.exclude.join(",")},}`,{nocomment:!0}),this._saveOnCellFocusChange=e.saveOnCellFocusChange,this._debug_printer("_excludeMatcher: ",this._excludeMatcher),e.active,this.saveAllDocumentWidgets(),!0===e.active?(this.trackWidgets(),this._notebookTracker.widgetAdded.connect(this.trackWidgets,this),this._editorTracker.widgetAdded.connect(this.trackWidgets,this)):(this.unTrackWidgets(),this._notebookTracker.widgetAdded.disconnect(this.trackWidgets,this),this._editorTracker.widgetAdded.disconnect(this.trackWidgets,this))}}const h="jupyterlab_autosave_on_focus_change:settings",u="jupyterlab_autosave_on_focus_change:toggle-active";class l{constructor(e){const t={debug:!1,...e};this._app=t.app,this._settingRegistry=t.settingRegistry,this._saveTracker=t.focusSaveTracker,this._mainMenu=t.mainMenu,this._active=!0,this._debug_printer=(t.debug,(...e)=>{})}parseSetting(e){this._active=e.get("active").composite;const t=e.get("exclude").composite,s=e.get("saveOnCellFocusChange").composite;return{active:this._active,exclude:t,saveOnCellFocusChange:s}}loadSetting(e){const t=this.parseSetting(e);this._saveTracker.updateSettings(t),this._debug_printer("FocusChangeAutoSaveSettings.loadSetting:",t)}trackSettingChanges(){Promise.all([this._app.restored,this._settingRegistry.load(h)]).then((([,e])=>{this.loadSetting(e),e.changed.connect(this.loadSetting,this),this.addToggleCommand(e)})).catch((e=>{console.error(`Something went wrong when reading the settings.\n${e}`)}))}addToggleCommand(e){this._app.commands.addCommand(u,{label:"Autosave Documents on Focus Change",isToggled:()=>this._active,execute:()=>{Promise.all([e.set("active",!this._active)]).then((()=>{e.changed.connect(this.loadSetting,this),this._debug_printer("FocusChangeAutoSaveSettings.addToggleCommand:",{active:e.get("active").composite})})).catch((e=>{console.error(`Something went wrong when changing the settings.\n${e}`)}))}}),this._mainMenu.settingsMenu.addGroup([{command:u}],4)}}const _={id:h,autoStart:!0,requires:[n.INotebookTracker,o.IEditorTracker,a.IDocumentManager,i.ISettingRegistry,c.IMainMenu],activate:(e,t,s,n,i,o)=>{console.log("JupyterLab extension jupyterlab_autosave_on_focus_change is activated!");const a=new g({shell:e.shell,docManager:n,notebookTracker:t,editorTracker:s});new l({app:e,settingRegistry:i,focusSaveTracker:a,mainMenu:o}).trackSettingChanges()}}}}]);