2.4.18 • Published 5 years ago

knob-js v2.4.18

Weekly downloads
141
License
MIT
Repository
github
Last release
5 years ago

knobjs

A component library for knockout.js to build skinnable UIs with ease.

Knobjs is a component library based on knockoutjs. The main goal of the lib is to ease the way of creating skinnable / themed UIs. The components communicate through ko.observables. Other than knockout, its dependency is superdata which is a general data layer which can be used with any client-side frameworks.

knob-button

This is one of the most basic components in knobjs. It works like you would expect it from a button. If you click on it, something will be triggered. This is why most of the components in knobjs are using this element. Each components where some kind of changes should be triggered by a click are implemented with knob-buttons.

Params

ParamTypeRequiredDefault valueDescription
variationstringNo"default"The variation of the button. See the style section.
labelstringPartially **The text which will be written on the button.
iconLeftstringPartially **The id of the icon on the left of the label.
iconRightstringPartially **The id of the icon on the right of the label
iconstringPartially **This is a synonim for iconLeft.
clickfunctionNoThis is the callback which will be called on click.

** At least one of these params has to be given, otherwise an error will be thrown. It's because you probably don't want to create a totally empty button.

Example

<knob-button class="button--sm" params="
	label: 'Search',
	variation: 'primary',
	icon: '#icon-search',
	click: clickCallback">
</knob-button>

<script>
	ko.applyBindings({
		clickCallback: function() {
			alert("search clicked")
		}
	});
</script>

knob-toggleSwitch

This switch component is used for enabling or disabling features. If you click on it, you toggle the value property associated with the component.

Params

ParamTypeRequiredDefault valueDescription
valueko.observable(boolean)Yesko.observalble(false)With this observable, you can set the default state of the switch button.
variationstringNoBy giving "square" value to variation, the component can be set to have rectangular button.

Example

<knob-toggleswitch params="
	value: toggleValue,
	variation: 'square'">
</knob-toggleswitch>

<script>
	ko.applyBindings({
		toggleValue: ko.observable(false)
	});
</script>

knob-notification

If you place this component somewhere in your html code, knob attaches an object called notifications to its interface. It doesn't need params like other components, You can use it like this:

	<knob-notification></knob-notification>

After that, you can call the following 4 functions in javascript code

	window.knob.notifications.showError("Error text", 1111)
	window.knob.notifications.showWarning("Warning text", 1111)
	window.knob.notifications.showSuccess("Succes text", 1111)
	window.knob.notifications.showLoading("Loading text", 1111)

They all have 2 parameters, the text to show, and the time to live in ms. After the given time the notifications will disappear. The notifications will have position: absolute, and top: 0 css parameters on them.

knob-input

This is the other most basic component in knobjs. It's just a simple input, but you can style it in the knob way.

Params

ParamTypeRequiredDefault valueDescription
variationstringNo"default"The variation of the button. See the style section.
typestringNo"text"The type of the input. (Eg.: 'password')
valueko.observableYesThis is the observable in which the value will be written. If you want to use the value of the input, then you must give it as a parameter.
hasFocusko.observable (boolean)Noko.observable (false)With this observable, you can programmatically set the focus to this input.

You can use "primary" variation instead of "default" in order to use a style depending on primary color. In this case, default state's fill color and border-color will be the primary color. Hover and active states will use darkened or lightened primary colors depending whether primary color is a dark color or not.

Example

<knob-input params="
	type: 'password',
	value: value">
</knob-input>

<knob-input params="
	variation: 'primary',
	value: value2">
</knob-input>

<script>
	ko.applyBindings({
		value: ko.observable(""),
		value2: ko.observable("")
	});
</script>

knob-radio

This component is built on top of the knob-button component and it implements a radio button behaviour. This means that you can select one of it's elements and only one element can be selected from the group. Since it is composed from knob-button elements, you can add icons and labels to it's elements.

Params

