Git Product home page Git Product logo

jquery-harmonia's Introduction

jQuery Harmonia

Harmonia gif demo

About

Replace an (un)ordered list with a form select.

Inspired by:

Demonstration

Click or scan:

qr code

Resize the browser window to see the plugin(s) in action (Firefox 15+ users, check out Responsive Design View).

Source: jquery.harmonia.js | jquery.harmonia.min.js

Installation

There are several ways to install this code:

  1. Download as a zip.
  2. Clone it: $ git clone https://github.com/mhulse/jquery-harmonia.git.
  3. Fork it and clone: $ git clone [email protected]:USERNAME/jquery-harmonia.git.
  4. Just grab the relevant JS (uglified) files.
  5. Using Bower: $ bower install https://github.com/mhulse/jquery-harmonia.git.

Usage

Setting up Harmonia is simple …

Markup:

<ul class="myList">
	<li><a href="http://en.wikipedia.org/wiki/Hebe_(mythology)">Hebe</a></li>
	<li><a href="http://en.wikipedia.org/wiki/Hebe_(mythology)">Hedone</a></li>
	<li><a href="http://en.wikipedia.org/wiki/Nike_(mythology)">Nike</a></li>
	<li><a href="#">No link</a></li>
	<li><a href="http://en.wikipedia.org/wiki/Angelos_(Greek_mythology)">Angelos</a></li>
	<li><a href="http://en.wikipedia.org/wiki/Erinyes">Erinyes</a></li>
	<li><a class="selected" href="http://en.wikipedia.org/wiki/Cronus">Cronus</a></li>
	<li><a href="http://en.wikipedia.org/wiki/Ceto">Ceto</a></li>
</ul>

Styling:

There are no CSS requirements.

Optionally, depending on your needs, CSS media queries can be used to show/hide the HTML list/form select(s) based on viewport size.

Javascript:

Put jQuery on your page:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

… and link to the plugin:

<script src="jquery.harmonia.min.js"></script>

Finally, instantiate the plugin:

<script>
	$(document).ready(function() {
		$('.myList').harmonia();
	});
</script>

Here’s an example with all the options:

$('.myList').harmonia({
	currentPage   : false,
	optionDefault : 'Choose ...',
	openTab       : false,
	classSelect   : 'harmonia-select',
	classInit     : 'harmonia-js-enabled',
	use           : 'before',
	idSelect      : '#foo',
	elementTarget : '#target',
	onInit        : function(data) { console.log('onInit:', $(this), data); },
	onAfterInit   : function(data) { console.log('onAfterInit:', $(this), data); },
	onAddOption   : function(data, return) { console.log('onAddOption:', $(this), data, return); },
	onChange      : function(data, el) { console.log('onChange:', $(this), el); }
});

… where:

Option Description Default
currentPage Select the current page? false
optionDefault Default option for <select>. 'Choose ...'
openTab Open link in new tab? Default is current window. false
selectClass Class name for <select>; class applied to generated <select> element(s). 'harmonia-select'
classInit Target menu; class name applied to instantiated element(s). 'harmonia-js-enabled'
use Replacement function to use when adding <select> to the DOM. Allowed values are after, append, before, html, and prepend. 'before'
idSelect ID name for <select>. ''
elementTarget Desired location to put the <select>; defaults to before (see use option) the current instantiated element. ''
onInit Callback after plugin data initialized. $.noop
onAfterInit Callback after plugin initialization. $.noop
onAddOption Callback when a new option has been added. $.noop
onChange Callback when <select> changes. $.noop

Advanced:

  1. All options can be overidden via an HTML5 data attribute named data-harmonia-options; the value of this attribute must be valid JSON syntax.

For example:

<ul class="myList toDestroy" data-harmonia-options='{ "classSelect" : "omgWtf", "idSelect" : "yolo", "elementTarget" : "#target", "use" : "after" }'> ... </ul>

