Skip to content

Commit

Permalink
#8 | Create options page
Browse files Browse the repository at this point in the history
Options Provider
Base Options Controller
  • Loading branch information
alan-null committed May 24, 2016
1 parent d1d17da commit 6725f7e
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 101 deletions.
3 changes: 2 additions & 1 deletion app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
"https://*/*",
"storage"
],
"content_scripts": [
{
Expand Down
4 changes: 4 additions & 0 deletions app/options/_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
/// <reference path='typings/angularjs/angular.d.ts' />
/// <reference path='typings/angular-formly/angular-formly.d.ts' />

// Providers
/// <reference path='providers/OptionsProvider.ts' />

// Controllers
/// <reference path='controllers/BaseOptionsController.ts' />
/// <reference path='controllers/OptionsController.ts' />
/// <reference path='controllers/LauncherController.ts' />
/// <reference path='controllers/DatabaseColorController.ts' />
Expand Down
44 changes: 44 additions & 0 deletions app/options/controllers/BaseOptionsController.ts
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);
}
}
}
38 changes: 14 additions & 24 deletions app/options/controllers/DatabaseColorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@

module SitecoreExtensions.Options {
'use strict';
export class DatabaseColorController {
model: any;
constructor(private $scope: any) {
$scope.vm = this;

$scope.vm.onSubmit = this.saveSettings;

export class DatabaseColorController extends BaseOptionsController {
constructor($scope: any, formlyVersion: string) {
super($scope, formlyVersion, 'Database Color')
$scope.vm.title = 'Database Color module';
$scope.vm.env = {
angularVersion: angular.version.full,
formlyVersion: "8.2.1"
};

$scope.vm.model = {
enabled: true
};

$scope.vm.fields = [{
key: 'enabled',
type: 'checkbox',
templateOptions: {
label: 'Enabled'
}
}];
}

saveSettings() {
console.log(this.model);
getFields() {
return [
{
key: 'enabled',
type: 'checkbox',
defaultValue: true,
templateOptions: {
label: 'Enabled'
}
},
]
}
}
}
41 changes: 15 additions & 26 deletions app/options/controllers/DatabaseNameController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,23 @@

module SitecoreExtensions.Options {
'use strict';
export class DatabaseNameController {
model: any;
constructor(private $scope: any) {
$scope.vm = this;

$scope.vm.onSubmit = this.saveSettings;

export class DatabaseNameController extends BaseOptionsController {
constructor($scope: any, formlyVersion: string) {
super($scope, formlyVersion, 'Database Name')
$scope.vm.title = 'Database Name module';
$scope.vm.env = {
angularVersion: angular.version.full,
formlyVersion: "8.2.1"
};

$scope.vm.model = {
enabled: true
};

$scope.vm.fields = [{
key: 'enabled',
type: 'checkbox',
templateOptions: {
label: 'Enabled'
}
}];
}

saveSettings() {
console.log(this.model);
getFields() {
return [
{
key: 'enabled',
type: 'checkbox',
defaultValue: true,
templateOptions: {
label: 'Enabled'
}
},
]
}
}
}
}
38 changes: 14 additions & 24 deletions app/options/controllers/GeneralOptionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@

module SitecoreExtensions.Options {
'use strict';
export class GeneralOptionsController {
model: any;
constructor(private $scope: any) {
$scope.vm = this;

$scope.vm.onSubmit = this.saveSettings;

export class GeneralOptionsController extends BaseOptionsController {
constructor($scope: any, formlyVersion: string) {
super($scope, formlyVersion, 'General')
$scope.vm.title = 'Sitecore Extensions general options';
$scope.vm.env = {
angularVersion: angular.version.full,
formlyVersion: "8.2.1"
};

$scope.vm.model = {
enabled: true
};

$scope.vm.fields = [{
key: 'enabled',
type: 'checkbox',
templateOptions: {
label: 'Enabled'
}
}];
}

saveSettings() {
console.log(this.model);
getFields() {
return [
{
key: 'enabled',
type: 'checkbox',
defaultValue: true,
templateOptions: {
label: 'Enabled'
}
},
]
}
}
}
41 changes: 15 additions & 26 deletions app/options/controllers/LauncherController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,23 @@

module SitecoreExtensions.Options {
'use strict';
export class LauncherController {
model: any;
constructor(private $scope: any) {
$scope.vm = this;

$scope.vm.onSubmit = this.saveSettings;

export class LauncherController extends BaseOptionsController {
constructor($scope: any, formlyVersion: string) {
super($scope, formlyVersion, 'Launcher')
$scope.vm.title = 'Launcher module';
$scope.vm.env = {
angularVersion: angular.version.full,
formlyVersion: "8.2.1"
};

$scope.vm.model = {
enabled: true
};

$scope.vm.fields = [{
key: 'enabled',
type: 'checkbox',
templateOptions: {
label: 'Enabled'
}
}];
}

saveSettings() {
console.log(this.model);
getFields() {
return [
{
key: 'enabled',
type: 'checkbox',
defaultValue: true,
templateOptions: {
label: 'Enabled'
}
},
]
}
}
}
}
85 changes: 85 additions & 0 deletions app/options/providers/OptionsProvider.ts
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 });
})
}
}
}

0 comments on commit 6725f7e

Please sign in to comment.