ParamTypeRequiredDefault valueDescription
groupstringYesThis value will indicate in which group the buttons will be. You can connect multiple knob-radios by putting them into the same group.
selectedko.observableNoThe selected element will be put into this observable.
selectedIdxko.observable (number)NoThe selected index will be written into this observable. Also, you can set the default selection by giving a value to this observable.
itemsArrayYesYou can configure the buttons within the component, so every parameter which you can pass to buttons are valid in this array. Also, there is an extra value parameter.

Example

<knob-radio class="knob-radio--inline" params="
	group: 'test',
	selected: selectedElement,
	selectedIdx: selectedIndex,
	items: [
		{icon: '#icon-grid', value: {field: 'id desc', asc: true}},
		{icon: '#icon-list', value: {field: 'id', asc: false}},
		{label: 'Third option', value: {field: 'name', asc: true}}
	]">
</knob-radio>

<script>
	ko.applyBindings({
		selectedElement: ko.observable(),
		selectedIndex: ko.observable(1)
	});
</script>

knob-dropdown

This component is also composed of knob-buttons. Therefore every element can have it's icon and label. It also can have an icon on the right, which is usually a caret pointing down to indicate the options will be visible if you click on it. In a sense, it's very similar to the knob-radio element.

Params

ParamTypeRequiredDefault valueDescription
selectedko.observableYesThe selected element. This is not visible in the dropdown list.
rightIconstringNoThe icon on the right of the selected element. Should be a caret to indicate that something will appear on the bottom if you click on it.
itemsArray or ko.observableArrayYesThe items in the dropdown. They are configured just like buttons and they also can have a value property.
selectedIdxnumber or ko.observableNo0The selected index. If it's an observable, is it possible to get or set selected index after creating component

You can give ko.observableArray as items parameter. This way you can change dropdown's items after creating component by setting value of observableArray to other items array. It throws error if given new value is invalid (each element of array value should contain label and/or icon property).

If you give an observable as selectedIdx parameter, you can change selected item after creating component by setting value of selectedIdx observable. If given selectedIdx value is not a valid index, it will be changed to 0.

If an item is selected or its position changes caused by setting items observableArray's value, selectedIdx observable will be automatically refreshed to the new selected index.

Example

<knob-dropdown params="
	selected: selectedElement,
	rightIcon: '#icon-down',
	items: [
		{label: 'id', icon: '#icon-a-z', value: {field: 'id desc', asc: true}},
		{label: 'id', icon: '#icon-z-a', value: {field: 'id', asc: false}},
		{label: 'name', icon: '#icon-a-z', value: {field: 'name', asc: true}},
		{label: 'name', icon: '#icon-z-a', value: {field: 'name', asc: false}}
	]">
</knob-dropdown>

<script>
	ko.applyBindings({
		selectedElement: ko.observable()
	});
</script>

Example for giving ko.observableArray() as items parameter can be found in ./examples/knob.html and ./examples/knob.js

knob-inline-text-editor

It's a quite common task to edit a short text. This component has one major boolean state variable, the editMode. If the edit mode is false, then the text value is shown as a text node and there is an edit button next to it. If you click on the edit mode, then it's replaced with a knob-input and a save and a cancel button. The save and cancel functionality can be triggered with pressing enter and the esc key.

Params

ParamTypeRequiredDefault valueDescription
valueko.observableYesThe initial value is read from this observable and the changes will be written back to it. (Only if you save the changes.)

Example

<knob-inline-text-editor params="
	value: value">
</knob-inline-text-editor>

<script>
	ko.applyBindings({
		value: ko.observable("")
	});
</script>

knob-items-per-page

This component is used in the knob-paged-list component. It's basically responsible for calculating the actual number of pages based on the number of items and the items per page value. This value is selected from a dropdown list.

Params

ParamTypeRequiredDefault valueDescription
numOfItemsko.observable (number)YesThe number of items in a result set.
itemsPerPageko.observable (number)YesThe number of items on a page.
numOfPagesko.observable (number)YesThe calculated number of pages, which are based on the previous two values.
itemsPerPageListArrayYesThe available numbers of items which will show up in the dropdown.

Example

<knob-items-per-page params="
	numOfItems: numOfItems,
	itemsPerPage: itemsPerPage,
	numOfPages: numOfPages,
	itemsPerPageList: [
		10,
		25,
		50,
		100
	]">
</knob-items-per-page>

<script>
	ko.applyBindings({
		numOfItems: ko.observable(1230),
		itemsPerPage: ko.observable(10),
		numOfPages: ko.observable()
	});
</script>

knob-pagination

This component is used by the knob-paged-list as well. It's very tightly bound with the knob-items-per-page. Basically if you pass the same numOfPages ko.observable to the two components, then they will smoothly function together.

Params

ParamTypeRequiredDefault valueDescription
numOfPagesko.observableYesYou can set the total number of pages with this property. The page selector items will be generated based on this variable.
currentPageko.observableYesko.observable(0)The selected page's value will be written into this ko.observable
afterHeadnumberNo2The number of page selectors to be visible at the beginning of the list.
beforeTailnumberNo2The number of page selectors to be visible at the end of the list.
beforeCurrentnumberNo2The number of page selectors to be visible before the currently selected item.
afterCurrentnumberNo2The number of page selectors to be visible after the currently selected element.

Example

<knob-pagination params="
	numOfPages: numOfPages,
	currentPage: current,
	afterHead: 3,
	beforeTail: 3,
	beforeCurrent: 4,
	afterCurrent: 4">
</knob-pagination>

<script>
	ko.applyBindings({
		current: ko.observable(0),
		numOfPages: ko.observable(100)
	});
</script>

knob-paged-list

An easily configurable general paged list with which you can list any kind of data. You can create project lists, galleries or anything related to listing by using this module. The child template can be anything of this component depending on the data you want to show in the list.

The main dependency of the module is superdata's store module.

Params

ParamTypeRequiredDefault valueDescription
storesuperdata.storeYesA store instance from superdata. Every data reading and writing goes through this module.
searchstringYesThe field's name based on which the filtering should work.
sortArrayYesThe field names based on which the you want to have a sorting option.

Example

<knob-paged-list params="store: store, search: 'title', sort: ['id', 'name']">
	<div>
		<span data-bind="text: model.data.id"></span>
		<span data-bind="text: model.data.email"></span>
		<span data-bind="text: model.data.name"></span>
		<span data-bind="text: model.data.title"></span>
	</div>
</knob-paged-list>

<script>
	ko.applyBindings({
		store: store //where store is a superdata.store.store instance
	});
</script>

knob-tabs

With the knob-tabs component, you can easily create tabbed user interfaces. This component is special in the sense that it works only if you add knob-tab components as child components. The params of the knob-tab components will be applied to a knob-radio component, which will be responsible for selecting the visible tab. You can change easily the tab orientation, with only one css selector. Now available:

  • knob-tab-orient--left-top
  • knob-tab-orient--top-left
  • knob-tab-orient--top-center
  • knob-tab-orient--top-right

Params

ParamTypeRequiredDefault valueDescription
defaultTabnumberNo0You can set the default selected tab by giving the zero-based index of it.
variationstringNo"tab"Possible values: "tab", "tab-transparent"

Also, the knob-tab child elements has to have at least one of the following parameters:

  • label
  • icon (a synonim for leftIcon)
  • leftIcon
  • rightIcon

Variation "tab-transparent" displays tab panels with transparent backgound.

Example

<knob-tabs params="defaultTab: 1"  class="knob-tab-orient--left-top">
	<knob-tab params="label: 'tab1', icon: '#icon-grid'">
		content1
	</knob-tab>
	<knob-tab params="icon: '#icon-grid'">
		content2
	</knob-tab>
	<knob-tab params="label: 'tabX'">
		content3
	</knob-tab>
</knob-tabs>

knob-modal

With this component, you can easily create a modal window.

Params

ParamTypeRequiredDefault valueDescription
variationstringYes"modalHead"The variation of the button in modal header section.
titlestringNo"text"Header text in the header element.
iconstringNo"text"The icon on the left of the header element.
visibleko.observable (boolean)YesThis is the observable in which the show / hide the modal.

Example

<knob-modal params="
	title: 'test modal',
	icon: '#icon-open',
	visible: modalVisible">
	<p>
		Content text
	</p>
</knob-modal>
<script>
	ko.applyBindings({
		modalVisible: ko.observable(false)
	});
</script>

knob-modal - alert

With this component, you can easily create a modal - alert window.

Params

ParamTypeRequiredDefault valueDescription
titlestringNo""Header text in the header element.
iconstringNo""The id of the icon on the left of the header element.
messagestringYesThe message shown.
okLabelstringYesThe label on the button.
visibleko.observable (boolean)YesThis is the observable in which the show / hide the modal.
callbackfunctionYesThe callback function to be executed on pressing the button button.

Example

	<knob-alert params="
		title: 'test alert',
		icon: '#icon-warning',
		message: 'I have to alert you about something?',
		okLabel: 'Ok',
		visible: alertVisible,
		callback : confirmCallback">
	</knob-alert>

	<script>
		ko.applyBindings({
			alertVisible: ko.observable(false),
			confirmCallback: function(ok) {
				console.log(ok);
			}
		});
	</script>

knob-modal - confirm

With this component, you can easily create a confirm dialog.

Params

ParamTypeRequiredDefault valueDescription
titlestringNo""Header text in the header element.
iconstringNo""The icon on the left of the header element.
visibleko.observable (boolean)YesfalseThis is the observable in which the show / hide the modal.
messagestringYesContent message in the confirm modal window content section.
okLabelstringYesThe label of the ok button.
cancelLabelstringYesThe label of the cancel button.
callbackfunctionYesThis function will be called when the user clicks on the ok or cancel button. If the ok was clicked, then the param of it will be true, otherwise false.

Example

	<knob-confirm params="
		title: 'Confirm',
		icon: '#icon-open',
		message: 'Lorem ipsum dolor sit amet?',
		visible: confirmVisible,
		okLabel: 'Ok',
		cancelLabel: 'Cancel',
		callback: confirmCallback">
	</knob-confirm>

	<script>
		ko.applyBindings({
			confirmVisible: ko.observable(false),
			confirmCallback: function(ok) {
				console.log(ok);
			}
		});
	</script>

knob-notification

With this component, you can easily create a notification bar - info, success, warning and error statements.

Params

ParamTypeRequiredDefault valueDescription
variationstringNo""Header text in the header element.
messagestringNo""Header text in the header element.
iconstringNo""The icon on the left of the header element.
visibleko.observable (boolean)YesfalseThis is the observable in which the show / hide the modal.

Example

	<knob-notification params="
		message: 'important message',
		icon: '#icon-done',
		variation: 'success',
		visible: notificationVisible
	">
	</knob-notification>

	<script>
		ko.applyBindings({
			notificationVisible: ko.observable(false)
		});
	</script>

Styling knob components

You can use simply themes, and color sets. Defines the color, and use them in any module.

Example - define

knob.init({
	theme: "default",
	colorSet: {
		primary: "#2199e8",
		secondary: "#777",

		info: {
			text: "#00529b",
			background: "#bde5f8"
		},
		success: {
			text: "#4f8a10",
			background: "#dff2bf"
		},
		warning: {
			text: "#9f6000",
			background: "#feefb3"
		},
		error: {
			text: "#d8000c",
			background: "#ffbaba"
		},

		white: "#fff",

		lightGray: "#e6e6e6",
		mediumGray: "#cacaca",
		darkGray: "#8a8a8a",

		black: "#000",
		transparent: "transparent"
	}
});

knob-js uses the tinycolor2 npm package to the lighten and darken colors. Here is the example, hover state is lighten, active state is darken:

