/* knockback-page-navigator-simple.js 0.1.1 (c) 2011, 2012 Kevin Malakoff - http://kmalakoff.github.com/knockback/ License: MIT (http://www.opensource.org/licenses/mit-license.php) Dependencies: Knockout.js, Backbone.js, and Underscore.js. */ (function() { return (function(factory) { // AMD if (typeof define === 'function' && define.amd) { return define('knockback-page-navigator-simple', factory); } // CommonJS/NodeJS or No Loader else { return factory.call(this); } })(function() {// Generated by CoffeeScript 1.3.3 var bind, kb, ko, throwMissing, throwUnexpected, _; throwMissing = function(instance, message) { throw "" + (_.isString(instance) ? instance : instance.constructor.name) + ": " + message + " is missing"; }; throwUnexpected = function(instance, message) { throw "" + (_.isString(instance) ? instance : instance.constructor.name) + ": " + message + " is unexpected"; }; try { this.kb = kb = !this.kb && (typeof require !== 'undefined') ? require('knockback') : this.kb; } catch (e) { ({}); } this.kb || (this.kb = kb || (kb = {})); this.Backbone || (this.Backbone = this.kb.Backbone); try { ko = !this.ko && (typeof require !== 'undefined') ? require('knockout') : this.ko; } catch (e) { ({}); } ko || (ko = {}); if (!ko.observable) { ko.dataFor = function(el) { return null; }; ko.removeNode = function(el) { return $(el).remove(); }; ko.observable = function(initial_value) { var value; value = initial_value; return function(new_value) { if (arguments.length) { return value = new_value; } else { return value; } }; }; ko.observableArray = function(initial_value) { var observable; observable = ko.observable(arguments.length ? initial_value : []); observable.push = function() { return observable().push.apply(observable(), arguments); }; observable.pop = function() { return observable().pop.apply(observable(), arguments); }; return observable; }; } _ = this._ ? this._ : (kb._ ? kb._ : {}); if (!_.bindAll) { bind = function(obj, fn_name) { var fn; fn = obj[fn_name]; return obj[fn_name] = function() { return fn.apply(obj, arguments); }; }; _.bindAll = function(obj, fn_name1) { var fn_name, _i, _len, _ref; _ref = Array.prototype.slice.call(arguments, 1); for (_i = 0, _len = _ref.length; _i < _len; _i++) { fn_name = _ref[_i]; bind(obj, fn_name); } }; } if (!_.isElement) { _.isElement = function(obj) { return obj && (obj.nodeType === 1); }; } if (this.x$) { this.$ = this.x$; } kb.PageNavigatorSimple = (function() { function PageNavigatorSimple(el, options) { this.options = options != null ? options : {}; el || throwMissing(this, 'el'); _.bindAll(this, 'hasHistory', 'activePage', 'activeUrl', 'loadPage', 'dispatcher'); this.el = el.length ? el[0] : el; $(this.el).addClass('page'); this.active_page = ko.observable(); } PageNavigatorSimple.prototype.destroy = function() { this.destroyed = true; this.el = null; return this.active_page = null; }; PageNavigatorSimple.prototype.hasHistory = function() { return false; }; PageNavigatorSimple.prototype.activePage = function() { return this.active_page(); }; PageNavigatorSimple.prototype.activeUrl = function() { var active_page; if ((active_page = this.active_page())) { return active_page.url; } else { return null; } }; PageNavigatorSimple.prototype.loadPage = function(info) { var active_page, previous_page; info || throwMissing(this, 'page info'); if (this.activeUrl() === window.location.hash) { active_page = this.activePage(); active_page.el || pane_navigator.ensureElement(active_page); if (active_page.el.parentNode !== this.el) { this.el.appendChild(active_page.el); } return active_page; } if ((previous_page = this.activePage())) { previous_page.destroy(this.options); } active_page = new kb.Pane(info, window.location.hash); active_page.activate(this.el); this.active_page(active_page); return active_page; }; PageNavigatorSimple.prototype.dispatcher = function(callback) { var page_navigator; page_navigator = this; return function() { page_navigator.destroyed || page_navigator.routeTriggered(this, callback, arguments); }; }; PageNavigatorSimple.prototype.routeTriggered = function(router, callback, args) { var active_page; if ((active_page = this.activePage()) && (active_page.url === window.location.hash)) { return this.loadPage(active_page); } else if (callback) { return callback.apply(router, args); } }; return PageNavigatorSimple; })(); if (typeof exports !== 'undefined') { exports.PageNavigatorSimple = kb.PageNavigatorSimple; } if (ko && ko.bindingHandlers) { ko.bindingHandlers['PageNavigatorSimple'] = { 'init': function(element, value_accessor, all_bindings_accessor, view_model) { var options, page_navigator; options = ko.utils.unwrapObservable(value_accessor()); if (!('no_remove' in options)) { options.no_remove = true; } page_navigator = new kb.PageNavigatorSimple(element, options); kb.utils.wrappedPageNavigator(element, page_navigator); ko.utils.domNodeDisposal.addDisposeCallback(element, function() { if (typeof options.unloaded === "function") { options.unloaded(page_navigator); } return kb.utils.wrappedPageNavigator(element, null); }); return typeof options.loaded === "function" ? options.loaded(page_navigator) : void 0; } }; } kb.override_transitions = []; kb.popOverrideTransition = function() { if (kb.override_transitions.length) { return kb.override_transitions.pop(); } else { return null; } }; kb.dispatchUrl = function(url) { window.location.hash = url; if (window.Backbone && window.Backbone.History.started) { return window.Backbone.history.loadUrl(url); } else if (window.Path) { return window.Path.dispatch(url); } }; kb.loadUrl = function(url, transition) { kb.override_transitions.push(transition); return kb.dispatchUrl(url); }; kb.loadUrlFn = function(url, transition) { return function(vm, event) { kb.loadUrl(url, transition); (!vm || !vm.stopPropagation) || (event = vm); if (event && event.stopPropagation) { event.stopPropagation(); return event.preventDefault(); } }; }; kb.utils || (kb.utils = {}); kb.utils.wrappedPageNavigator = function(el, value) { if ((arguments.length === 1) || (el.__kb_page_navigator === value)) { return el.__kb_page_navigator; } if (el.__kb_page_navigator) { el.__kb_page_navigator.destroy(); } el.__kb_page_navigator = value; return value; }; kb.Pane = (function() { function Pane(info, url) { if (arguments.length) { this.url = url; } this.setInfo(info); } Pane.prototype.destroy = function(options) { if (options == null) { options = {}; } this.deactivate(options); this.removeElement(options, true); this.create = null; return this.el = null; }; Pane.prototype.setInfo = function(info) { var key, value; if (_.isElement(info)) { this.el = info; } else { for (key in info) { value = info[key]; this[key] = value; } } if (this.el) { $(this.el).addClass('pane'); } return this; }; Pane.prototype.ensureElement = function() { var info; if (this.el) { return this.el; } this.create || throwMissing(this, 'create'); info = this.create.apply(this, this.args); if (info) { this.setInfo(info); } this.el || throwMissing(this, 'element'); if (this.el) { $(this.el).addClass('pane'); } return this; }; Pane.prototype.removeElement = function(options, force) { if (options == null) { options = {}; } if (!this.el) { return this; } if (options.no_remove) { return; } if (force || (this.create && !options.no_destroy)) { ko.removeNode(this.el); this.el = null; } else if (this.el.parentNode) { this.el.parentNode.removeChild(this.el); } return this; }; Pane.prototype.activate = function(container_el) { var view_model; this.ensureElement(); if ($(this.el).hasClass('active')) { return; } $(this.el).addClass('active'); if (this.el.parentNode !== container_el) { container_el.appendChild(this.el); } view_model = this.view_model ? this.view_model : ko.dataFor(this.el); if (view_model && view_model.activate) { view_model.activate(this); } return this; }; Pane.prototype.deactivate = function(options) { var view_model; if (options == null) { options = {}; } if (!(this.el && $(this.el).hasClass('active'))) { return; } $(this.el).removeClass('active'); view_model = this.view_model ? this.view_model : ko.dataFor(this.el); if (view_model && view_model.deactivate) { view_model.deactivate(this); } this.removeElement(options); return this; }; return Pane; })(); if (typeof exports !== 'undefined') { exports.Pane = kb.Pane; } ; return kb;}); }).call(this);