Skip to content

Commit

Permalink
MINOR Using jquery.concrete and jquery.selector on github through pis…
Browse files Browse the repository at this point in the history
…ton dependency management

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@92542 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
chillu committed Nov 21, 2009
1 parent d95f867 commit 4b12937
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 61 deletions.
12 changes: 6 additions & 6 deletions forms/CalendarDateField.php
Expand Up @@ -29,12 +29,12 @@ function Field() {


// javascript: concrete
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.class.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.specifity.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.matches.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.dat.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.concrete.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.class.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.specifity.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.matches.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/src/jquery.dat.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/src/jquery.concrete.js');

// javascript: custom
Requirements::javascript(SAPPHIRE_DIR . '/javascript/CalendarDateField.js');
Expand Down
12 changes: 6 additions & 6 deletions forms/TabSet.php
Expand Up @@ -60,12 +60,12 @@ public function FieldHolder() {
Requirements::css(SAPPHIRE_DIR . '/thirdparty/jquery-ui-themes/smoothness/ui.tabs.css');

// concrete
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.class.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.specifity.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.matches.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.dat.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.concrete.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.class.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.specifity.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.matches.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/src/jquery.dat.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/src/jquery.concrete.js');

Requirements::javascript(SAPPHIRE_DIR . '/javascript/TabSet.js');

Expand Down
12 changes: 6 additions & 6 deletions security/Security.php
Expand Up @@ -344,12 +344,12 @@ public function login() {
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-ui/ui.tabs.js');

// concrete
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.class.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.specifity.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.selector.matches.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.dat.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/jquery.concrete.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.class.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.specifity.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-selector/src/jquery.selector.matches.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/src/jquery.dat.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery-concrete/src/jquery.concrete.js');

Requirements::css(THIRDPARTY_DIR . '/jquery/themes/smoothness/ui.all.css');
Requirements::css(THIRDPARTY_DIR . '/jquery/themes/smoothness/ui.tabs.css');
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions thirdparty/jquery-concrete/.gitignore
@@ -0,0 +1,2 @@
vendor/
spec/
8 changes: 8 additions & 0 deletions thirdparty/jquery-concrete/.piston.yml
@@ -0,0 +1,8 @@
---
format: 1
handler:
commit: c857ed5d684031115a2bb369f601c7147a2c69bc
branch: master
lock: false
repository_url: git://github.com/hafriedlander/jquery.concrete.git
repository_class: Piston::Git::Repository
10 changes: 10 additions & 0 deletions thirdparty/jquery-concrete/LICENSE
@@ -0,0 +1,10 @@
Copyright (C) 2009 Hamish Friedlander (hamish@silverstripe.com) and SilverStripe Limited (www.silverstripe.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Hamish Friedlander nor SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267 changes: 267 additions & 0 deletions thirdparty/jquery-concrete/README.textile
@@ -0,0 +1,267 @@
h1. Concrete - Support for ConcreteUI programming in jQuery

A basic desire for jQuery programming is some sort of OO or other organisational method for code. For your consideration,
we provide a library for ConcreteUI style programming. In ConcreteUI you attach behavioral code to DOM objects. Concrete extends this
concept beyond what is provided by other libraries to provide a very easy to use system with class like, ploymorphic, namespaced properties

h2. Basic use

h4. First intro

To attach methods to DOM nodes, call the `concrete` function on a jQuery selector object, passing a hash listing the method names and bodys

<pre><code>
$('div').concrete({
foo: function(..){..},
bar: function(..){..}
});
</code></pre>

You can then call those methods on any jQuery object.

<pre><code>
$('#a').foo();
</code></pre>

Any elements in the jQuery selection that match the selector used during definition ('div' in this example) will have foo called with that element
set as this. Any other objects are skipped. The return value will be the return value of foo() for the last matched DOM object in the set

h4. A proper example

Given this DOM structure:

<pre><code>
<body>
<div class="internal_text">Internal text</div>
<div class="attribute_text" rel="Attribute text"></div>
<div>Nonsense</div>
</body>
</code></pre>

And this concrete definition

<pre><code>
$('.internal_text').concrete({
foo: function(){ console.log(this.text()); }
});
$('.attribute_text').concrete({
foo: function(){ console.log(this.attr('rel')); }
});
</code></pre>

Then this call

<pre><code>
$('div').foo();
</code></pre>

Will log this to the console

<pre><code>
Internal text
Attribute text
</code></pre>

h4. Limitations

When defining methods, the jQuery object that concrete is called on must be a plain selector, without context. These examples will not work

<pre><code>
$('div', el).concrete(...)
$([ela, elb, elc]).concrete(...)
$('<div id="a"></div>').concrete(...)
</code></pre>

h2. Live

The definitions you provide are not bound to the elements that match at definition time. You can declare behaviour prior to the DOM existing in any
form (i.e. prior to DOMReady) and later calls will function correctly.

h2. Selector specifity

When there are two definitions for a particular method on a particular DOM node, the function with the most _specific_ selector is used.
_Specifity_ is calculated as defined by the CSS 2/3 spec. This can be seen as _subclassing_ applied to behaviour.

Another example. Given this DOM structure

<pre><code>
<body>
<div>Internal text</div>
<div class="attribute_text" rel="Attribute text"></div>
<div>Nonsense</div>
</body>
</code></pre>

And this concrete definition

<pre><code>
$('div').concrete({
foo: function(){ console.log(this.text()); }
});
$('.attribute_text').concrete({
foo: function(){ console.log(this.attr('rel')); }
});
</code></pre>

Then this call

<pre><code>
$('div').foo();
</code></pre>

Will log this to the console

<pre><code>
Internal text
Attribute text
Nonsense
</code></pre>

h2. Events

If you declare a function with a name starting with 'on', then instead of defining that function, it will be bound to an event of that
name. Just like other functions this binding will be live, and only the most specific definition will be used

<pre><code>
<head>
<script type='text/javascript'>
/* No need for onready wrapper. Events are bound as needed */
$('div').concrete({
onclick: function(){ this.css({backgroundColor: 'blue'}); }
});
$('.green').concrete({
onclick: function(){ this.css({color: 'green'}); }
});
</script>
<body>
<div>Background will turn blue when clicked on</div>
<div>Will also have blue background when clicked on</div>
<div class='green'>Will have green text when clicked on. Background color will not change</div>
</body>
</code></pre>

h2. Constructors / Destructors

Declaring a function with the name `onmatch` will create a behavior that is called on each object when it matches. Likewise, `onunmatch` will
be called when an object that did match this selector stops matching it (because it is removed, or because you've changed its properties).

Note that an onunmatch block must be paired with an onmatch block - an onunmatch without an onmatch _in the same concrete definition block_ is illegal

Like other functions, only the most specific definition will be used. However, because property changes are not atomic, this may not work as you
expect.

h2. Namespaces

To avoid name clashes, to allow multiple bindings to the same event, and to generally seperate a set of functions from other code you can use namespaces

<pre><code>
$('div').concrete('foo.bar', function($){ return {
baz: function(){}
}});
</code></pre>

You can then call these functions like this:

<pre><code>
$('div').concrete('foo.bar').baz()
</code></pre>

Namespaced functions work just like regular functions (`this` is still set to a matching DOM Node). However, specifity is calculated per namespace.
This is particularly useful for events, because given this:

<pre><code>
$('div').concrete({
onclick: function(){ this.css({backgroundColor: 'blue'}); }
});

$('div').concrete('foo', function($){ return {
onclick: function(){ this.css({color: 'green'}); }
}});
</code></pre>

Clicking on a div will change the background **and** foreground color.

This is particularly important when writing reusable code, since otherwise you can't know before hand whether your event handler will be called or not

Although a namespace can be any string, best practise is to name them with dotted-identifier notation.

h4. Namespaces and scope (or What the hell's up with that ugly function closure)

Inside a namespace definition, functions remember the namespace they are in, and calls to other functions will be looked up inside that namespace first.
Where they don't exist, they will be looked up in the base namespace

<pre><code>
$('div').concrete('foo', {
bar: function() { this.baz(); this.qux(); }
baz: function() { console.log('baz'); }
})

$('div').concrete({
qux: function() { console.log('qux'); }
})
</code></pre>

Will print baz, qux to the console

Note that 'exists' means that a function is declared in this namespace for _any_ selector, not just a matching one. Given the dom

<pre><code>
<div>Internal text</div>
</code></pre>

And the concrete definitions

<pre><code>
$('div').concrete('foo', {
bar: function() { this.baz(); }
})

$('span').concrete('foo' {
baz: function() { console.log('a'); }

$('div').concrete({
baz: function() { console.log('b'); }
})
</code></pre>

Then doing $('div').bar(); will _not_ display b. Even though the span rule could never match a div, because baz is defined for some rule in the foo namespace, the base namespace will never be checked

h4. Forcing base

In some situations (such as the last example) you may want to force using the base namespace. In this case you can call concrete without any arguments. For example, if the first definition in the previous example was

<pre><code>
$('div').concrete('foo', {
bar: function() { this.concrete().baz(); }
})
</code></pre>

Then b _would_ be output to the console. It is up to the developer to decide when they need to be explicit about using the base namespace

h4. Using

Sometimes a block outside of a namespace will need to refer to that namespace repeatedly. By passing a function to the concrete function, you can change the looked-up namespace

<pre><code>
$('div').concrete('foo', {
bar: function() { console.log('a'); }
})

$('div').concrete('foo', function(){
this.bar();
});
</code></pre>

This equivilent to /with/ in javascript, and just like /with/, care should be taken to only use this construct in situations that merit it.












0 comments on commit 4b12937

Please sign in to comment.