Example - during use

"default": {
	"default": {
		"backgroundColor": theme.secondary,
		"borderColor": theme.secondary,
		"color": theme.black,
		"fill": theme.black
	},
	"hover": {
		"backgroundColor": tinycolor(theme.success.background).lighten().toString(),
		"borderColor": tinycolor(theme.success.background).lighten().toString()
	},
	"active": {
		"backgroundColor": tinycolor(theme.success.background).darken().toString(),
		"borderColor": tinycolor(theme.success.background).darken().toString()
	},
	"disabled": {
		"backgroundColor": theme.mediumGray,
		"color": theme.lightGray,
		"fill": theme.lightGray
	}
}

Icons and localization

You can add two extra parameters when you init knob, with which you can customize the icons and the labels in the components. The first is the icons, which should be an object, the secont is the labels property, which also should be an object.

Params

ParamTypeRequiredDefault valueDescription
config.icons.searchstringNo"#icon-search"The search icon in the paged list component.
config.icons.sort.ascstringNo"#icon-sort-asc"The icon of the ascending sort.
config.icons.sort.descstringNo"#icon-sort-desc"The icon of the descending sort.
config.icons.dropdownstringNo"#icon-expand-more"The icon on the right hand side in dropdowns.
config.icons.loadingstringNo"#icon-loading"The loading icon. It is alwas spinned!
config.icons.pagination.firststringNo"#icon-first-page"The icon of the last page button in the pagination.
config.icons.pagination.prevstringNo"#icon-chervon-left"The icon of the prev button in the pagination.
config.icons.pagination.nextstringNo"#icon-chervon-right"The icon of the next button in the pagination.
config.icons.pagination.laststringNo"#icon-last-page"The icon of the last page button in the paginagion.
config.labels.noResultsstringNo"No results"The string which will be displayed if the query in a list returns with an empty set.

Example

knob.init({
	theme: "default",
	colorSet: {
		primary: "#2199e8",
		secondary: "#777",

		info: {
			text: "#00529b",
			background: "#bde5f8"
		},
		success: {
			text: "#4f8a10",
			background: "#dff2bf"
		},
		warning: {
			text: "#9f6000",
			background: "#feefb3"
		},
		error: {
			text: "#d8000c",
			background: "#ffbaba"
		},

		white: "#fff",

		lightGray: "#e6e6e6",
		mediumGray: "#cacaca",
		darkGray: "#8a8a8a",

		black: "#000",
		transparent: "transparent"
	},
	icons: {
		search: "#icon-search",
		sort: {
			asc: "#icon-sort-asc",
			desc: "#icon-sort-desc"
		},
		dropdown: "#icon-expand-more",
		loading: "#icon-loading",
		pagination: {
			first: "#icon-first-page",
			prev: "#icon-chevron-left",
			last: "#icon-chevron-right",
			next: "#icon-last-page"
		}
	},
	labels: {
		noResults: "No results"
	}
});

knob-checkbox

Checkbox component in knobjs. It has two state: checked (marked by a tick) or unchecked (marked by a cross). It's value is it's state (boolean). You can style it by giving icons object as parameter.

Params

ParamTypeRequiredDefault valueDescription
valueko.observableYesThis is the observable in which the value will be written. If you want to use the value of the checkbox, then you must give it as a parameter.
iconsobjectNo{ tick: icons.tick, cross: icons.cross }You can set custon icons belonging to checked and unchecked states

Example

<knob-checkbox params="
	value: checkboxValue
"></knob-checkbox>

<script>
	ko.applyBindings({
		checkboxValue: ko.observable(false)
	});
</script>

##knob-numericinput

A numeric stepper component. It's built up from two knob-buttons and a knob-input, the buttons are able to increase or decrease the input's value. Holding one of the buttons make the value change faster to a cap.

Params

