Commit 098c0150 authored by Sindre Sorhus's avatar Sindre Sorhus

Merge pull request #645 from mutualmobile/lavaca

Lavaca
parents 7d8221c1 b72a1cb0
......@@ -221,6 +221,9 @@
<li class="routing labs">
<a href="labs/dependency-examples/durandal/" data-source="http://durandaljs.com/" data-content="Single Page Apps Done Right">Durandal</a>
</li>
<li class="routing labs">
<a href="labs/dependency-examples/lavaca_require/" data-source="http://getlavaca.com" data-content="A curated collection of tools for building mobile web applications.">Lavaca + RequireJS</a>
</li>
</ul>
<hr>
<h2>Real-time</h2>
......
{
"name": "todomvc-lavaca_require",
"version": "0.0.0",
"dependencies": {
"todomvc-common": "~0.1.6",
"requirejs": "~2.1.6",
"dustjs-linkedin": "~1.1.1",
"dustjs-linkedin-helpers": "~1.1.1",
"jquery": "~2.0.3",
"mout": "~0.7.1"
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
define(['./prop', '../object/deepMatches'], function(prop, deepMatches) {
/**
* Converts argument into a valid iterator.
* Used internally on most array/object/collection methods that receives a
* callback/iterator providing a shortcut syntax.
*/
function makeIterator(src, thisObj){
switch(typeof src) {
case 'object':
// typeof null == "object"
return (src != null)? function(val, key, target){
return deepMatches(val, src);
} : src;
case 'string':
case 'number':
return prop(src);
case 'function':
if (typeof thisObj === 'undefined') {
return src;
} else {
return function(val, i, arr){
return src.call(thisObj, val, i, arr);
};
}
default:
return src;
}
}
return makeIterator;
});
define(function () {
/**
* Returns a function that gets a property of the passed object
*/
function prop(name){
return function(obj){
return obj[name];
};
}
return prop;
});
define(['./kindOf', './isPlainObject', '../object/mixIn'], function (kindOf, isPlainObject, mixIn) {
/**
* Clone native types.
*/
function clone(val){
switch (kindOf(val)) {
case 'Object':
return cloneObject(val);
case 'Array':
return cloneArray(val);
case 'RegExp':
return cloneRegExp(val);
case 'Date':
return cloneDate(val);
default:
return val;
}
}
function cloneObject(source) {
if (isPlainObject(source)) {
return mixIn({}, source);
} else {
return source;
}
}
function cloneRegExp(r) {
var flags = '';
flags += r.multiline ? 'm' : '';
flags += r.global ? 'g' : '';
flags += r.ignorecase ? 'i' : '';
return new RegExp(r.source, flags);
}
function cloneDate(date) {
return new Date(+date);
}
function cloneArray(arr) {
return arr.slice();
}
return clone;
});
define(['./clone', '../object/forOwn', './kindOf', './isPlainObject'], function (clone, forOwn, kindOf, isPlainObject) {
/**
* Recursively clone native types.
*/
function deepClone(val, instanceClone) {
switch ( kindOf(val) ) {
case 'Object':
return cloneObject(val, instanceClone);
case 'Array':
return cloneArray(val, instanceClone);
default:
return clone(val);
}
}
function cloneObject(source, instanceClone) {
if (isPlainObject(source)) {
var out = {};
forOwn(source, function(val, key) {
this[key] = deepClone(val, instanceClone);
}, out);
return out;
} else if (instanceClone) {
return instanceClone(source);
} else {
return source;
}
}
function cloneArray(arr, instanceClone) {
var out = [],
i = -1,
n = arr.length,
val;
while (++i < n) {
out[i] = deepClone(arr[i], instanceClone);
}
return out;
}
return deepClone;
});
define(['./isKind'], function (isKind) {
/**
*/
var isArray = Array.isArray || function (val) {
return isKind(val, 'Array');
};
return isArray;
});
define(['./kindOf'], function (kindOf) {
/**
* Check if value is from a specific "kind".
*/
function isKind(val, kind){
return kindOf(val) === kind;
}
return isKind;
});
define(['./isKind'], function (isKind) {
/**
*/
function isObject(val) {
return isKind(val, 'Object');
}
return isObject;
});
define(function () {
/**
* Checks if the value is created by the `Object` constructor.
*/
function isPlainObject(value) {
return (!!value
&& typeof value === 'object'
&& value.constructor === Object);
}
return isPlainObject;
});
define(function () {
var _rKind = /^\[object (.*)\]$/,
_toString = Object.prototype.toString,
UNDEF;
/**
* Gets the "kind" of value. (e.g. "String", "Number", etc)
*/
function kindOf(val) {
if (val === null) {
return 'Null';
} else if (val === UNDEF) {
return 'Undefined';
} else {
return _rKind.exec( _toString.call(val) )[1];
}
}
return kindOf;
});
define(['./forOwn', '../lang/isArray'], function(forOwn, isArray) {
function containsMatch(array, pattern) {
var i = -1, length = array.length;
while (++i < length) {
if (deepMatches(array[i], pattern)) {
return true;
}
}
return false;
}
function matchArray(target, pattern) {
var i = -1, patternLength = pattern.length;
while (++i < patternLength) {
if (!containsMatch(target, pattern[i])) {
return false;
}
}
return true;
}
function matchObject(target, pattern) {
var result = true;
forOwn(pattern, function(val, key) {
if (!deepMatches(target[key], val)) {
// Return false to break out of forOwn early
return (result = false);
}
});
return result;
}
/**
* Recursively check if the objects match.
*/
function deepMatches(target, pattern){
if (target && typeof target === 'object') {
if (isArray(target) && isArray(pattern)) {
return matchArray(target, pattern);
} else {
return matchObject(target, pattern);
}
} else {
return target === pattern;
}
}
return deepMatches;
});
define(['./forOwn', '../lang/isPlainObject'], function (forOwn, isPlainObject) {
/**
* Mixes objects into the target object, recursively mixing existing child
* objects.
*/
function deepMixIn(target, objects) {
var i = 0,
n = arguments.length,
obj;
while(++i < n){
obj = arguments[i];
if (obj) {
forOwn(obj, copyProp, target);
}
}
return target;
}
function copyProp(val, key) {
var existing = this[key];
if (isPlainObject(val) && isPlainObject(existing)) {
deepMixIn(existing, val);
} else {
this[key] = val;
}
}
return deepMixIn;
});
define(['./hasOwn', './every', '../lang/isObject'], function(hasOwn, every, isObject) {
function defaultCompare(a, b) {
return a === b;
}
// Makes a function to compare the object values from the specified compare
// operation callback.
function makeCompare(callback) {
return function(value, key) {
return hasOwn(this, key) && callback(value, this[key]);
};
}
function checkProperties(value, key) {
return hasOwn(this, key);
}
/**
* Checks if two objects have the same keys and values.
*/
function equals(a, b, callback) {
callback = callback || defaultCompare;
if (!isObject(a) || !isObject(b)) {
return callback(a, b);
}
return (every(a, makeCompare(callback), b) &&
every(b, checkProperties, a));
}
return equals;
});
define(['./forOwn', '../function/makeIterator_'], function(forOwn, makeIterator) {
/**
* Object every
*/
function every(obj, callback, thisObj) {
callback = makeIterator(callback, thisObj);
var result = true;
forOwn(obj, function(val, key) {
// we consider any falsy values as "false" on purpose so shorthand
// syntax can be used to check property existence
if (!callback(val, key, obj)) {
result = false;
return false; // break
}
});
return result;
}
return every;
});
define(function () {
var _hasDontEnumBug,
_dontEnums;
function checkDontEnum(){
_dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
];
_hasDontEnumBug = true;
for (var key in {'toString': null}) {
_hasDontEnumBug = false;
}
}
/**
* Similar to Array/forEach but works over object properties and fixes Don't
* Enum bug on IE.
* based on: http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
*/
function forIn(obj, fn, thisObj){
var key, i = 0;
// no need to check if argument is a real object that way we can use
// it for arrays, functions, date, etc.
//post-pone check till needed
if (_hasDontEnumBug == null) checkDontEnum();
for (key in obj) {
if (exec(fn, obj, key, thisObj) === false) {
break;
}
}
if (_hasDontEnumBug) {
while (key = _dontEnums[i++]) {
// since we aren't using hasOwn check we need to make sure the
// property was overwritten
if (obj[key] !== Object.prototype[key]) {
if (exec(fn, obj, key, thisObj) === false) {
break;
}
}
}
}
}
function exec(fn, obj, key, thisObj){
return fn.call(thisObj, obj[key], key, obj);
}
return forIn;
});
define(['./hasOwn', './forIn'], function (hasOwn, forIn) {
/**
* Similar to Array/forEach but works over object properties and fixes Don't
* Enum bug on IE.
* based on: http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
*/
function forOwn(obj, fn, thisObj){
forIn(obj, function(val, key){
if (hasOwn(obj, key)) {
return fn.call(thisObj, obj[key], key, obj);
}
});
}
return forOwn;
});
define(function () {
/**
* Safer Object.hasOwnProperty
*/
function hasOwn(obj, prop){
return Object.prototype.hasOwnProperty.call(obj, prop);
}
return hasOwn;
});
define(['./hasOwn', '../lang/deepClone', '../lang/isObject'], function (hasOwn, deepClone, isObject) {
/**
* Deep merge objects.
*/
function merge() {
var i = 1,
key, val, obj, target;
// make sure we don't modify source element and it's properties
// objects are passed by reference
target = deepClone( arguments[0] );
while (obj = arguments[i++]) {
for (key in obj) {
if ( ! hasOwn(obj, key) ) {
continue;
}
val = obj[key];
if ( isObject(val) && isObject(target[key]) ){
// inception, deep merge objects
target[key] = merge(target[key], val);
} else {
// make sure arrays, regexp, date, objects are cloned
target[key] = deepClone(val);
}
}
}
return target;
}
return merge;
});
define(['./forOwn'], function(forOwn){
/**
* Combine properties from all the objects into first one.
* - This method affects target object in place, if you want to create a new Object pass an empty object as first param.
* @param {object} target Target Object
* @param {...object} objects Objects to be combined (0...n objects).
* @return {object} Target Object.
*/
function mixIn(target, objects){
var i = 0,
n = arguments.length,
obj;
while(++i < n){
obj = arguments[i];
if (obj != null) {
forOwn(obj, copyProp, target);
}
}
return target;
}
function copyProp(val, key){
this[key] = val;
}
return mixIn;
});
(function () {
'use strict';
// Underscore's Template Module
// Courtesy of underscorejs.org
var _ = (function (_) {
_.defaults = function (object) {
if (!object) {
return object;
}
for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
var iterable = arguments[argsIndex];
if (iterable) {
for (var key in iterable) {
if (object[key] == null) {
object[key] = iterable[key];
}
}
}
}
return object;
}
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
_.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\t': 't',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_.template = function(text, data, settings) {
var render;
settings = _.defaults({}, settings, _.templateSettings);
// Combine delimiters into one regular expression via alternation.
var matcher = new RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
}
if (interpolate) {
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
}
if (evaluate) {
source += "';\n" + evaluate + "\n__p+='";
}
index = offset + match.length;
return match;
});
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
source + "return __p;\n";
try {
render = new Function(settings.variable || 'obj', '_', source);
} catch (e) {
e.source = source;
throw e;
}
if (data) return render(data, _);
var template = function(data) {
return render.call(this, data, _);
};
// Provide the compiled function source as a convenience for precompilation.
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
return template;
};
return _;
})({});
if (location.hostname === 'todomvc.com') {
window._gaq = [['_setAccount','UA-31081062-1'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'));
}
function redirect() {
if (location.hostname === 'tastejs.github.io') {
location.href = location.href.replace('tastejs.github.io/todomvc', 'todomvc.com');
}
}
function findRoot() {
var base;
[/labs/, /\w*-examples/].forEach(function (href) {
var match = location.href.match(href);
if (!base && match) {
base = location.href.indexOf(match);
}
});
return location.href.substr(0, base);
}
function getFile(file, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', findRoot() + file, true);
xhr.send();
xhr.onload = function () {
if (xhr.status === 200 && callback) {
callback(xhr.responseText);
}
};
}
function Learn(learnJSON, config) {
if (!(this instanceof Learn)) {
return new Learn(learnJSON, config);
}
var template, framework;
if (typeof learnJSON !== 'object') {
try {
learnJSON = JSON.parse(learnJSON);
} catch (e) {
return;
}
}
if (config) {
template = config.template;
framework = config.framework;
}
if (!template && learnJSON.templates) {
template = learnJSON.templates.todomvc;
}
if (!framework && document.querySelector('[data-framework]')) {
framework = document.querySelector('[data-framework]').getAttribute('data-framework');
}
if (template && learnJSON[framework]) {
this.frameworkJSON = learnJSON[framework];
this.template = template;
this.append();
}
}
Learn.prototype.append = function () {
var aside = document.createElement('aside');
aside.innerHTML = _.template(this.template, this.frameworkJSON);
aside.className = 'learn';
// Localize demo links
var demoLinks = aside.querySelectorAll('.demo-link');
Array.prototype.forEach.call(demoLinks, function (demoLink) {
demoLink.setAttribute('href', findRoot() + demoLink.getAttribute('href'));
});
document.body.className = (document.body.className + ' learn-bar').trim();
document.body.insertAdjacentHTML('afterBegin', aside.outerHTML);
};
redirect();
getFile('learn.json', Learn);
})();
<!doctype html>
<html lang="en" data-framework="lavaca">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Lavaca • TodoMVC</title>
<link rel="stylesheet" href="bower_components/todomvc-common/base.css">
</head>
<body>
<section id="todoapp"></section>
<footer id="info">
<p>Double-click to edit a todo</p>
<p>Created by <a href="https://github.com/mutualmobile/" data-external>Mutual Mobile</a></p>
<p>Part of <a href="http://todomvc.com" data-external>TodoMVC</a></p>
</footer>
<script src="bower_components/todomvc-common/base.js"></script>
<script src="bower_components/requirejs/require.js"></script>
<script src="js/libs/lavaca.js"></script>
<script src="js/app/boot.js"></script>
</body>
</html>
\ No newline at end of file
define(function (require) {
'use strict';
var History = require('lavaca/net/History');
var Application = require('lavaca/mvc/Application');
var TodosController = require('app/net/TodosController');
var $ = require('$');
require('lavaca/ui/DustTemplate');
// Uncomment this section to use hash-based browser history instead of HTML5 history.
// You should use hash-based history if there's no server-side component supporting your app's routes.
History.overrideStandardsMode();
// Override Lavaca's default view-root selector to match
// the TodoMVC template file better
Application.prototype.viewRootSelector = '#todoapp';
/**
* @class app
* @super Lavaca.mvc.Application
* Global application-specific object
*/
var app = new Application(function () {
// Initialize the routes
this.router.add({
'/': [TodosController, 'home', {filter: 'all'}],
'/active': [TodosController, 'home', {filter: 'active'}],
'/completed': [TodosController, 'home', {filter: 'completed'}]
});
// Patch learn sidebar links so they get ignored by the router
$(document.body).find('aside.learn a').addClass('ignore');
});
return app;
});
\ No newline at end of file
require.config({
baseUrl: 'js',
paths: {
$: '../bower_components/jquery/jquery',
jquery: '../bower_components/jquery/jquery',
mout: '../bower_components/mout/src',
dust: '../bower_components/dustjs-linkedin/dist/dust-full-1.1.1',
'dust-helpers': '../bower_components/dustjs-linkedin-helpers/dist/dust-helpers-1.1.1',
rdust: 'libs/require-dust',
lavaca: 'Lavaca',
Lavaca: 'lavaca'
},
shim: {
$: {
exports: '$'
},
jquery: {
exports: '$'
},
dust: {
exports: 'dust'
},
'dust-helpers': {
deps: ['dust']
},
templates: {
deps: ['dust']
}
}
});
require(['app/app']);
\ No newline at end of file
define(function(require) {
'use strict';
// Constants
var LOCAL_STORAGE_KEY = 'todos-lavaca-require';
var Collection = require('lavaca/mvc/Collection');
/**
* A collection of ToDo items that saves
* and restores its data from localStorage
* @class app.models.TodosCollection
* @super Lavaca.mvc.Collection
*/
var TodosCollection = Collection.extend(function TodosCollection() {
// Call the super class' constructor
Collection.apply(this, arguments);
// Set some computed properties on this model
this.apply({
allComplete: allComplete,
itemsLeft: itemsLeft,
itemsCompleted: itemsCompleted
});
// Restore any data from localStorage
restore.call(this);
// Listen for changes to the models and then
// save them in localStorage
this.on('addItem', store);
this.on('moveItem', store);
this.on('removeItem', store);
this.on('changeItem', store);
}, {
/**
* @method removeCompleted
* Remove models that are complete
*/
removeCompleted: function () {
this.remove({completed: true});
}
});
/* ---- Computed Properties ---- */
// Returns a boolean indicating whether
// all items are complete
function allComplete() {
var allAreComplete = true;
this.each(function (index, model) {
if (!model.get('completed')) {
allAreComplete = false;
return false; // break out of `each` loop
}
});
return allAreComplete;
}
// Returns a count of incomplete items
function itemsLeft() {
return this.filter({completed: false}).length;
}
// Returns a count of complete items
function itemsCompleted() {
return this.filter({completed: true}).length;
}
/* ---- Handle Persistence ---- */
// Called every time the models change.
// Set a timeout that will write the data
// to localStorage. Clear the timeout with
// every call to make sure that data is only
// written once even if multiple changes
// are made in the same run loop
function store() {
clearTimeout(this.storeTimer);
this.storeTimer = setTimeout(function () {
// Safari will throw an exception
// when trying to write to localStorage in
// private browsing mode
try {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(this.toObject().items));
} catch (e) {}
}.bind(this));
}
// Pull the data from localStorage and
// add it to the collection
function restore() {
var data;
var i;
try {
data = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));
if (data) {
for (i = 0; i < data.length; i++) {
this.add(data[i]);
}
}
} catch (e) {}
}
return new TodosCollection();
});
define(function(require) {
'use strict';
var Controller = require('lavaca/mvc/Controller');
var TodosView = require('app/ui/views/TodosView');
var todosCollection = require('app/models/TodosCollection');
/**
* @class app.net.TodosController
* @super Lavaca.mvc.Controller
* Todos controller
*/
var TodosController = Controller.extend({
home: function (params) {
// Set the `filter` parameter on the collection based
// on the values defined with the routes in app.js
todosCollection.set('filter', params.filter);
// Create an instance of TodosView with `collection` as its model
// and then set a history state which will update the URL
return this
.view(null, TodosView, todosCollection)
.then(this.history({}, document.title, params.url));
}
});
return TodosController;
});
\ No newline at end of file
define(function (require) {
'use strict';
var View = require('lavaca/mvc/View');
var Promise = require('lavaca/util/Promise');
var $ = require('$');
var _UNDEFINED;
/**
* A view for synchronizing a collection of models with sub views
* @class app.ui.views.CollectionView
* @super lavaca.mvc.View
*/
var CollectionView = View.extend(function CollectionView() {
// Call the super class' constructor
View.apply(this, arguments);
this.el.empty();
this.collectionViews = [];
this.mapEvent({
model: {
'addItem': this.onItemEvent.bind(this),
'moveItem': this.onItemEvent.bind(this),
'removeItem': this.onItemEvent.bind(this),
'changeItem': this.onItemEvent.bind(this)
}
});
}, {
/**
* A class name added to the view container
* @property className
* @type String
*/
className: 'collection-view',
/**
* A function that should return a jQuery element
* that will be used as the `el` for a particular
* item in the collection. The function is passed
* two parameters, the model and the index.
* @property itemEl
* @type jQuery
*/
itemEl: function () {
return $('<div/>'); // default to <li>?
},
/**
* The view type used for each item view
* @property TView
* @type lavaca.mvc.View
*/
TView: View,
/**
* Initializes and renders all item views to be shown
* @method render
*/
render: function () {
var models = this.model.filter(this.modelFilter.bind(this));
var fragment = document.createDocumentFragment();
var view;
models.forEach(function (item) {
view = this.addItemView(item);
fragment.appendChild(view.el[0]);
}.bind(this));
this.trigger('rendersuccess', {html: fragment});
return new Promise().resolve();
},
/**
* Creates a new view and inserts it into the DOM at the
* provided index
* @method addItemView
* @param {Object} [model] the model for the view
* @param {Number} [index] the index to insert the view in the DOM
*/
addItemView: function (model, index) {
var count = this.collectionViews.length;
var insertIndex = index === null || index === _UNDEFINED ? count : index;
var view;
if (insertIndex < 0 || insertIndex > count) {
throw 'Invalid item view insertion index';
}
view = new this.TView(this.itemEl(model, index), model, this);
this.childViews.set(view.id, view);
this.collectionViews.splice(insertIndex, 0, view);
this.applyChildViewEvent(view);
if (insertIndex === 0) {
this.el.prepend(view.el[0]);
} else if (insertIndex === count) {
this.el.append(view.el[0]);
} else {
this.el
.children()
.eq(insertIndex - 1)
.after(view.el[0]);
}
return view;
},
/**
* adds listeners for a specific child view
* @method applyChildViewEvent
* @param {Object} [view] the view to add listeners to
*/
applyChildViewEvent: function (view) {
var childViewEventMap = this.childViewEventMap;
var type;
for (type in childViewEventMap) {
if (view instanceof childViewEventMap[type].TView) {
view.on(type, childViewEventMap[type].callback);
}
}
},
/**
* Remove and disposes a view
* @method removeItemView
* @param {Number} [index] the index of the view to remove
*/
removeItemView: function (index) {
var view = this.collectionViews.splice(index, 1)[0];
this.childViews.remove(view.id);
view.el.remove();
view.dispose();
},
/**
* Returns the index of a view
* @method getViewIndexByModel
* @param {Object} [model] the model of the view to find
*/
getViewIndexByModel: function (model) {
var collectionViewIndex = -1;
this.collectionViews.every(function (view, i) {
if (view.model === model) {
collectionViewIndex = i;
return false;
}
return true;
});
return collectionViewIndex;
},
/**
* The filter to run against the collection
* @method modelFilter
* @param {Number} [i] the index
* @param {Object} [model] the model
*/
modelFilter: function () {
return true;
},
/**
* Event handler for all collection events that produces all add, remove, and move actions
* @method modelFilter
* @param {Obejct} [e] the event
*/
onItemEvent: function () {
var models = this.model.filter(this.modelFilter.bind(this));
var i = -1;
var model, view, viewIndex, oldIndex, modelIndex, temp;
// Add new views
while (model = models[++i]) {
viewIndex = this.getViewIndexByModel(model);
if (viewIndex === -1) {
this.addItemView(model, i);
}
}
// Remove Old Views
i = -1;
while (view = this.collectionViews[++i]) {
modelIndex = models.indexOf(view.model);
if (modelIndex === -1) {
this.removeItemView(i);
}
}
// Move any existing views
i = -1;
while (model = models[++i]) {
oldIndex = this.getViewIndexByModel(model);
if (oldIndex !== i) {
this.swapViews(this.collectionViews[i], this.collectionViews[oldIndex]);
temp = this.collectionViews[oldIndex];
this.collectionViews[oldIndex] = this.collectionViews[i];
this.collectionViews[i] = temp;
}
}
},
/**
* Swaps two views in the DOM
* @method swapViews
* @param {Obejct} [viewA] a view
* @param {Obejct} [viewB] another view
*/
swapViews: function (viewA, viewB) {
var a = viewA.el[0];
var b = viewB.el[0];
var aParent = a.parentNode;
var aSibling = a.nextSibling === b ? a : a.nextSibling;
b.parentNode.insertBefore(a, b);
aParent.insertBefore(b, aSibling);
}
});
return CollectionView;
});
define(function (require) {
'use strict';
// constants
var ENTER_KEY = 13;
var ESC_KEY = 27;
var View = require('lavaca/mvc/View');
require('rdust!templates/todo-item');
/**
* Todos view type
* @class app.ui.views.TodosView
* @super Lavaca.mvc.View
*/
var TodosView = View.extend(function TodosView() {
// Call the super class' constructor
View.apply(this, arguments);
this.mapEvent({
'input.toggle': {
change: toggleComplete.bind(this)
},
'self': {
dblclick: startEditing.bind(this)
},
'input.edit': {
keydown: editTodo.bind(this),
blur: endEditing.bind(this)
},
'button.destroy': {
click: remove.bind(this)
},
model: {
'change': this.onModelChange.bind(this)
}
});
this.render();
}, {
/**
* The name for the template used in the view
* @property template
* @type String
*/
template: 'templates/todo-item',
/**
* A class name added to the view container
* @property className
* @type String
*/
className: 'todo-item',
/**
* Executes when the template renders successfully
* @method onRenderSuccess
*/
onRenderSuccess: function () {
View.prototype.onRenderSuccess.apply(this, arguments);
this.checkIfCompleted();
},
/**
* Redraws template with model
* @method redraw
*/
redraw: function () {
View.prototype.redraw.apply(this, arguments)
.then(this.checkIfCompleted.bind(this));
},
/**
* Adds/Removes a completed class to view element,
* this wouldnt be needed if we restructured some DOM or CSS
* @method checkIfCompleted
*/
checkIfCompleted: function () {
this.$input = this.el.find('.edit'); // cache input element
if (this.model.get('completed')) {
this.el.addClass('completed');
} else {
this.el.removeClass('completed');
}
},
/**
* Redraws template when model changes
* @method onModelChange
*/
onModelChange: function () {
if (this.model) {
this.redraw();
}
}
});
// Set the completion state of a single model
function toggleComplete(e) {
this.model.set('completed', e.currentTarget.checked);
}
// Start editing a Todo
function startEditing() {
this.el.addClass('editing');
this.$input.focus();
this.$input.val(this.model.get('title')); // resetting value fixes text selection on focus
}
// Commit the edit when the ENTER key
// is pressed
function editTodo(e) {
if (e.which === ENTER_KEY) {
endEditing.call(this);
} else if (e.which === ESC_KEY) {
this.$input.val(this.model.get('title'));
endEditing.call(this);
}
}
function endEditing() {
var trimmedValue = this.$input.val().trim();
this.el.removeClass('editing');
if (trimmedValue) {
this.model.set('title', trimmedValue);
} else {
remove.call(this);
}
}
// Remove the Todo when the 'x' is clicked
function remove() {
this.trigger('removeView', {model: this.model});
}
return TodosView;
});
define(function (require) {
'use strict';
var CollectionView = require('app/ui/views/CollectionView');
var TodoItemView = require('app/ui/views/TodoItemView');
var $ = require('$');
/**
* Todos view type
* @class app.ui.views.TodosCollectionView
* @super app/ui/views/CollectionView
*/
var TodosCollectionView = CollectionView.extend(function TodosCollectionView() {
CollectionView.apply(this, arguments);
this.mapChildViewEvent('removeView', this.onRemoveView.bind(this), this.TView);
this.render();
}, {
/**
* A class name added to the view container
* @property className
* @type String
*/
className: 'todos-collection-view',
/**
* A function that should return a jQuery element
* that will be used as the `el` for a particular
* item in the collection. The function is passed
* two parameters, the model and the index.
* @property itemEl
* @type jQuery
*/
itemEl: function () {
return $('<li/>');
},
/**
* The view type used for each item view
* @property itemEl
* @type lavaca.mvc.View
*/
TView: TodoItemView,
/**
* Executes when the template renders successfully
* @method onRenderSuccess
*
* @param {Event} e The render event. This object should have a string property named "html"
* that contains the template's rendered HTML output.
*/
onRenderSuccess: function (e) {
this.el.html(e.html);
this.bindMappedEvents();
this.applyEvents();
this.createWidgets();
this.el.data('view', this);
this.el.attr('data-view-id', this.id);
this.hasRendered = true;
},
/**
* The filter to run against the collection
* @method modelFilter
* @param {Number} [i] the index
* @param {Object} [model] the model
*/
modelFilter: function (i, model) {
var filter = this.model.get('filter');
var shouldBeComplete = filter === 'completed';
if (filter === 'all' || model.get('completed') === shouldBeComplete) {
return true;
}
},
/**
* Removes a view when removeView event it triggered
* @method swapViews
* @param {Obejct} [viewA] a view
* @param {Obejct} [viewB] another view
*/
onRemoveView: function (e) {
this.model.remove(e.model);
}
});
return TodosCollectionView;
});
\ No newline at end of file
define(function (require) {
'use strict';
// constants
var ENTER_KEY = 13;
var PageView = require('lavaca/mvc/PageView');
var TodosCollectionView = require('app/ui/views/TodosCollectionView');
require('rdust!templates/todos');
/**
* Todos view type
* @class app.ui.views.TodosView
* @super Lavaca.mvc.PageView
*/
var TodosView = PageView.extend(function TodosView() {
// Call the super class' constructor
PageView.apply(this, arguments);
// Map collection view to #todo-list
this.mapChildView('#todo-list', TodosCollectionView, this.model);
// Map DOM and model events to event handler
// functions declared below
this.mapEvent({
'#new-todo': {
keypress: addTodo.bind(this)
},
'input#toggle-all': {
change: toggleAll.bind(this)
},
'button#clear-completed': {
click: removeCompleted.bind(this)
},
model: {
'addItem': modelChange.bind(this),
'moveItem': modelChange.bind(this),
'removeItem': modelChange.bind(this),
'changeItem': modelChange.bind(this)
}
});
this.countIsZero = !this.model.count();
}, {
/**
* @field {String} template
* @default 'example'
* The name of the template used by the view
*/
template: 'templates/todos',
/**
* @field {String} className
* @default 'example'
* A class name added to the view container
*/
className: 'todos'
});
/* ---- Event Handlers ---- */
// Whenever the model changes, set a timeout
// that will re-render the view's template
// and update the DOM. Clear the timeout with
// every call to make sure that the redraw
// only happens once even if multiple changes
// are made in the same run loop
function modelChange() {
clearTimeout(this.redrawTimeout);
this.redrawTimeout = setTimeout(function () {
var count = this.model.count();
if (count === 0) {
this.countIsZero = true;
this.redraw();
} else if (this.countIsZero && count) {
this.countIsZero = false;
this.redraw();
} else {
this.redraw('#footer, #toggle-all');
}
}.bind(this));
}
// Create a new Todo when the ENTER
// key is pressed
function addTodo(e) {
var input = e.currentTarget;
var val;
if (e.which === ENTER_KEY) {
val = input.value.trim();
if (val) {
this.model.add({
id: Date.now(),
title: val,
completed: false
});
input.value = '';
}
e.preventDefault();
}
}
// Set the completion state of all models
function toggleAll(e) {
this.model.each(function (index, model) {
model.set('completed', e.currentTarget.checked);
});
}
// Remove all completed Todos
function removeCompleted() {
this.model.removeCompleted();
}
return TodosView;
});
\ No newline at end of file
This diff is collapsed.
/**
* AMD implementation for dust.js
* This is based on require-cs code.
* see: http://github.com/jrburke/require-cs for details
*/
/*jslint */
/*global define, window, XMLHttpRequest, importScripts, Packages, java,
ActiveXObject, process, require */
define(function() {
'use strict';
var fs, getXhr,
progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
fetchText = function () {
throw new Error('Environment unsupported.');
},
buildMap = {},
isBrowser = (typeof window !== "undefined" && window.navigator && window.document) || typeof importScripts !== "undefined",
isNode = typeof process !== "undefined" && process.versions && !!process.versions.node;
if (typeof process !== "undefined" &&
process.versions &&
!!process.versions.node) {
//Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');
fetchText = function (path, callback) {
callback(fs.readFileSync(path, 'utf8'));
};
} else if ((typeof window !== "undefined" && window.navigator && window.document) || typeof importScripts !== "undefined") {
// Browser action
getXhr = function () {
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
for (i = 0; i < 3; i++) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}
if (!xhr) {
throw new Error("getXhr(): XMLHttpRequest not available");
}
return xhr;
};
fetchText = function (url, callback) {
var xhr = getXhr();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
};
// end browser.js adapters
} else if (typeof Packages !== 'undefined') {
//Why Java, why is this so awkward?
fetchText = function (path, callback) {
var encoding = "utf-8",
file = new java.io.File(path),
lineSeparator = java.lang.System.getProperty("line.separator"),
input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
stringBuffer, line,
content = '';
try {
stringBuffer = new java.lang.StringBuffer();
line = input.readLine();
// Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
// http://www.unicode.org/faq/utf_bom.html
// Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
if (line && line.length() && line.charAt(0) === 0xfeff) {
// Eat the BOM, since we've already found the encoding on this file,
// and we plan to concatenating this buffer with others; the BOM should
// only appear at the top of a file.
line = line.substring(1);
}
stringBuffer.append(line);
while ((line = input.readLine()) !== null) {
stringBuffer.append(lineSeparator);
stringBuffer.append(line);
}
//Make sure we return a JavaScript string and not a Java string.
content = String(stringBuffer.toString()); //String
} finally {
input.close();
}
callback(content);
};
}
return {
write: function (pluginName, name, write) {
if (buildMap.hasOwnProperty(name)) {
var text = buildMap[name];
write.asModule(pluginName + "!" + name, text);
}
},
load: function (name, parentRequire, load, config) {
var self = this;
if (isBrowser) {
require(['dust'], function(dust) { self.process(dust, name, parentRequire, load, config); });
} else if(isNode) {
self.process(require.nodeRequire('dustjs-linkedin'), name, parentRequire, load, config);
}
},
process: function(dust, name, parentRequire, load, config) {
var path = parentRequire.toUrl(name + '.html');
fetchText(path, function (text) {
//Do dust transform.
try {
text = "define(['dust'],function(dust){"+dust.compile(text, name)+" return {render: function(context, callback) {return dust.render('"+name+"', context, callback)}}})";
}
catch (err) {
err.message = "In " + path + ", " + err.message;
throw(err);
}
//Hold on to the transformed text if a build.
if (config.isBuild) {
buildMap[name] = text;
}
//IE with conditional comments on cannot handle the
//sourceURL trick, so skip it if enabled.
/*@if (@_jscript) @else @*/
if (!config.isBuild) {
text += "\r\n//@ sourceURL=" + path;
}
/*@end@*/
load.fromText('rdust!' + name, text);
});
}
};
});
<div class="view" data-id="{id}">
<input class="toggle" type="checkbox" {?completed}checked{/completed}>
<label>{title}</label>
<button class="destroy"></button>
</div>
<input class="edit" value="{title}">
\ No newline at end of file
<header id="header">
<h1>todos</h1>
<input id="new-todo" placeholder="What needs to be done?" autofocus>
</header>
{?items}
<section id="main">
<input id="toggle-all" type="checkbox" {?allComplete}checked{/allComplete}>
<label for="toggle-all">Mark all as complete</label>
<ul id="todo-list" {@eq key=filter value="active"}class="active"{/eq}{@eq key=filter value="completed"}class="completed"{/eq}></ul>
</section>
<footer id="footer">
<span id="todo-count"><strong>{itemsLeft}</strong> item{@ne key=itemsLeft value="1" type="number"}s{/ne} left</span>
<ul id="filters">
<li>
<a {@eq key=filter value="all"}class="selected"{/eq} href="/">All</a>
</li>
<li>
<a {@eq key=filter value="active"}class="selected"{/eq} href="/active">Active</a>
</li>
<li>
<a {@eq key=filter value="completed"}class="selected"{/eq} href="/completed">Completed</a>
</li>
</ul>
{@gt key=itemsCompleted value="0"}<button id="clear-completed">Clear completed ({itemsCompleted})</button>{/gt}
</footer>
{/items}
\ No newline at end of file
......@@ -1245,6 +1245,40 @@
}]
}]
},
"lavaca": {
"name": "Lavaca",
"description": "A curated collection of tools for building mobile web applications.",
"homepage": "getlavaca.com",
"examples": [{
"name": "Dependency Example",
"url": "labs/dependency-examples/lavaca_require"
}],
"link_groups": [{
"heading": "Official Resources",
"links": [{
"name": "Guide",
"url": "http://getlavaca.com/#/guide"
}, {
"name": "API Reference",
"url": "http://getlavaca.com/#/apidoc"
}, {
"name": "Live examples",
"url": "http://getlavaca.com/#/examples"
}]
}, {
"heading": "Articles and Guides",
"links": [{
"name": "Why Lavaca is the only sane HTML5 mobile development framework out there",
"url": "http://povolotski.me/2013/09/20/lavaca-intro/"
}]
}, {
"heading": "Community",
"links": [{
"name": "Lavaca on Twitter",
"url": "http://twitter.com/getlavaca"
}]
}]
},
"maria": {
"name": "Maria",
"description": "The MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC.",
......
......@@ -79,6 +79,7 @@ We also have a number of in-progress applications in Labs:
- [Aria Templates](http://ariatemplates.com/)
- [Enyo + Backbone.js](http://enyojs.com/)
- [SAPUI5](http://scn.sap.com/community/developer-center/front-end)
- [Lavaca](http://getlavaca.com) + [RequireJS](http://requirejs.org) (using AMD)
## Live demos
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment