-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contextual help added to view creation (#416)
- Loading branch information
1 parent
f15f04c
commit 227c70e
Showing
11 changed files
with
234 additions
and
16 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,19 @@ | ||
.help-view | ||
{ | ||
a::after { | ||
@extend %icon-font; | ||
display: block; | ||
font-size: 1.125rem; | ||
color: $brand-secundary; | ||
max-width: 1.125rem; | ||
content: "\E816"; | ||
} | ||
|
||
label { | ||
font-weight: bold; | ||
|
||
&.checkbox-label { | ||
font-weight: normal; | ||
} | ||
} | ||
} |
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,6 @@ | ||
import { initializeComponent } from "component"; | ||
import HelpView from "./lib/component"; | ||
|
||
export default (scope) =>{ | ||
initializeComponent(scope, ".help-view", HelpView); | ||
}; |
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,54 @@ | ||
import HelpView from "./component"; | ||
|
||
declare global { | ||
interface Window { | ||
$: JQueryStatic; | ||
alert: (message?: any)=>void; | ||
} | ||
} | ||
|
||
window.$ = require("jquery"); | ||
|
||
export {}; | ||
|
||
class TestHelpView extends HelpView { | ||
public get button() { | ||
return this.$button; | ||
} | ||
} | ||
|
||
describe("help view tests", ()=> { | ||
it("throws an error when help-text is not provided", ()=> { | ||
const element = document.createElement("div"); | ||
expect(()=>new HelpView(element)).toThrow("help-text is required"); | ||
}); | ||
|
||
it("throws when help-target is not provided", () =>{ | ||
const element = document.createElement("div"); | ||
element.setAttribute("data-help-text", "help text"); | ||
expect(()=>new HelpView(element)).toThrow("help-target is required"); | ||
}); | ||
|
||
it("throws when help-target is not found", ()=>{ | ||
const element = document.createElement("div"); | ||
element.setAttribute("data-help-text", "help text"); | ||
const helpTarget = "help-target"; | ||
element.setAttribute("data-help-target", helpTarget); | ||
expect(()=>new HelpView(element)).toThrow(`Could not find help target with id: ${helpTarget}`); | ||
}); | ||
|
||
it("links the help text to the help target", ()=>{ | ||
const element = document.createElement("div"); | ||
element.setAttribute("data-help-text", "help text"); | ||
const helpTarget = "help-target"; | ||
element.setAttribute("data-help-target", helpTarget); | ||
document.body.appendChild(element); | ||
const target = document.createElement("div"); | ||
target.id = helpTarget; | ||
document.body.appendChild(target); | ||
const v = new TestHelpView(element); | ||
expect(v.button?.length).toBe(1); | ||
v.button?.trigger("click"); | ||
expect($(target).text()).toBe("help text"); | ||
}); | ||
}); |
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,47 @@ | ||
import { Component } from "component"; | ||
import { MarkDown } from "util/formatters/markdown"; | ||
|
||
/** | ||
* @class HelpView | ||
* @extends Component | ||
* @description A component that displays help text in a target. | ||
*/ | ||
export default class HelpView extends Component { | ||
// This is protected so that it can be accessed in tests. | ||
protected $button: JQuery<HTMLAnchorElement>; | ||
/** | ||
* @constructor Create a new HelpView component. | ||
* @param element The Element to attach the component to. | ||
*/ | ||
constructor(element: HTMLElement) { | ||
super(element); | ||
this.initHelp(element); | ||
} | ||
|
||
/** | ||
* @method initHelp | ||
* @description Initialize the help view. | ||
* @param element The element to attach the help view to. | ||
*/ | ||
initHelp(element: HTMLElement) { | ||
const $el = $(element); | ||
const $label = $el.find("label").parent(); | ||
// Yes, I know it's not a button, but $a just didn't feel right! | ||
const $button = $(document.createElement("a")); | ||
if ($label && $label.length > 0) { | ||
$button.addClass("btn").addClass("btn-plain").addClass("btn-help").attr("role", "button").attr("type", "button"); | ||
$label.first().append($button); | ||
} | ||
this.$button = $button; | ||
const helpText = $el.data("help-text"); | ||
if (!helpText) throw new Error("help-text is required"); | ||
const helpTitle = $el.data("help-title"); | ||
const helpTarget = $el.data("help-target"); | ||
if (!helpTarget) throw new Error("help-target is required"); | ||
const target = document.getElementById(helpTarget); | ||
if (!target) throw new Error(`Could not find help target with id: ${helpTarget}`); | ||
$button.on("click", () => { | ||
target.innerHTML = MarkDown`${helpTitle ? `### ${helpTitle}` : ""}\n${helpText}`; | ||
}); | ||
} | ||
} |
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,33 @@ | ||
import { MarkDown } from "./markdown"; | ||
|
||
describe("Markdown formatter tests", ()=>{ | ||
it("Should return empty string", ()=>{ | ||
const expected = ""; | ||
const result = MarkDown``; | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
it("Should return basic string with formatted value when no markdown is given", ()=>{ | ||
const expected = "<p>test</p>"; | ||
const result = MarkDown`test`; | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
it("Should return properly formatted text", ()=>{ | ||
const expected = "<h1>test</h1>"; | ||
const result = MarkDown`# test`; | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
it("Should return properly formatted text with various inputs", ()=>{ | ||
const expected = "<h1>test</h1>\n<p>test <em>test</em></p>"; | ||
const result = MarkDown`# test\ntest *test*`; | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
it("Should return properly formatted text with newlines", ()=>{ | ||
const expected = "<h1>test</h1>\n<p>test</p>\n<p>test</p>"; | ||
const result = MarkDown`# test\\ntest\\ntest`; | ||
expect(result).toBe(expected); | ||
}); | ||
}); |
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,20 @@ | ||
import { marked } from "marked"; | ||
|
||
type MarkdownCode = string; | ||
|
||
type stringLike = { toString(): string }; | ||
|
||
function MarkDown(strings: TemplateStringsArray, ...values:(stringLike| string|number|MarkdownCode)[]): MarkdownCode { | ||
marked.use({breaks: true}) | ||
let str = ''; | ||
for (let i = 0; i < strings.length; i++) { | ||
str += strings[i]; | ||
if (i < values.length) { | ||
str += values[i] as string ? values[i] : values[i] as MarkdownCode ? values[i] : values[i] as stringLike ? values[i].toString() : String(values[i]); | ||
} | ||
} | ||
str = str.replace(/\\n/g, '\n\n'); | ||
return marked(str).trim(); | ||
} | ||
|
||
export {MarkdownCode, MarkDown}; |
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