ParamTypeRequiredDefault valueDescription
valueko.observableYesThe value of the input, it should store a number as an inital value.
minValuenumberYesThe minimum value the input can reach.
maxValuenumberYesThe maxmimum value the input can reach.
stepnumberYesThe amount the input can be decreased or increased with when clicking once.
prefixstringNoString before the input's value.
postfixstringNoString appended before the input's value.
minTimeoutnumberNo50The miminum of the refresh rate when holding one of the buttons.
timeoutDecrementnumberNo100Specifies how fast the value will refresh.
baseTimeoutnumberNo500Refresh rate in the beginning.
layoutArrangementstringNo*"back"Specifies the button placements.

*Only "back", "split" or "front" values are viable. No value means "back" by default. Otherwise an exception is thrown.

Example

<knob-numericinput params="
		minValue: -100,
		maxValue: 100,
		value: ko.observable(0),
		step: 1,
		prefix: 'Font size: ',
		postfix: 'px',
		layoutArrangement: 'split'
	">
</knob-numericinput>

Additional features

background color

You can set a background color for body of the HTML document by setting background property in theme parameter passed to knob's init function.

2.4.18

5 years ago

2.4.17

5 years ago

2.4.16

5 years ago

2.4.15

5 years ago

2.4.14

6 years ago

2.4.13

6 years ago

2.4.12

6 years ago

2.4.11

6 years ago

2.4.10

6 years ago

2.4.9

6 years ago

2.4.8

6 years ago

2.4.7

6 years ago

2.4.6

6 years ago

2.4.5

6 years ago

2.4.4

6 years ago

2.4.3

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.17

6 years ago

2.3.16

6 years ago

2.3.15

6 years ago

2.3.14

6 years ago

2.3.13

6 years ago

2.3.12

6 years ago

2.3.11

6 years ago

2.3.10

6 years ago

2.3.9

6 years ago

2.3.8

6 years ago

2.3.7

6 years ago

2.3.6

6 years ago

2.3.5

6 years ago

2.3.4

6 years ago

2.3.3

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.16

6 years ago

2.2.15

6 years ago

2.2.14

6 years ago

2.2.13

6 years ago

2.2.12

6 years ago

2.2.11

6 years ago

2.2.10

6 years ago

2.2.9

6 years ago

2.2.8

6 years ago

2.2.7

6 years ago

2.2.6

6 years ago

2.2.5

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.19

6 years ago

2.1.18

6 years ago

2.1.17

6 years ago

2.1.16

6 years ago

2.1.15

6 years ago

2.1.14

6 years ago

2.1.13

6 years ago

2.1.12

7 years ago

2.1.11

7 years ago

2.1.10

7 years ago

2.1.9

7 years ago

2.1.8

7 years ago

2.1.7

7 years ago

2.1.6

7 years ago

2.1.5

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

1.0.1

7 years ago

1.0.0-rc.11

7 years ago

1.0.0-rc.10

7 years ago

1.0.0-rc.9

7 years ago

1.0.0-rc.5

7 years ago

1.0.0-rc.3

7 years ago

1.0.0-rc.2

7 years ago

1.0.0-rc.1

7 years ago

1.0.0-rc.0

7 years ago

0.1.44

7 years ago

0.1.43

7 years ago

0.1.42

7 years ago

0.1.41

7 years ago

0.1.40

7 years ago

0.1.39

7 years ago

0.1.38

7 years ago

0.1.37

7 years ago

0.1.36

7 years ago

0.1.35

7 years ago

0.1.34

7 years ago

0.1.33

7 years ago

0.1.32

7 years ago

0.1.31

8 years ago

0.1.30

8 years ago

0.1.29

8 years ago

0.1.28

8 years ago

0.1.27

8 years ago

0.1.26

8 years ago

0.1.25

8 years ago

0.1.24

8 years ago

0.1.23

8 years ago

0.1.22

8 years ago

0.1.21

8 years ago

0.1.20

8 years ago

0.1.19

8 years ago

0.1.18

8 years ago

0.1.17

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.1

8 years ago