Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
Now with custom trim
Browse files Browse the repository at this point in the history
  • Loading branch information
soney committed Jan 15, 2014
1 parent 7478676 commit 94ee94d
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 22 deletions.
6 changes: 3 additions & 3 deletions api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
// moved: [ { item: 'c', from: 2, insert_at: 0, move_from: 1, to: 0 } ],
// index_changed: [ { from: 2, from_item: 'c', item: 'c', to: 0, to_item: 'c' } ]
// }
</code></pre></div></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/util.js#L489"> Source: src/util.js:489</a><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/core.js#L1305"> (Exposed: src/core.js:1305)</a></div></div></div></div><div id="cjs_bindAttr" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.bindAttr(...)</div><div class="panel-body"><div class="desc"><p>Constrain a DOM node&#39;s attribute values</p>
</code></pre></div></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/util.js#L494"> Source: src/util.js:494</a><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/core.js#L1305"> (Exposed: src/core.js:1305)</a></div></div></div></div><div id="cjs_bindAttr" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.bindAttr(...)</div><div class="panel-body"><div class="desc"><p>Constrain a DOM node&#39;s attribute values</p>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">.bindAttr(element, values)</td><tr class="param"><td class="name">element</td><td class="type">dom</td><td class="description">The DOM element</td></tr><tr class="param"><td class="name">values</td><td class="type">object</td><td class="description">An object whose key-value pairs are the attribute names and values respectively</td></tr></tr><tr class="returns"><td class="static">Returns</td><td class="type">Binding</td><td class="description">A binding object representing the link from constraints to elements</td></tr><tr class="callname"><td colspan="3" class="calldesc">.bindAttr(key, value)</td><tr class="param"><td class="name">key</td><td class="type">string</td><td class="description">The name of the attribute to constraint</td></tr><tr class="param"><td class="name">value</td><td class="type">cjs.Constraint,string</td><td class="description">The value of this attribute</td></tr></tr><tr class="returns"><td class="static">Returns</td><td class="type">Binding</td><td class="description">A binding object representing the link from constraints to elements</td></tr></tbody></table></div><div class="panel-body examples"><h4>Example:</h4><div class="example"><p> If <code>my_elem</code> is an input element</p>
<pre><code>var default_txt = cjs(&#39;enter name&#39;);
cjs.bindAttr(my_elem, &#39;placeholder&#39;, default_txt);
Expand Down Expand Up @@ -420,7 +420,7 @@ <h2 id="partials">Partials</h2>
<pre><code>cjs.on(&quot;click&quot;, elem1, elem2)
</code></pre></div><div class="example"><p> After 3 seconds</p>
<pre><code>cjs.on(&quot;timeout&quot;, 3000)
</code></pre></div></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L105"> Source: src/state_machine/cjs_events.js:105</a></div></div></div></div><div id="cjs_pauseTemplate" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.pauseTemplate(node)</div><div class="panel-body"><div class="desc"><p>Pause dynamic updates to a template</p>
</code></pre></div></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L104"> Source: src/state_machine/cjs_events.js:104</a></div></div></div></div><div id="cjs_pauseTemplate" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.pauseTemplate(node)</div><div class="panel-body"><div class="desc"><p>Pause dynamic updates to a template</p>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">.pauseTemplate(node)</td><tr class="param"><td class="name">node</td><td class="type">dom</td><td class="description">The dom node created by <code>createTemplate</code></td></tr></tr><tr class="returns"><td class="static">Returns</td><td class="type">boolean</td><td class="description">Whether the template was successfully paused</td></tr></tbody></table></div><div class="panel-body related"><h4>Related:</h4><ul class="related_links"><li class="also_see"><a href="#cjs_resumeTemplate">cjs.resumeTemplate</a></li><li class="also_see"><a href="#cjs_createTemplate">cjs.createTemplate</a></li><li class="also_see"><a href="#cjs_destroyTemplate">cjs.destroyTemplate</a></li></ul></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/template/cjs_template.js#L829"> Source: src/template/cjs_template.js:829</a></div></div></div></div><div id="cjs_registerPartial" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.registerPartial(name, value)</div><div class="panel-body"><div class="desc"><p>Register a partial that can be used in other templates</p>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">.registerPartial(name, value)</td><tr class="param"><td class="name">name</td><td class="type">string</td><td class="description">The name that this partial can be referred to as</td></tr><tr class="param"><td class="name">value</td><td class="type">Template</td><td class="description">The template</td></tr></tr><tr class="returns"><td class="static">Returns</td><td class="type">cjs</td><td class="description"><code>cjs</code></td></tr></tbody></table></div><div class="panel-body related"><h4>Related:</h4><ul class="related_links"><li class="also_see"><a href="#cjs_unregisterPartial">cjs.unregisterPartial</a></li></ul></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/template/cjs_template.js#L787"> Source: src/template/cjs_template.js:787</a></div></div></div></div><div id="cjs_removeDependency" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.removeDependency(...)</div><div class="panel-body"><div class="desc"><p>Remove the edge going from <code>fromNode</code> to <code>toNode</code></p>
</div></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/core.js#L258"> Source: src/core.js:258</a><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/core.js#L1302"> (Exposed: src/core.js:1302)</a></div></div></div></div><div id="cjs_resumeTemplate" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.resumeTemplate(node)</div><div class="panel-body"><div class="desc"><p>Resume dynamic updates to a template</p>
Expand Down Expand Up @@ -575,7 +575,7 @@ <h2 id="partials">Partials</h2>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">.resume()</td></tr><tr class="returns"><td class="static">Returns</td><td class="type">Binding</td><td class="description"><code>this</code></td></tr></tbody></table></div><div class="panel-body related"><h4>Related:</h4><ul class="related_links"><li class="also_see"><a href="#cjs_Binding.prototype.pause">cjs.Binding.prototype.pause</a></li><li class="also_see"><a href="#cjs_Binding.prototype.throttle">cjs.Binding.prototype.throttle</a></li></ul></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/binding.js#L156"> Source: src/binding.js:156</a></div></div></div></div><div id="cjs_Binding_prototype_throttle" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.Binding.prototype.throttle(min_delay)</div><div class="panel-body"><div class="desc"><p>Require at least <code>min_delay</code> milliseconds between setting the attribute</p>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">.throttle(min_delay)</td><tr class="param"><td class="name">min_delay</td><td class="type">number</td><td class="description">The minimum number of milliseconds between updates</td></tr></tr><tr class="returns"><td class="static">Returns</td><td class="type">Binding</td><td class="description"><code>this</code></td></tr></tbody></table></div><div class="panel-body related"><h4>Related:</h4><ul class="related_links"><li class="also_see"><a href="#cjs_Binding.prototype.pause">cjs.Binding.prototype.pause</a></li><li class="also_see"><a href="#cjs_Binding.prototype.resume">cjs.Binding.prototype.resume</a></li></ul></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/binding.js#L167"> Source: src/binding.js:167</a></div></div></div></div><div id="cjs_CJSEvent" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">new cjs.CJSEvent(...)</div><div class="panel-body"><div class="desc"><p><strong>Note:</strong> the preferred way to create this object is with the <code>cjs.on</code> function
Creates an event that can be used in a finite-state machine transition</p>
</div></div><div class="panel-body related"><h4>Related:</h4><ul class="related_links"><li class="also_see"><a href="#cjs_on">cjs.on</a></li></ul></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L1"> Source: src/state_machine/cjs_events.js:1</a><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L103"> (Exposed: src/state_machine/cjs_events.js:103)</a></div></div></div></div><div id="cjs_CJSEvent_prototype__addTransition" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.CJSEvent.prototype._addTransition(transition)</div><div class="panel-body"><div class="desc"><p>Add a transition to my list of transitions that this event is attached to</p>
</div></div><div class="panel-body related"><h4>Related:</h4><ul class="related_links"><li class="also_see"><a href="#cjs_on">cjs.on</a></li></ul></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L1"> Source: src/state_machine/cjs_events.js:1</a><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L102"> (Exposed: src/state_machine/cjs_events.js:102)</a></div></div></div></div><div id="cjs_CJSEvent_prototype__addTransition" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.CJSEvent.prototype._addTransition(transition)</div><div class="panel-body"><div class="desc"><p>Add a transition to my list of transitions that this event is attached to</p>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">._addTransition(transition)</td><tr class="param"><td class="name">transition</td><td class="type">Transition</td><td class="description">The transition this event is attached to</td></tr></tr></tbody></table></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L39"> Source: src/state_machine/cjs_events.js:39</a></div></div></div></div><div id="cjs_CJSEvent_prototype__fire" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.CJSEvent.prototype._fire(...events)</div><div class="panel-body"><div class="desc"><p>When I fire, go through every transition I&#39;m attached to and fire it then let any interested listeners know as well</p>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">._fire(...events)</td><tr class="param"><td class="name">...events</td><td class="type">*</td><td class="description">Any number of events that will be passed to the transition</td></tr></tr></tbody></table></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L73"> Source: src/state_machine/cjs_events.js:73</a></div></div></div></div><div id="cjs_CJSEvent_prototype__removeTransition" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.CJSEvent.prototype._removeTransition(transition)</div><div class="panel-body"><div class="desc"><p>Remove a transition from my list of transitions</p>
</div><table class="params"><tbody><tr class="callname"><td colspan="3" class="calldesc">._removeTransition(transition)</td><tr class="param"><td class="name">transition</td><td class="type">Transition</td><td class="description">The transition this event is attached to</td></tr></tr></tbody></table></div><div class="panel-footer"><a target="_blank" href="https://github.com/soney/ConstraintJS/blob/v0.9.3-beta/src/state_machine/cjs_events.js#L53"> Source: src/state_machine/cjs_events.js:53</a></div></div></div></div><div id="cjs_CJSEvent_prototype_guard" class="row"><div class="col-md-12 entry"><div class="panel panel-default"><div class="panel-heading classname">cjs.CJSEvent.prototype.guard([filter])</div><div class="panel-body"><div class="desc"><p>Create a transition that calls filter whenever it fires to ensure that it should fire</p>
Expand Down
20 changes: 12 additions & 8 deletions build/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var cjs = (function (root) {

// Many of the functions here are from http://underscorejs.org/
// Save bytes in the minified (but not gzipped) version:
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
var ArrayProto = Array.prototype, ObjProto = Object.prototype,
FuncProto = Function.prototype, StringProto = String.prototype;

// Create quick reference variables for speed access to core prototypes.
var slice = ArrayProto.slice,
Expand All @@ -31,10 +32,14 @@ var nativeSome = ArrayProto.some,
nativeKeys = Object.keys,
nativeFilter = ArrayProto.filter,
nativeReduce = ArrayProto.reduce,
nativeMap = ArrayProto.map;
nativeMap = ArrayProto.map,
nativeTrim = StringProto.trim;

//Bind a function to a context
var bind = function (func, context) { return function () { return func.apply(context, arguments); }; },
trim = function(str){
return nativeTrim ? nativeTrim.call(str) : String(str).replace(new RegExp(/^\s+|\s+$/, 'g'), '');
},
doc = root.document,
sTO = bind(root.setTimeout, root),
cTO = bind(root.clearTimeout, root),
Expand Down Expand Up @@ -4290,7 +4295,7 @@ var text_binding = create_textual_binding(function(element, value) { // set the
// and add all of the added classes
curr_class_name += map(ad.added, function(x) { return x.item; }).join(" ");

curr_class_name = curr_class_name.trim(); // and trim to remove extra spaces
curr_class_name = trim(curr_class_name); // and trim to remove extra spaces

element.className = curr_class_name; // finally, do the work of setting the class
}, []), // say that we don't have any classes to start with
Expand Down Expand Up @@ -4587,7 +4592,7 @@ var parse_single_state_spec = function(str) {
// Parse one side of the transition
var parse_state_spec = function(str) {
// Split by , and remove any excess spacing
var state_spec_strs = map(str.split(","), function(ss) { return ss.trim(); });
var state_spec_strs = map(str.split(","), function(ss) { return trim(ss); });

// The user only specified one state
if(state_spec_strs.length === 1) {
Expand Down Expand Up @@ -5129,8 +5134,7 @@ var CJSEvent = function(parent, filter, onAddTransition, onRemoveTransition) {
/** @lends */

var isElementOrWindow = function(elem) { return elem === root || isElement(elem); },
do_trim = function(x) { return x.trim(); },
split_and_trim = function(x) { return map(x.split(" "), do_trim); },
split_and_trim = function(x) { return map(x.split(" "), trim); },
timeout_event_type = "timeout";

extend(cjs, {
Expand Down Expand Up @@ -6306,9 +6310,9 @@ extend(cjs, {
createTemplate: function(template_str) {
if(!isString(template_str)) {
if(is_jquery_obj(template_str) || isNList(template_str)) {
template_str = template_str.length > 0 ? template_str[0].textContent.trim() : "";
template_str = template_str.length > 0 ? trim(template_str[0].textContent) : "";
} else if(isElement(template_str)) {
template_str = template_str.textContent.trim();
template_str = trim(template_str.textContent);
} else {
template_str = "" + template_str;
}
Expand Down
4 changes: 2 additions & 2 deletions build/cjs.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/cjs.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ var text_binding = create_textual_binding(function(element, value) { // set the
// and add all of the added classes
curr_class_name += map(ad.added, function(x) { return x.item; }).join(" ");

curr_class_name = curr_class_name.trim(); // and trim to remove extra spaces
curr_class_name = trim(curr_class_name); // and trim to remove extra spaces

element.className = curr_class_name; // finally, do the work of setting the class
}, []), // say that we don't have any classes to start with
Expand Down
3 changes: 1 addition & 2 deletions src/state_machine/cjs_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ var CJSEvent = function(parent, filter, onAddTransition, onRemoveTransition) {
/** @lends */

var isElementOrWindow = function(elem) { return elem === root || isElement(elem); },
do_trim = function(x) { return x.trim(); },
split_and_trim = function(x) { return map(x.split(" "), do_trim); },
split_and_trim = function(x) { return map(x.split(" "), trim); },
timeout_event_type = "timeout";

extend(cjs, {
Expand Down
2 changes: 1 addition & 1 deletion src/state_machine/cjs_fsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ var parse_single_state_spec = function(str) {
// Parse one side of the transition
var parse_state_spec = function(str) {
// Split by , and remove any excess spacing
var state_spec_strs = map(str.split(","), function(ss) { return ss.trim(); });
var state_spec_strs = map(str.split(","), function(ss) { return trim(ss); });

// The user only specified one state
if(state_spec_strs.length === 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/template/cjs_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,9 @@ extend(cjs, {
createTemplate: function(template_str) {
if(!isString(template_str)) {
if(is_jquery_obj(template_str) || isNList(template_str)) {
template_str = template_str.length > 0 ? template_str[0].textContent.trim() : "";
template_str = template_str.length > 0 ? trim(template_str[0].textContent) : "";
} else if(isElement(template_str)) {
template_str = template_str.textContent.trim();
template_str = trim(template_str.textContent);
} else {
template_str = "" + template_str;
}
Expand Down
9 changes: 7 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

// Many of the functions here are from http://underscorejs.org/
// Save bytes in the minified (but not gzipped) version:
var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype;
var ArrayProto = Array.prototype, ObjProto = Object.prototype,
FuncProto = Function.prototype, StringProto = String.prototype;

// Create quick reference variables for speed access to core prototypes.
var slice = ArrayProto.slice,
Expand All @@ -22,10 +23,14 @@ var nativeSome = ArrayProto.some,
nativeKeys = Object.keys,
nativeFilter = ArrayProto.filter,
nativeReduce = ArrayProto.reduce,
nativeMap = ArrayProto.map;
nativeMap = ArrayProto.map,
nativeTrim = StringProto.trim;

//Bind a function to a context
var bind = function (func, context) { return function () { return func.apply(context, arguments); }; },
trim = function(str){
return nativeTrim ? nativeTrim.call(str) : String(str).replace(new RegExp(/^\s+|\s+$/, 'g'), '');
},
doc = root.document,
sTO = bind(root.setTimeout, root),
cTO = bind(root.clearTimeout, root),
Expand Down

0 comments on commit 94ee94d

Please sign in to comment.