Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
utils
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
EIT-SWE
ux
utils
Commits
67a37a9e
Commit
67a37a9e
authored
Jan 23, 2020
by
Shane Eckenrode
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add theming entrypoint with stylemanager service
parent
443c5aa6
Changes
14
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
251 additions
and
1 deletion
+251
-1
angular.json
angular.json
+28
-0
libs/utils/theming/README.md
libs/utils/theming/README.md
+7
-0
libs/utils/theming/jest.config.js
libs/utils/theming/jest.config.js
+9
-0
libs/utils/theming/ng-package.json
libs/utils/theming/ng-package.json
+5
-0
libs/utils/theming/src/index.ts
libs/utils/theming/src/index.ts
+1
-0
libs/utils/theming/src/lib/style-manager.spec.ts
libs/utils/theming/src/lib/style-manager.spec.ts
+56
-0
libs/utils/theming/src/lib/style-manager.ts
libs/utils/theming/src/lib/style-manager.ts
+95
-0
libs/utils/theming/src/test-setup.ts
libs/utils/theming/src/test-setup.ts
+1
-0
libs/utils/theming/tsconfig.json
libs/utils/theming/tsconfig.json
+7
-0
libs/utils/theming/tsconfig.lib.json
libs/utils/theming/tsconfig.lib.json
+20
-0
libs/utils/theming/tsconfig.spec.json
libs/utils/theming/tsconfig.spec.json
+10
-0
libs/utils/theming/tslint.json
libs/utils/theming/tslint.json
+7
-0
nx.json
nx.json
+3
-0
tsconfig.json
tsconfig.json
+2
-1
No files found.
angular.json
View file @
67a37a9e
...
...
@@ -259,6 +259,34 @@
"styleext"
:
"scss"
}
}
},
"utils-theming"
:
{
"projectType"
:
"library"
,
"root"
:
"libs/utils/theming"
,
"sourceRoot"
:
"libs/utils/theming/src"
,
"prefix"
:
"psu"
,
"architect"
:
{
"lint"
:
{
"builder"
:
"@angular-devkit/build-angular:tslint"
,
"options"
:
{
"tsConfig"
:
[
"libs/utils/theming/tsconfig.lib.json"
,
"libs/utils/theming/tsconfig.spec.json"
],
"exclude"
:
[
"**/node_modules/**"
,
"!libs/utils/theming/**"
]
}
},
"test"
:
{
"builder"
:
"@nrwl/jest:jest"
,
"options"
:
{
"jestConfig"
:
"libs/utils/theming/jest.config.js"
,
"tsConfig"
:
"libs/utils/theming/tsconfig.spec.json"
,
"setupFile"
:
"libs/utils/theming/src/test-setup.ts"
}
}
},
"schematics"
:
{
"@nrwl/angular:component"
:
{
"styleext"
:
"scss"
}
}
}
},
"cli"
:
{
...
...
libs/utils/theming/README.md
0 → 100644
View file @
67a37a9e
# utils-theming
This library was generated with
[
Nx
](
https://nx.dev
)
.
## Running unit tests
Run
`nx test utils-theming`
to execute the unit tests.
libs/utils/theming/jest.config.js
0 → 100644
View file @
67a37a9e
module
.
exports
=
{
name
:
'
utils-theming
'
,
preset
:
'
../../../jest.config.js
'
,
coverageDirectory
:
'
../../../coverage/libs/utils/theming
'
,
snapshotSerializers
:
[
'
jest-preset-angular/AngularSnapshotSerializer.js
'
,
'
jest-preset-angular/HTMLCommentSerializer.js
'
]
};
libs/utils/theming/ng-package.json
0 → 100644
View file @
67a37a9e
{
"lib"
:
{
"entryFile"
:
"src/index.ts"
}
}
libs/utils/theming/src/index.ts
0 → 100644
View file @
67a37a9e
export
{
StyleManager
,
STYLE_MANAGER_CONFIG
,
Theme
}
from
'
./lib/style-manager
'
;
libs/utils/theming/src/lib/style-manager.spec.ts
0 → 100644
View file @
67a37a9e
import
{
HttpClientTestingModule
}
from
'
@angular/common/http/testing
'
;
import
{
inject
,
TestBed
}
from
'
@angular/core/testing
'
;
import
{
StyleManager
}
from
'
./style-manager
'
;
describe
(
'
StyleManager
'
,
()
=>
{
let
styleManager
:
StyleManager
;
beforeEach
(()
=>
TestBed
.
configureTestingModule
({
imports
:
[
HttpClientTestingModule
],
providers
:
[
StyleManager
]
})
);
beforeEach
(
inject
([
StyleManager
],
(
sm
:
StyleManager
)
=>
{
styleManager
=
sm
;
}));
afterEach
(()
=>
{
const
links
=
document
.
head
.
querySelectorAll
(
'
link
'
);
// tslint:disable-next-line: prefer-const
for
(
let
link
of
Array
.
prototype
.
slice
.
call
(
links
))
{
if
(
link
.
className
.
includes
(
'
style-manager-
'
))
{
document
.
head
.
removeChild
(
link
);
}
}
});
it
(
'
should add stylesheet to head
'
,
()
=>
{
styleManager
.
setStyle
(
'
test
'
,
'
test.css
'
);
const
styleEl
=
document
.
head
.
querySelector
(
'
.style-manager-test
'
)
as
HTMLLinkElement
;
expect
(
styleEl
).
not
.
toBeNull
();
expect
(
styleEl
.
href
.
endsWith
(
'
test.css
'
)).
toBe
(
true
);
});
it
(
'
should change existing stylesheet
'
,
()
=>
{
styleManager
.
setStyle
(
'
test
'
,
'
test.css
'
);
const
styleEl
=
document
.
head
.
querySelector
(
'
.style-manager-test
'
)
as
HTMLLinkElement
;
expect
(
styleEl
).
not
.
toBeNull
();
expect
(
styleEl
.
href
.
endsWith
(
'
test.css
'
)).
toBe
(
true
);
styleManager
.
setStyle
(
'
test
'
,
'
new.css
'
);
expect
(
styleEl
.
href
.
endsWith
(
'
new.css
'
)).
toBe
(
true
);
});
it
(
'
should remove existing stylesheet
'
,
()
=>
{
styleManager
.
setStyle
(
'
test
'
,
'
test.css
'
);
let
styleEl
=
document
.
head
.
querySelector
(
'
.style-manager-test
'
)
as
HTMLLinkElement
;
expect
(
styleEl
).
not
.
toBeNull
();
expect
(
styleEl
.
href
.
endsWith
(
'
test.css
'
)).
toBe
(
true
);
styleManager
.
removeStyle
(
'
test
'
);
styleEl
=
document
.
head
.
querySelector
(
'
.style-manager-test
'
)
as
HTMLLinkElement
;
expect
(
styleEl
).
toBeNull
();
});
});
libs/utils/theming/src/lib/style-manager.ts
0 → 100644
View file @
67a37a9e
import
{
Inject
,
Injectable
,
InjectionToken
}
from
'
@angular/core
'
;
export
interface
Theme
{
name
:
string
;
primary
:
string
;
isDefault
?:
boolean
;
isDark
?:
boolean
;
}
export
const
STYLE_MANAGER_CONFIG
=
new
InjectionToken
<
Theme
[]
>
(
'
style.manager.config
'
);
/**
* Class for managing stylesheets. Stylesheets are loaded into named slots so that they can be
* removed or changed later.
*/
@
Injectable
({
providedIn
:
'
root
'
})
export
class
StyleManager
{
private
_currentTheme
:
Theme
;
private
_availableThemes
:
Theme
[];
constructor
(@
Inject
(
STYLE_MANAGER_CONFIG
)
private
config
:
Theme
[])
{
this
.
availableThemes
=
config
;
this
.
currentTheme
=
this
.
availableThemes
.
find
(
theme
=>
theme
.
isDefault
);
}
public
set
currentTheme
(
theme
:
Theme
)
{
this
.
_currentTheme
=
theme
;
if
(
theme
.
isDefault
)
{
this
.
removeStyle
(
'
theme
'
);
}
else
{
// This may need adjusted to allow the path to be configurable later.
this
.
setStyle
(
'
theme
'
,
`alt-themes/
${
theme
.
name
}
.css`
);
}
}
public
get
currentTheme
():
Theme
{
return
this
.
_currentTheme
;
}
public
set
availableThemes
(
themes
:
Theme
[])
{
this
.
_availableThemes
=
themes
;
}
public
get
availableThemes
():
Theme
[]
{
return
this
.
_availableThemes
;
}
public
setCurrentTheme
(
themeName
:
string
):
void
{
const
newTheme
=
this
.
availableThemes
.
find
(
theme
=>
theme
.
name
===
themeName
);
if
(
!
newTheme
)
{
console
.
warn
(
`Couldn't find a theme with name
${
themeName
}
.`
);
}
else
{
this
.
currentTheme
=
newTheme
;
}
}
/**
* Set the stylesheet with the specified key.
*/
private
setStyle
(
key
:
string
,
href
:
string
):
void
{
getLinkElementForKey
(
key
).
setAttribute
(
'
href
'
,
href
);
}
/**
* Remove the stylesheet with the specified key.
*/
private
removeStyle
(
key
:
string
):
void
{
const
existingLinkElement
=
getExistingLinkElementByKey
(
key
);
if
(
existingLinkElement
)
{
document
.
head
.
removeChild
(
existingLinkElement
);
}
}
}
function
getLinkElementForKey
(
key
:
string
):
Element
{
return
getExistingLinkElementByKey
(
key
)
||
createLinkElementWithKey
(
key
);
}
function
getExistingLinkElementByKey
(
key
:
string
):
Element
{
return
document
.
head
.
querySelector
(
`link[rel="stylesheet"].
${
getClassNameForKey
(
key
)}
`
);
}
function
createLinkElementWithKey
(
key
:
string
):
Element
{
const
linkEl
=
document
.
createElement
(
'
link
'
);
linkEl
.
setAttribute
(
'
rel
'
,
'
stylesheet
'
);
linkEl
.
classList
.
add
(
getClassNameForKey
(
key
));
document
.
head
.
appendChild
(
linkEl
);
return
linkEl
;
}
function
getClassNameForKey
(
key
:
string
):
string
{
return
`style-manager-
${
key
}
`
;
}
libs/utils/theming/src/test-setup.ts
0 → 100644
View file @
67a37a9e
import
'
jest-preset-angular
'
;
libs/utils/theming/tsconfig.json
0 → 100644
View file @
67a37a9e
{
"extends"
:
"../../../tsconfig.json"
,
"compilerOptions"
:
{
"types"
:
[
"node"
,
"jest"
]
},
"include"
:
[
"**/*.ts"
]
}
libs/utils/theming/tsconfig.lib.json
0 → 100644
View file @
67a37a9e
{
"extends"
:
"./tsconfig.json"
,
"compilerOptions"
:
{
"outDir"
:
"../../../dist/out-tsc"
,
"target"
:
"es2015"
,
"declaration"
:
true
,
"inlineSources"
:
true
,
"types"
:
[],
"lib"
:
[
"dom"
,
"es2018"
]
},
"angularCompilerOptions"
:
{
"annotateForClosureCompiler"
:
true
,
"skipTemplateCodegen"
:
true
,
"strictMetadataEmit"
:
true
,
"fullTemplateTypeCheck"
:
true
,
"strictInjectionParameters"
:
true
,
"enableResourceInlining"
:
true
},
"exclude"
:
[
"src/test-setup.ts"
,
"**/*.spec.ts"
]
}
libs/utils/theming/tsconfig.spec.json
0 → 100644
View file @
67a37a9e
{
"extends"
:
"./tsconfig.json"
,
"compilerOptions"
:
{
"outDir"
:
"../../../dist/out-tsc"
,
"module"
:
"commonjs"
,
"types"
:
[
"jest"
,
"node"
]
},
"files"
:
[
"src/test-setup.ts"
],
"include"
:
[
"**/*.spec.ts"
,
"**/*.d.ts"
]
}
libs/utils/theming/tslint.json
0 → 100644
View file @
67a37a9e
{
"extends"
:
"../../../tslint.json"
,
"rules"
:
{
"directive-selector"
:
[
true
,
"attribute"
,
"ut"
,
"camelCase"
],
"component-selector"
:
[
true
,
"element"
,
"ut"
,
"kebab-case"
]
}
}
nx.json
View file @
67a37a9e
...
...
@@ -28,6 +28,9 @@
},
"utils-angular"
:
{
"tags"
:
[]
},
"utils-theming"
:
{
"tags"
:
[]
}
}
}
tsconfig.json
View file @
67a37a9e
...
...
@@ -20,7 +20,8 @@
"@psu/utils/security"
:
[
"libs/utils/security/src/index.ts"
],
"@psu/utils/rx"
:
[
"libs/utils/rx/src/index.ts"
],
"@psu/utils/browser"
:
[
"libs/utils/browser/src/index.ts"
],
"@psu/utils/angular"
:
[
"libs/utils/angular/src/index.ts"
]
"@psu/utils/angular"
:
[
"libs/utils/angular/src/index.ts"
],
"@psu/utils/theming"
:
[
"libs/utils/theming/src/index.ts"
]
}
},
"exclude"
:
[
"node_modules"
,
"tmp"
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment