You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

327 lines
13 KiB

---
layout : 'default'
css : 'module'
title : 'UI Modules'
description : 'Modules are interface elements whose behavior is an essential part of their definition'
type : 'Semantic'
---
<%- @partial('header', { tabs: { overview: 'Overview', init: 'Initializing', behavior: 'Behaviors', settings: 'Settings'} }) %>
<div class="main container">
<div class="ui tab" data-tab="overview">
<p>All official javascript modules in Semantic are designed using a singular design pattern. This pattern allows several useful features common to all javascript components</p>
<div class="ui relaxed list">
<div class="item">
<i class="top aligned green check icon"></i>
<div class="content">
<div class="header">Run-time Performance Analysis</div>
<p>Semantic modules all provide the ability to log performance traces to the console, allowing you to see which aspects of the module are more or less performant and to track total init time <code>onDomReady</code></p>
</div>
</div>
<div class="item">
<i class="top aligned green check icon"></i>
<div class="content">
<div class="header">Human Readable Traces</div>
<p>Unlike other component libraries that hide explanations of behavior in inline comments which can only be read by combing the source, semantic modules provide run-time debug output to the JavaScript console telling you what each component is doing as it is doing it.</p>
</div>
</div>
<div class="item">
<i class="top aligned green check icon"></i>
<div class="content">
<div class="header">Settings can be overwritten after initialization</div>
<p>Semantic provides methods to set default settings, set settings at initialization, and set settings after initialization, allowing complete flexibility over component behaviors.</p>
</div>
</div>
<div class="item">
<i class="top aligned green check icon"></i>
<div class="content">
<div class="header">All modules include an initialize and destroy method</div>
<p>All events and metadata are namespaced and can be removed after initialization, modules automatically handle destroy/init events to allow users to lazy-initialize a plugin multiple times with no issues.</p>
</div>
</div>
<div class="item">
<i class="top aligned green check icon"></i>
<div class="content">
<div class="header">Instance available in metadata</div>
<p>Modules store their instance in metadata meaning that, in a pinch, you can directly modify the instance of a UI element by modifying its properties.</p>
</div>
</div>
</div>
<div class="ui divider"></div>
<a href="/spec/docs/modal.commented.html" class="ui large primary labeled icon button">
<i class="info icon"></i>
Real World Commented Example
</a>
<a href="/spec/docs/module.commented.html" class="ui large secondary labeled icon button">
<i class="info icon"></i>
Commented Design Pattern
</a>
</div>
<div class="ui tab" data-tab="init">
<h3 class="ui header">Overview</h3>
<p>Semantic does not automatically attach any events on page load. You must decide which modules to initialize on each page by initializing a module in javascript.</p>
<div class="ui info message">It's okay to initialize an element multiple times, the element will automatically destroy the previous instance and re-initialize with the settings provided.</div>
<h3 class="ui header">Example of Initializing</h3>
<p>The following example shows how to attach behavior to elements on a page using Semantic</p>
<div class="code" data-type="html" data-preview="true">
<div class="ui medium image">
<a class="ui corner label" data-content="This appears to the right">
<i class="help link icon"></i>
</a>
<img src="/images/demo/photo.jpg" data-content="This appears in the default location">
</div>
</div>
<div class="evaluated code" data-type="javascript">
// just initializing
$('.ui.image img')
.popup()
;
</div>
<div class="evaluated code" data-type="javascript">
// initializing with settings
$('.ui.image .corner.label')
.popup({
position: 'right center'
})
;
</div>
</div>
<div class="ui tab" data-tab="behavior">
<div class="fixed column">
<div class="ui sticky">
<h3 class="ui header">Demo Element</h3>
<i class="demo circular help link icon" data-content="Popup #1"></i>
<i class="demo circular help link icon" data-content="Popup #2"></i>
<div class="ui raised segment">
<div class="ui top attached label">Console</div>
<pre class="console"></pre>
</div>
</div>
</div>
<div class="examples">
<h2 class="ui dividing header">Using Module Behaviors</h2>
<p>Behaviors are an element's API. They invoke functionality or return aspects of the current state for an element</p>
<p>Behaviors can be called using spaced words, camelcase or dot notation. The preferred method however is <b>spaced words</b>. Method lookup is done automatically internally.</p>
<p>Behaviors can be called using the syntax:</p>
<div class="code">
// both are the same
$('.your.element')
.module('behavior name', argumentOne, argumentTwo)
.module('behaviorName', argumentOne, argumentTwo)
;
</div>
<h3 class="ui header">Common Behaviors</h3>
<p>All modules have a set of core behaviors which allow you to configure the component</p>
<table class="ui celled definition sortable table segment">
<thead>
<th>Name</th>
<th>Usage</th>
</thead>
<tbody>
<tr>
<td>initialize</td>
<td>Initializes an element, adding page event handlers and init data</td>
</tr>
<tr>
<td>destroy</td>
<td>Removes all changes to the page made by initialization</td>
</tr>
<tr>
<td>refresh</td>
<td>Refreshes cached values for a component</td>
</tr>
<tr>
<td>setting(setting, value)</td>
<td>Allows you to modify or read a component setting</td>
</tr>
<tr>
<td>invoke(query, passedArguments, instance)</td>
<td>Internal method used for translating sentence case method into an internal method</td>
</tr>
<tr>
<td>debug(comment, values)</td>
<td>Displays a log if a user has logging enabled</td>
</tr>
<tr>
<td>verbose(comment, values)</td>
<td>Displays a log if a user has verbose logging enabled</td>
</tr>
<tr>
<td>error(name)</td>
<td>Displays a name error message from the component's settings</td>
</tr>
<tr>
<td>performance log(comment, value)</td>
<td>Adds a performance trace for an element</td>
</tr>
<tr>
<td>performance display</td>
<td>Displays current element performance trace</td>
</tr>
</tbody>
</table>
<h2 class="ui dividing header">Examples</h2>
<h3 class="ui header">Overriding Internals</h3>
<p>Internal methods can be overwritten at run-time for individual instances of a module</p>
<div class="evaluated code">
// initialize both popups inline
$('.demo.icon')
.popup({
inline: true
})
;
//output the first popup's logs to the page
$('.demo.icon').first()
.popup('internal', 'debug', function() {
$('.console')
.append(arguments[0] + "\n")
// scroll to bottom
.prop('scrollTop', $('.console').prop('scrollHeight') )
;
})
;
</div>
<h3 class="ui header">Triggering Behavior</h3>
<p>Some behaviors can accept arguments, for example a popup <b>show</b> behavior can accept a callback function. This arbitrary example shows opening a popup then changing its position programatically</p>
<div class="code" data-demo="true">
// Sets a popup to top left position with an offset of negative five
$('.demo.icon').first()
.popup('setting', 'position', 'top right')
.popup('show', function() {
$(this)
.popup('set position', 'right center')
;
})
;
</div>
<h3 class="ui header">Returning values</h3>
<p>Behaviors may also provide an API for accessing a module's internal state. For example popups have a method <code>is visible</code> which returns true or false for whether a popup is currently visible.</p>
<div class="code" data-demo="true">
// returns boolean value instead of jQuery chain
$('.demo.icon')
.popup('debug', $('.demo.icon').first().popup('is visible') )
;
</div>
<div class="code" data-demo="true">
// if selector size > 1 returns array of values [boolean, boolean...]
$('.demo.icon')
.popup('debug', $('.demo.icon').popup('is visible') )
;
</div>
</div>
</div>
<div class="ui tab" data-tab="settings">
<p>Unlike many javascript components, anything arbitrary in Semantic is a setting. This means no need to dig inside the internals of the component to alter an expected css selector or class name, simply alter the settings object</p>
<h3 class="ui header">Common Settings</h3>
<p>The following is a list of common settings usually found in javascript modules.
<table class="ui celled sortable definition table segment">
<thead>
<th>Name</th>
<th>Usage</th>
</thead>
<tbody>
<tr>
<td>name</td>
<td>Name used in debug logs to differentiate this widget from other debug statements.</td>
</tr>
<tr>
<td>debug</td>
<td>Whether to provide standard debug output to console.</td>
</tr>
<tr>
<td>performance</td>
<td>Whether to provide performance logging to console of internal method calls.</td>
</tr>
<tr>
<td>verbose</td>
<td>Whether to provide extra debug output to console</td>
</tr>
<tr>
<td>namespace</td>
<td>Namespace used for DOM event and metadata namespacing. Allows module's destroy method to not affect other modules.</td>
</tr>
<tr>
<td>metadata</td>
<td>An object containing any metadata attributes used.</td>
</tr>
<tr>
<td>selectors</td>
<td>An object containing all selectors used in the module, these are usually children of the module.</td>
</tr>
<tr>
<td>classNames</td>
<td>An object containing all class names used in the module.</td>
</tr>
<tr>
<td>errors</td>
<td colspan="2">A javascript array of error statements used in the plugin. These may sometimes appear to the user, but most often appear in debug statements.
</td>
</tr>
</tbody>
</table>
<h3 class="ui header">Changing Settings</h3>
<p>The following examples use <a href="/modules/popup.html">popup</a> as an example for how to modify settings</p>
<div class="ui ordered very relaxed list">
<div class="item">
<div class="header">Setting module defaults</div>
Default settings for the module can be overridden by modifying <b>$.fn.module.settings</b>.
<br>
<div class="code">
$.fn.popup.settings.moduleName = 'Godzilla';
</div>
</div>
<div class="item">
<div class="header">At initialization</div>
A settings object can be passed in when initializing the plugin
<br>
<div class="code">
$('.foo')
.popup({
moduleName : 'Godzilla',
verbose : true
})
;
</div>
</div>
<div class="item">
<div class="header">After initialization</div>
Settings can be changed after a module is initialized by calling the 'settings' method on the module with either a settings object or a name, value pair.
<br>
<div class="code">
$('.foo')
// lets initialize that!
.popup()
// oh wait forgot something
.popup('setting', 'moduleName', 'Godzilla')
// and some more things
.popup('setting', {
debug : true,
verbose : true
})
;
</div>
</div>
</div>
<h3 class="ui header">Reading Settings</h3>
<p>Settings can also be read programmatically:
<div class="code">
// outputs 'godzilla' (1) or ['godzilla', 'godzilla'...] (2 or more)
$('.foo').popup('setting', 'moduleName');
</div>
</div>
</div>