( function ( mw ) { /** * @typedef {Object} ext.popups.UserSettings */ var IS_ENABLED_KEY = 'mwe-popups-enabled', PREVIEW_COUNT_KEY = 'ext.popups.core.previewCount'; /** * Given the global state of the application, creates an object whose methods * encapsulate all interactions with the given User Agent's storage. * * @param {mw.storage} storage The `mw.storage` singleton instance * @param {mw.user} user The `mw.user` singleton instance * * @return {ext.popups.UserSettings} */ mw.popups.createUserSettings = function ( storage, user ) { return { /** * Gets whether or not the user has previously enabled Page Previews. * * N.B. that if the user hasn't previously enabled or disabled Page * Previews, i.e. mw.popups.userSettings.setIsEnabled(true), then they * are treated as if they have enabled them. * * @return {Boolean} */ getIsEnabled: function () { return storage.get( IS_ENABLED_KEY ) !== '0'; }, /** * Sets whether or not the user has enabled Page Previews. * * @param {Boolean} isEnabled */ setIsEnabled: function ( isEnabled ) { storage.set( IS_ENABLED_KEY, isEnabled ? '1' : '0' ); }, /** * Gets whether or not the user has previously enabled **or disabled** * Page Previews. * * @return {Boolean} */ hasIsEnabled: function () { return storage.get( IS_ENABLED_KEY, undefined ) !== undefined; }, /** * Gets the user's Page Previews token. * * If the storage doesn't contain a token, then one is generated and * persisted to the storage before being returned. * * @return {String} */ getToken: function () { var key = 'PopupsExperimentID', id = storage.get( key ); if ( !id ) { id = user.generateRandomSessionId(); storage.set( key, id ); } return id; }, /** * Gets the number of Page Previews that the user has seen. * * If the storage isn't available, then -1 is returned. * * @return {Number} */ getPreviewCount: function () { var result = storage.get( PREVIEW_COUNT_KEY ); if ( result === false ) { return -1; } else if ( result === null ) { return 0; } return parseInt( result, 10 ); }, /** * Sets the number of Page Previews that the user has seen. * * @param {Number} count */ setPreviewCount: function ( count ) { storage.set( PREVIEW_COUNT_KEY, count.toString() ); } }; }; }( mediaWiki ) );