Note the nesting order of the single (') and double (") quotes.

  1. All options may be defined before plugin initialzation, like: $.fn.harmonia.defaults.XXX = 'foo'; (where XXX is name of option).

  2. Move the generated <select> anywhere on the page using the elementTarget option. For example: data-harmonia-options='{ "elementTarget" : "#foo" }', where elementTarget is: <div id="foo"></div> somewhere on page.

  3. Nested lists may be used to create <optgroup>s.

For example:

<ul class="myList">
	<li><a href="https://www.google.com">Top</a></li>
	<li>
		<a href="#">Subs</a>
		<ul>
			<li><a href="https://www.google.com">Sub 1</a></li>
			<li><a href="https://www.google.com">Sub 2</a></li>
		</ul>
	</li>
</ul>

… which gets converted to:

<select class="myList">
	<option>Choose ...</option>
	<option value="https://www.google.com">Top</option>
	<optgroup label="Subs">
		<option value="https://www.google.com">Sub 1</option>
		<option value="https://www.google.com">Sub 2</option>
	</optgroup>
</select>
  1. A <li> item’s <a>, with class="selected", will apply a selected="selected" attribute to the generated <option>. Note that a selected class will trump the currentPage option.

Check out the demo page for working examples of the aforementioned features.

Contributing

Please read the CONTRIBUTING.md.

Feedback

Bugs? Constructive feedback? Questions?

Changelog

  • 2014-07-20   v1.1.1   Versatile.
  • 2014-06-26   v1.1.0   Über!
  • 2014-04-14   v1.0.0   Stable.
  • 2012-08-05   v0.1.0

LEGAL

Copyright © 2013-2014 Micky Hulse

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

:octocat:

jquery-harmonia's People

Contributors

mhulse avatar pyvirus avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jquery-harmonia's Issues

Allow each select to have unique ID or class

So each can be styled individually if so desired.

Might want to investigate how useful the selectId option is, as only one ID can be on a page. Maybe the data- attribute feature #21 cold be a fix for this issue too?

Strip HTML from inside HREF

If there's HTML inside list item's href then Harmonia won't generate an option. Just need to strip all HTML to get at the text node(s).

Tag 1.0.0!

As soon as things are stable, let's tag it!

Bower

Make this repo a Bower-installable package.

Document optgroup links

It's not possible to make optgroup headings linkable, so you can do:

<li>
    <a href="/report/">Report</a>
    <ul>
        <li><a href="/report/" class="avr_util-hide">Report Home</a></li>
        <li><a href="/report/something-wrong/">Something Wrong</a></li>
        <li><a href="/report/something-right/">Something Right</a></li>
        <li><a href="/report/status-of-reports/">Status of Reports</a></li>
    </ul>
</li>

Where .avr_util-hide is just display:none.

Selected

If currentPage : true in the options, and when passing this first default option:

$default = _optionize.call(data.target, $('<a>'), data.settings.optionDefault);

… it hits this code:

link     = ($a.attr('href') || ''); // Current `<a>`'s href.

… and then passes checks later so that it becomes the "selected" option.

Need to make sure that links without href attributes don't pass selected checks.

Handle non-links better

With this:

        <ul class="bits bits-right harmonia" data-harmonia-options='{
            "elementTarget": "#top-select"
        }'>
            <li><span>No Link</span></li>
            <li><a href="http://google.com">Link</a></li>
            <li><a href="http://google.com">Link</a></li>
            <li><a href="http://google.com">Link</a></li>
            <li class="fl"><a href="http://google.com">Link</a></li>
        </ul>

I get:

screenshot 2017-05-14 00 14 37

Links that are "/" or "" or ?

Kinda a patch:

//----------------------------------
// Compare urls directly or index:
//----------------------------------

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
if ((href == '/') || (href == ahref) || ((href.indexOf(ahref) != -1) && (ahref != '/'))) {

    selected = true;

}

Not fully tested. Was working with this:

<ul class="avr_links avr_harmonia">
    <li><a href="/">Home</a></li>
    ...

The slash was hitting the indexOf and matching as selected.

Is the above patch good enough? Need to test more ... Might be nice to have a multi-page demo to account for this scenario.

Fix comment...

 * @docs      http://github.com/username/jquery-harmonia

Replace username with registerguard.

Handle console better

var console = this.console || {log:function(){}};

Might be a better way to handle console/debug output. Allows me to ditch the check for C and just use console.log();

Make optgroups

Like:

<select name="fm_select">
    <optgroup label="Group 1">
        <option value="Option 1.1">Option 1.1</option>
    </optgroup> 
    <optgroup label="Group 2">
        <option value="Option 2.1">Option 2.1</option>
        <option value="Option 2.2">Option 2.2</option>
    </optgroup>
    <optgroup label="Group 3" disabled>
        <option value="Option 3.1">Option 3.1</option>
        <option value="Option 3.2">Option 3.2</option>
        <option value="Option 3.3">Option 3.3</option>
    </optgroup>
</select>

screen shot 2014-06-25 at 10 50 41 pm

Reduce branches down to gh-pages

The current workflow is to work on develop, merge into master and merger master to gh-pages.

Skip all of the above and make this repo a gh-pages-only workflow.

Demo page

Make font size 16px so ios doesn't zoom.

Also, center and make a little more presentable.

Fix

Move the generated anywhere on the page using the elementTarget option. For example: data-harmonia-options='{ "elementTarget" : "#target" }', where elementTarget is: <div id="foo"></div>. Make foo target and say "... somewhere on page".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.