-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Options Provider Base Options Controller
- Loading branch information
Showing
8 changed files
with
193 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/// <reference path='../_all.ts' /> | ||
|
||
module SitecoreExtensions.Options { | ||
'use strict'; | ||
|
||
export abstract class BaseOptionsController { | ||
name: string; | ||
model: any; | ||
optionsProvider: OptionsProvider; | ||
constructor(private $scope: any, formlyVersion: string, name: string) { | ||
this.name = name; | ||
this.optionsProvider = new OptionsProvider(); | ||
$scope.vm = this; | ||
$scope.vm.fields = this.getFields(); | ||
|
||
$scope.vm.env = { | ||
angularVersion: angular.version.full, | ||
formlyVersion: formlyVersion | ||
}; | ||
|
||
this.optionsProvider.getModuleOptions(name, (settings: IModuleOptions) => { | ||
$scope.$apply(function () { | ||
if (settings != null) { | ||
$scope.vm.model = settings.model; | ||
} | ||
}); | ||
}); | ||
|
||
$scope.vm.onSubmit = this.saveSettings; | ||
} | ||
|
||
abstract getFields(): any[]; | ||
|
||
buildModuleOptions(): ModuleOptionsBase { | ||
return new ModuleOptionsBase(this.name, this.model); | ||
} | ||
|
||
saveSettings() { | ||
var moduleOptions = this.buildModuleOptions() | ||
this.optionsProvider.setModuleOptions(moduleOptions); | ||
console.log(this.model); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/// <reference path='../../../typings/chrome/chrome.d.ts'/> | ||
/// <reference path='../../../typings/es6-shim/es6-shim.d.ts'/> | ||
|
||
module SitecoreExtensions.Options { | ||
'use strict'; | ||
|
||
export interface IModuleOptions { | ||
name: string; | ||
model: any; | ||
} | ||
|
||
export class ModuleOptionsBase implements IModuleOptions { | ||
name: string; | ||
model: any; | ||
constructor(name: string, model: any) { | ||
this.name = name; | ||
this.model = model; | ||
} | ||
} | ||
|
||
export class OptionsWrapper { | ||
options: IModuleOptions[] | ||
|
||
constructor(options: IModuleOptions[]) { | ||
this.options = options; | ||
} | ||
|
||
getModuleOptions(moduleName: string): IModuleOptions { | ||
return this.options.find(m => { return m.name == moduleName }); | ||
} | ||
|
||
setModuleOptions(options: IModuleOptions): void { | ||
var moduleOptions = this.getModuleOptions(options.name); | ||
var index = this.options.indexOf(moduleOptions); | ||
if (index !== -1) { | ||
this.options[index] = options; | ||
} else { | ||
this.options.push(options); | ||
} | ||
} | ||
|
||
static create(optionsWrapper: OptionsWrapper): OptionsWrapper { | ||
return new OptionsWrapper(optionsWrapper.options); | ||
} | ||
} | ||
|
||
declare type GetOptionsCallback = (arg: OptionsWrapper) => void; | ||
|
||
export class OptionsProvider { | ||
getOptions(done: GetOptionsCallback): void { | ||
chrome.storage.sync.get({ | ||
sc_ext_options: null, | ||
}, function (items: any) { | ||
done(items.sc_ext_options) | ||
}); | ||
} | ||
|
||
setOptions(moduleOptions: IModuleOptions[], done: any): void { | ||
chrome.storage.sync.set({ | ||
sc_ext_options: new OptionsWrapper(moduleOptions), | ||
}, done); | ||
} | ||
|
||
getModuleOptions(name: string, done: any): void { | ||
this.getOptions((optionsWrapper) => { | ||
if (optionsWrapper != null) { | ||
done(OptionsWrapper.create(optionsWrapper).getModuleOptions(name)); | ||
} | ||
}) | ||
} | ||
|
||
setModuleOptions(options: IModuleOptions): void { | ||
this.getOptions((optionsWrapper) => { | ||
if (optionsWrapper != null) { | ||
(OptionsWrapper.create(optionsWrapper)).setModuleOptions(options); | ||
} else { | ||
var array = new Array<IModuleOptions>(); | ||
array.push(options) | ||
optionsWrapper = new OptionsWrapper(array); | ||
} | ||
chrome.storage.sync.set({ sc_ext_options: optionsWrapper }); | ||
}) | ||
} | ||
} | ||
} |