-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExtensionManagerViewModel.js
More file actions
1 lines (1 loc) · 10.7 KB
/
ExtensionManagerViewModel.js
File metadata and controls
1 lines (1 loc) · 10.7 KB
1
define(function(require,exports,module){var _=require("thirdparty/lodash");const ExtensionManager=require("extensibility/ExtensionManager"),registry_utils=require("extensibility/registry_utils"),EventDispatcher=require("utils/EventDispatcher"),Strings=require("strings"),PreferencesManager=require("preferences/PreferencesManager");var _searchFields=[["metadata","name"],["metadata","title"],["metadata","description"],["metadata","author","name"],["metadata","keywords"],["owner"]];function ExtensionManagerViewModel(){this._handleStatusChange=this._handleStatusChange.bind(this),ExtensionManager.on("statusChange."+this.source,this._handleStatusChange).on("registryUpdate."+this.source,this._handleStatusChange)}function RegistryViewModel(){ExtensionManagerViewModel.call(this),this.infoMessage=`<div style="display: flex; justify-content: space-between;">\n <div style="margin-top: 3px; width: 80%;">${Strings.REGISTRY_SANITY_CHECK_WARNING}</div>\n <div>\n <button class="dialog-button btn primary btn-mini createExtesnion"\n style="white-space: nowrap;">\n ${Strings.EXTENSION_MANAGER_CREATE_EXTENSION}</button>\n </div>\n </div>`}function InstalledViewModel(){ExtensionManagerViewModel.call(this);var self=this;ExtensionManager.on("registryDownload."+this.source,function(){self._sortFullSet(),self._setInitialFilter()}),this.infoMessage=`<div style="display: flex; justify-content: space-between;">\n <div style="margin-top: 3px;">${Strings.EXTENSION_MANAGER_THEMES_INFO}</div>\n <div>\n <button id="InstalledViewThemeRevert" class="dialog-button btn btn-mini undoTheme forced-hidden">\n ${Strings.EXTENSION_MANAGER_THEMES_UNDO}</button>\n </div>\n </div>`}function DefaultViewModel(){ExtensionManagerViewModel.call(this)}function ThemesViewModel(){ExtensionManagerViewModel.call(this),this.infoMessage=`<div style="display: flex; justify-content: space-between;">\n <div style="margin-top: 3px;">${Strings.EXTENSION_MANAGER_THEMES_INFO}</div>\n <div>\n <button id="ThemeViewThemeRevert" class="dialog-button btn btn-mini undoTheme forced-hidden">\n ${Strings.EXTENSION_MANAGER_THEMES_UNDO}</button>\n <button class="dialog-button btn btn-mini createTheme primary"\n style="white-space: nowrap;">\n ${Strings.EXTENSION_MANAGER_CREATE_THEME}</button>\n </div>\n </div>`}EventDispatcher.makeEventDispatcher(ExtensionManagerViewModel.prototype),ExtensionManagerViewModel.prototype.SOURCE_REGISTRY="registry",ExtensionManagerViewModel.prototype.SOURCE_THEMES="themes",ExtensionManagerViewModel.prototype.SOURCE_INSTALLED="installed",ExtensionManagerViewModel.prototype.SOURCE_DEFAULT="default",ExtensionManagerViewModel.prototype.extensions=null,ExtensionManagerViewModel.prototype.source=null,ExtensionManagerViewModel.prototype.filterSet=null,ExtensionManagerViewModel.prototype.sortedFullSet=null,ExtensionManagerViewModel.prototype._lastQuery=null,ExtensionManagerViewModel.prototype.infoMessage=null,ExtensionManagerViewModel.prototype.message=null,ExtensionManagerViewModel.prototype.notifyCount=0,ExtensionManagerViewModel.prototype._initializeFromSourcePromise=null,ExtensionManagerViewModel.prototype.dispose=function(){this.trigger("dispose"),ExtensionManager.off("."+this.source)},ExtensionManagerViewModel.prototype._setInitialFilter=function(){this.filterSet=_.clone(this.sortedFullSet),this.trigger("filter")},ExtensionManagerViewModel.prototype._sortFullSet=function(){},ExtensionManagerViewModel.prototype.initialize=function(){var self=this;return this._initializeFromSourcePromise=this._initializeFromSource().always(function(){self._updateMessage()}),this._initializeFromSourcePromise},ExtensionManagerViewModel.prototype._handleStatusChange=function(e,id){this.trigger("change",id)},ExtensionManagerViewModel.prototype.filter=function(query,force){var self=this,initialList;initialList=!force&&this._lastQuery&&0===query.indexOf(this._lastQuery)?this.filterSet:this.sortedFullSet;var keywords=query.toLowerCase().split(/\s+/);function filterForKeyword(extensionList,word){var filteredList=[];return extensionList.forEach(function(id){var entry=self._getEntry(id);entry&&self._entryMatchesQuery(entry,word)&&filteredList.push(id)}),filteredList}var i,currentList=initialList;for(i=0;i<keywords.length;i++)currentList=filterForKeyword(currentList,keywords[i]);this._lastQuery=query,this.filterSet=currentList,this._updateMessage(),this.trigger("filter")},ExtensionManagerViewModel.prototype._updateMessage=function(){this._initializeFromSourcePromise&&"rejected"===this._initializeFromSourcePromise.state()?this.message=Strings.EXTENSION_MANAGER_ERROR_LOAD:this.filterSet&&0===this.filterSet.length?this.message=this.sortedFullSet&&this.sortedFullSet.length?Strings.NO_EXTENSION_MATCHES:Strings.NO_EXTENSIONS:this.message=null},ExtensionManagerViewModel.prototype._getEntry=function(id){return null},ExtensionManagerViewModel.prototype._entryMatchesQuery=function(entry,query){return""===query||_searchFields.some(function(fieldSpec){var i,cur=entry,components;for(i=0;i<fieldSpec.length;i++)if(!(cur=cur[fieldSpec[i]]))return!1;if(Array.isArray(cur))return cur.some(function(keyword){return-1!==keyword.toLowerCase().indexOf(query)});if("owner"===fieldSpec[fieldSpec.length-1]){if(-1!==cur.split(":")[1].toLowerCase().indexOf(query))return!0}else if(-1!==cur.toLowerCase().indexOf(query))return!0})},ExtensionManagerViewModel.prototype._setSortedExtensionList=function(extensions,isTheme){this.filterSet=this.sortedFullSet=registry_utils.sortRegistry(extensions,"registryInfo",PreferencesManager.get("extensions.sort")).filter(function(entry){return isTheme?entry.registryInfo&&entry.registryInfo.metadata.theme:entry.registryInfo&&!entry.registryInfo.metadata.theme}).filter(function(entry){return!ExtensionManager.isExtensionTakenDown(entry.registryInfo.metadata.name)}).map(function(entry){return entry.registryInfo.metadata.name})},RegistryViewModel.prototype=Object.create(ExtensionManagerViewModel.prototype),RegistryViewModel.prototype.constructor=RegistryViewModel,RegistryViewModel.prototype.source=ExtensionManagerViewModel.prototype.SOURCE_REGISTRY,RegistryViewModel.prototype._initializeFromSource=function(){var self=this;return ExtensionManager.downloadRegistry().done(function(){self.extensions=ExtensionManager.extensions,self._setSortedExtensionList(ExtensionManager.extensions,!1),self._setInitialFilter()}).fail(function(){self.extensions=[],self.sortedFullSet=[],self.filterSet=[]})},RegistryViewModel.prototype._getEntry=function(id){var entry=this.extensions[id];return entry?entry.registryInfo:entry},InstalledViewModel.prototype=Object.create(ExtensionManagerViewModel.prototype),InstalledViewModel.prototype.constructor=InstalledViewModel,InstalledViewModel.prototype.source=ExtensionManagerViewModel.prototype.SOURCE_INSTALLED,InstalledViewModel.prototype._initializeFromSource=function(){var self=this;return this.extensions=ExtensionManager.extensions,this.sortedFullSet=Object.keys(this.extensions).filter(function(key){return self.extensions[key].installInfo&&self.extensions[key].installInfo.locationType!==ExtensionManager.LOCATION_DEFAULT}),this._sortFullSet(),this._setInitialFilter(),this._countUpdates(),(new $.Deferred).resolve().promise()},InstalledViewModel.prototype._sortFullSet=function(){var self=this;this.sortedFullSet=this.sortedFullSet.sort(function(key1,key2){var ua1=self.extensions[key1].installInfo.updateAvailable,ua2=self.extensions[key2].installInfo.updateAvailable;if(ua1&&!ua2)return-1;if(!ua1&&ua2)return 1;var metadata1=self.extensions[key1].installInfo.metadata,metadata2=self.extensions[key2].installInfo.metadata,id1=(metadata1.title||metadata1.name).toLocaleLowerCase(),id2=(metadata2.title||metadata2.name).toLocaleLowerCase();return id1.localeCompare(id2)})},InstalledViewModel.prototype._countUpdates=function(){var self=this;this.notifyCount=0,this.sortedFullSet.forEach(function(key){self.extensions[key].installInfo.updateCompatible&&!ExtensionManager.isMarkedForUpdate(key)&&self.notifyCount++})},InstalledViewModel.prototype._handleStatusChange=function(e,id){var index=this.sortedFullSet.indexOf(id),refilter=!1;-1===index||this.extensions[id].installInfo?-1===index&&this.extensions[id].installInfo&&(this.sortedFullSet.push(id),this._sortFullSet(),refilter=!0):(this.sortedFullSet.splice(index,1),this._countUpdates(),refilter=!0),refilter&&this.filter(this._lastQuery||"",!0),this.extensions[id].installInfo&&this._countUpdates(),ExtensionManagerViewModel.prototype._handleStatusChange.call(this,e,id)},InstalledViewModel.prototype._getEntry=function(id){var entry=this.extensions[id];return entry?entry.installInfo:entry},DefaultViewModel.prototype=Object.create(ExtensionManagerViewModel.prototype),DefaultViewModel.prototype.constructor=DefaultViewModel,DefaultViewModel.prototype.source=ExtensionManagerViewModel.prototype.SOURCE_DEFAULT,DefaultViewModel.prototype._initializeFromSource=function(){var self=this;return this.extensions=ExtensionManager.extensions,this.sortedFullSet=Object.keys(this.extensions).filter(function(key){return self.extensions[key].installInfo&&self.extensions[key].installInfo.locationType===ExtensionManager.LOCATION_DEFAULT}).filter(function(key){return!ExtensionManager.isExtensionTakenDown(key)}),this._sortFullSet(),this._setInitialFilter(),(new $.Deferred).resolve().promise()},DefaultViewModel.prototype._sortFullSet=function(){var self=this;this.sortedFullSet=this.sortedFullSet.sort(function(key1,key2){var metadata1=self.extensions[key1].installInfo.metadata,metadata2=self.extensions[key2].installInfo.metadata,id1=(metadata1.title||metadata1.name).toLocaleLowerCase(),id2=(metadata2.title||metadata2.name).toLocaleLowerCase();return id1.localeCompare(id2)})},DefaultViewModel.prototype._getEntry=function(id){return this.extensions[id]?this.extensions[id].installInfo:null},ThemesViewModel.prototype=Object.create(ExtensionManagerViewModel.prototype),ThemesViewModel.prototype.constructor=ThemesViewModel,ThemesViewModel.prototype.source=ExtensionManagerViewModel.prototype.SOURCE_THEMES,ThemesViewModel.prototype._initializeFromSource=function(){var self=this;return ExtensionManager.downloadRegistry().done(function(){self.extensions=ExtensionManager.extensions,self._setSortedExtensionList(ExtensionManager.extensions,!0),self._setInitialFilter()}).fail(function(){self.extensions=[],self.sortedFullSet=[],self.filterSet=[]})},ThemesViewModel.prototype._getEntry=function(id){var entry=this.extensions[id];return entry?entry.registryInfo:entry},exports.RegistryViewModel=RegistryViewModel,exports.ThemesViewModel=ThemesViewModel,exports.InstalledViewModel=InstalledViewModel,exports.DefaultViewModel=DefaultViewModel});