Resources/js/can.jquery-1.1.6.js
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 02 Sep 2013 02:22:21 -0700
changeset 0 80c32ef237c6
permissions -rw-r--r--
Initial commit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
/*!
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
 * CanJS - 1.1.6
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
 * http://canjs.us/
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
 * Copyright (c) 2013 Bitovi
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
 * Mon, 15 Jul 2013 04:14:43 GMT
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
 * Licensed MIT
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
 * Includes: can/construct/construct.js,can/observe/observe.js,can/observe/compute/compute.js,can/model/model.js,can/view/view.js,can/view/ejs/ejs.js,can/control/control.js,can/route/route.js,can/control/route/route.js,can/observe/backup/backup.js,can/util/object/object.js,can/util/string/string.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
 * Download from: http://bitbuilder.herokuapp.com/can.custom.js?configuration=jquery&plugins=can%2Fconstruct%2Fconstruct.js&plugins=can%2Fobserve%2Fobserve.js&plugins=can%2Fobserve%2Fcompute%2Fcompute.js&plugins=can%2Fmodel%2Fmodel.js&plugins=can%2Fview%2Fview.js&plugins=can%2Fview%2Fejs%2Fejs.js&plugins=can%2Fcontrol%2Fcontrol.js&plugins=can%2Froute%2Froute.js&plugins=can%2Fcontrol%2Froute%2Froute.js&plugins=can%2Fobserve%2Fbackup%2Fbackup.js&plugins=can%2Futil%2Fobject%2Fobject.js&plugins=can%2Futil%2Fstring%2Fstring.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
 */
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
(function(undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
    // ## can/util/can.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
    var __m5 = (function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
        var can = window.can || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
        if (typeof GLOBALCAN === 'undefined' || GLOBALCAN !== false) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
            window.can = can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
        can.isDeferred = function(obj) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
            var isFunction = this.isFunction;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
            // Returns `true` if something looks like a deferred.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
            return obj && isFunction(obj.then) && isFunction(obj.pipe);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
        var cid = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
        can.cid = function(object, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
            if (object._cid) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
                return object._cid
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
                return object._cid = (name || "") + (++cid)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
        can.VERSION = '@EDGE';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
    })();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
    // ## can/util/array/each.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
    var __m6 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
        can.each = function(elements, callback, context) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
            var i = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
                key;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
            if (elements) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
                if (typeof elements.length === 'number' && elements.pop) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
                    if (elements.attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
                        elements.attr('length');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
                    for (key = elements.length; i < key; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
                        if (callback.call(context || elements[i], elements[i], i, elements) === false) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
                            break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
                } else if (elements.hasOwnProperty) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
                    for (key in elements) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
                        if (elements.hasOwnProperty(key)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
                            if (callback.call(context || elements[key], elements[key], key, elements) === false) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
            return elements;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
    })(__m5);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
    // ## can/util/jquery/jquery.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
    var __m3 = (function($, can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
        // _jQuery node list._
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
        $.extend(can, $, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
                trigger: function(obj, event, args) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
                    if (obj.trigger) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
                        obj.trigger(event, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
                        $.event.trigger(event, args, obj, true);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
                addEvent: function(ev, cb) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
                    $([this]).bind(ev, cb);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
                    return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
                removeEvent: function(ev, cb) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
                    $([this]).unbind(ev, cb);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
                    return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
                // jquery caches fragments, we always needs a new one
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
                buildFragment: function(elems, context) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
                    var oldFragment = $.buildFragment,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
                        ret;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
                    elems = [elems];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
                    // Set context per 1.8 logic
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
                    context = context || document;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
                    context = !context.nodeType && context[0] || context;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
                    context = context.ownerDocument || context;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
                    ret = oldFragment.call(jQuery, elems, context);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
                    return ret.cacheable ? $.clone(ret.fragment) : ret.fragment || ret;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
                $: $,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
                each: can.each
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
        // Wrap binding functions.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
        $.each(['bind', 'unbind', 'undelegate', 'delegate'], function(i, func) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
            can[func] = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
                var t = this[func] ? this : $([this]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
                t[func].apply(t, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
                return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
        // Wrap modifier functions.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
        $.each(["append", "filter", "addClass", "remove", "data", "get"], function(i, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   117
            can[name] = function(wrapped) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
                return wrapped[name].apply(wrapped, can.makeArray(arguments).slice(1));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
        // Memory safe destruction.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
        var oldClean = $.cleanData;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
        $.cleanData = function(elems) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
            $.each(elems, function(i, elem) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
                if (elem) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
                    can.trigger(elem, "destroyed", [], false);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
            oldClean(elems);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
    })(jQuery, __m5, __m6);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
    // ## can/util/string/string.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
    var __m2 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   139
        // ##string.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
        // _Miscellaneous string utility functions._  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
        // Several of the methods in this plugin use code adapated from Prototype
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
        // Prototype JavaScript framework, version 1.6.0.1.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
        // © 2005-2007 Sam Stephenson
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
        var strUndHash = /_|-/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   146
            strColons = /\=\=/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   147
            strWords = /([A-Z]+)([A-Z][a-z])/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
            strLowUp = /([a-z\d])([A-Z])/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   149
            strDash = /([a-z\d])([A-Z])/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   150
            strReplacer = /\{([^\}]+)\}/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   151
            strQuote = /"/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   152
            strSingleQuote = /'/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
            // Returns the `prop` property from `obj`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
            // If `add` is true and `prop` doesn't exist in `obj`, create it as an
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
            // empty object.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
            getNext = function(obj, prop, add) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   158
                var result = obj[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   159
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
                if (result === undefined && add === true) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
                    result = obj[prop] = {}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   163
                return result
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
            // Returns `true` if the object can have properties (no `null`s).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
            isContainer = function(current) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
                return (/^f|^o/).test(typeof current);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
        can.extend(can, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
                // Escapes strings for HTML.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
                esc: function(content) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
                    // Convert bad values into empty strings
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
                    var isInvalid = content === null || content === undefined || (isNaN(content) && ("" + content === 'NaN'));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
                    return ("" + (isInvalid ? '' : content))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
                        .replace(/&/g, '&amp;')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
                        .replace(/</g, '&lt;')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
                        .replace(/>/g, '&gt;')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
                        .replace(strQuote, '&#34;')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
                        .replace(strSingleQuote, "&#39;");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
                getObject: function(name, roots, add) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
                    // The parts of the name we are looking up
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
                    // `['App','Models','Recipe']`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
                    var parts = name ? name.split('.') : [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   191
                        length = parts.length,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   192
                        current,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   193
                        r = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   194
                        i, container, rootsLength;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   195
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   196
                    // Make sure roots is an `array`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   197
                    roots = can.isArray(roots) ? roots : [roots || window];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   198
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
                    rootsLength = roots.length
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
                    if (!length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202
                        return roots[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   203
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   204
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   205
                    // For each root, mark it as current.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   206
                    for (r; r < rootsLength; r++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   207
                        current = roots[r];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   208
                        container = undefined;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   209
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   210
                        // Walk current to the 2nd to last object or until there
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   211
                        // is not a container.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   212
                        for (i = 0; i < length && isContainer(current); i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   213
                            container = current;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   214
                            current = getNext(container, parts[i]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   215
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   216
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   217
                        // If we found property break cycle
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   218
                        if (container !== undefined && current !== undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
                            break
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   220
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   221
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   222
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   223
                    // Remove property from found container
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   224
                    if (add === false && current !== undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   225
                        delete container[parts[i - 1]]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   226
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   228
                    // When adding property add it to the first root
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   229
                    if (add === true && current === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   230
                        current = roots[0]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   231
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   232
                        for (i = 0; i < length && isContainer(current); i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   233
                            current = getNext(current, parts[i], true);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   234
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   235
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   236
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
                    return current;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   238
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   239
                // Capitalizes a string.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   240
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
                capitalize: function(s, cache) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
                    // Used to make newId.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
                    return s.charAt(0).toUpperCase() + s.slice(1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   244
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   245
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   246
                // Underscores a string.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   247
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   248
                underscore: function(s) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   249
                    return s
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   250
                        .replace(strColons, '/')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   251
                        .replace(strWords, '$1_$2')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   252
                        .replace(strLowUp, '$1_$2')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   253
                        .replace(strDash, '_')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   254
                        .toLowerCase();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
                // Micro-templating.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   257
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   258
                sub: function(str, data, remove) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   259
                    var obs = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   260
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   261
                    str = str || '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   262
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   263
                    obs.push(str.replace(strReplacer, function(whole, inside) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   264
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   265
                                // Convert inside to type.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   266
                                var ob = can.getObject(inside, data, remove === true ? false : undefined);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   267
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   268
                                if (ob === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   269
                                    obs = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   270
                                    return "";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   271
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   272
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   273
                                // If a container, push into objs (which will return objects found).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   274
                                if (isContainer(ob) && obs) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   275
                                    obs.push(ob);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   276
                                    return "";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   277
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   278
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   279
                                return "" + ob;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   280
                            }));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   281
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   282
                    return obs === null ? obs : (obs.length <= 1 ? obs[0] : obs);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   283
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   284
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   285
                // These regex's are used throughout the rest of can, so let's make
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   286
                // them available.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   287
                replacer: strReplacer,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   288
                undHash: strUndHash
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   289
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   290
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   291
    })(__m3);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   292
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   293
    // ## can/construct/construct.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   294
    var __m1 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   295
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   296
        // ## construct.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   297
        // `can.Construct`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   298
        // _This is a modified version of
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   299
        // [John Resig's class](http://ejohn.org/blog/simple-javascript-inheritance/).  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   300
        // It provides class level inheritance and callbacks._
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   301
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   302
        // A private flag used to initialize a new class instance without
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   303
        // initializing it's bindings.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   304
        var initializing = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   305
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   306
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   307
        can.Construct = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   308
            if (arguments.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   309
                return can.Construct.extend.apply(can.Construct, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   310
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   311
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   312
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   313
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   314
        can.extend(can.Construct, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   315
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   316
                newInstance: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   317
                    // Get a raw instance object (`init` is not called).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   318
                    var inst = this.instance(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   319
                        arg = arguments,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   320
                        args;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   321
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   322
                    // Call `setup` if there is a `setup`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   323
                    if (inst.setup) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   324
                        args = inst.setup.apply(inst, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   325
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   326
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   327
                    // Call `init` if there is an `init`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   328
                    // If `setup` returned `args`, use those as the arguments
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   329
                    if (inst.init) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   330
                        inst.init.apply(inst, args || arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   331
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   332
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   333
                    return inst;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   334
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   335
                // Overwrites an object with methods. Used in the `super` plugin.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   336
                // `newProps` - New properties to add.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   337
                // `oldProps` - Where the old properties might be (used with `super`).  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   338
                // `addTo` - What we are adding to.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   339
                _inherit: function(newProps, oldProps, addTo) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   340
                    can.extend(addTo || newProps, newProps || {})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   341
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   342
                // used for overwriting a single property.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   343
                // this should be used for patching other objects
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   344
                // the super plugin overwrites this
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   345
                _overwrite: function(what, oldProps, propName, val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   346
                    what[propName] = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   347
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   348
                // Set `defaults` as the merger of the parent `defaults` and this 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   349
                // object's `defaults`. If you overwrite this method, make sure to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   350
                // include option merging logic.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   351
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   352
                setup: function(base, fullName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   353
                    this.defaults = can.extend(true, {}, base.defaults, this.defaults);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   354
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   355
                // Create's a new `class` instance without initializing by setting the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   356
                // `initializing` flag.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   357
                instance: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   358
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   359
                    // Prevents running `init`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   360
                    initializing = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   361
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   362
                    var inst = new this();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   363
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   364
                    // Allow running `init`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   365
                    initializing = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   366
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   367
                    return inst;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   368
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   369
                // Extends classes.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   370
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   371
                extend: function(fullName, klass, proto) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   372
                    // Figure out what was passed and normalize it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   373
                    if (typeof fullName != 'string') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   374
                        proto = klass;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   375
                        klass = fullName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   376
                        fullName = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   377
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   378
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   379
                    if (!proto) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   380
                        proto = klass;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   381
                        klass = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   382
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   383
                    proto = proto || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   384
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   385
                    var _super_class = this,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   386
                        _super = this.prototype,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   387
                        name, shortName, namespace, prototype;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   388
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   389
                    // Instantiate a base class (but only create the instance,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   390
                    // don't run the init constructor).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   391
                    prototype = this.instance();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   392
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   393
                    // Copy the properties over onto the new prototype.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   394
                    can.Construct._inherit(proto, _super, prototype);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   395
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   396
                    // The dummy class constructor.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   397
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   398
                    function Constructor() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   399
                        // All construction is actually done in the init method.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   400
                        if (!initializing) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   401
                            return this.constructor !== Constructor && arguments.length ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   402
                            // We are being called without `new` or we are extending.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   403
                            arguments.callee.extend.apply(arguments.callee, arguments) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   404
                            // We are being called with `new`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   405
                            this.constructor.newInstance.apply(this.constructor, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   406
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   407
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   408
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   409
                    // Copy old stuff onto class (can probably be merged w/ inherit)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   410
                    for (name in _super_class) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   411
                        if (_super_class.hasOwnProperty(name)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   412
                            Constructor[name] = _super_class[name];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   413
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   414
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   415
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   416
                    // Copy new static properties on class.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   417
                    can.Construct._inherit(klass, _super_class, Constructor);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   418
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   419
                    // Setup namespaces.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   420
                    if (fullName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   421
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   422
                        var parts = fullName.split('.'),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   423
                            shortName = parts.pop(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   424
                            current = can.getObject(parts.join('.'), window, true),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   425
                            namespace = current,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   426
                            _fullName = can.underscore(fullName.replace(/\./g, "_")),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   427
                            _shortName = can.underscore(shortName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   428
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   429
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   430
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   431
                        current[shortName] = Constructor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   432
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   433
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   434
                    // Set things that shouldn't be overwritten.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   435
                    can.extend(Constructor, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   436
                            constructor: Constructor,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   437
                            prototype: prototype,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   438
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   439
                            namespace: namespace,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   440
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   441
                            _shortName: _shortName,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   442
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   443
                            fullName: fullName,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   444
                            _fullName: _fullName
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   445
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   446
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   447
                    // Dojo and YUI extend undefined
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   448
                    if (shortName !== undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   449
                        Constructor.shortName = shortName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   450
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   451
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   452
                    // Make sure our prototype looks nice.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   453
                    Constructor.prototype.constructor = Constructor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   454
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   455
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   456
                    // Call the class `setup` and `init`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   457
                    var t = [_super_class].concat(can.makeArray(arguments)),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   458
                        args = Constructor.setup.apply(Constructor, t);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   459
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   460
                    if (Constructor.init) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   461
                        Constructor.init.apply(Constructor, args || t);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   462
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   463
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   464
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   465
                    return Constructor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   466
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   467
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   468
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   469
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   470
        return can.Construct;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   471
    })(__m2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   472
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   473
    // ## can/util/bind/bind.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   474
    var __m8 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   475
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   476
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   477
        // ## Bind helpers
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   478
        can.bindAndSetup = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   479
            // Add the event to this object
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   480
            can.addEvent.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   481
            // If not initializing, and the first binding
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   482
            // call bindsetup if the function exists.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   483
            if (!this._init) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   484
                if (!this._bindings) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   485
                    this._bindings = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   486
                    // setup live-binding
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   487
                    this._bindsetup && this._bindsetup();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   488
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   489
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   490
                    this._bindings++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   491
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   492
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   493
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   494
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   495
            return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   496
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   497
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   498
        can.unbindAndTeardown = function(ev, handler) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   499
            // Remove the event handler
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   500
            can.removeEvent.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   501
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   502
            this._bindings--;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   503
            // If there are no longer any bindings and
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   504
            // there is a bindteardown method, call it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   505
            if (!this._bindings) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   506
                this._bindteardown && this._bindteardown();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   507
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   508
            return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   509
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   510
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   511
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   512
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   513
    })(__m3);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   514
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   515
    // ## can/observe/observe.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   516
    var __m7 = (function(can, bind) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   517
        // ## observe.js  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   518
        // `can.Observe`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   519
        // _Provides the observable pattern for JavaScript Objects._  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   520
        // Returns `true` if something is an object with properties of its own.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   521
        var canMakeObserve = function(obj) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   522
            return obj && !can.isDeferred(obj) && (can.isArray(obj) || can.isPlainObject(obj) || (obj instanceof can.Observe));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   523
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   524
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   525
            // Removes all listeners.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   526
            unhookup = function(items, namespace) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   527
                return can.each(items, function(item) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   528
                    if (item && item.unbind) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   529
                        item.unbind("change" + namespace);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   530
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   531
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   532
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   533
            // Listens to changes on `child` and "bubbles" the event up.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   534
            // `child` - The object to listen for changes on.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   535
            // `prop` - The property name is at on.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   536
            // `parent` - The parent object of prop.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   537
            // `ob` - (optional) The Observe object constructor
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   538
            // `list` - (optional) The observable list constructor
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   539
            hookupBubble = function(child, prop, parent, Ob, List) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   540
                Ob = Ob || Observe;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   541
                List = List || Observe.List;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   542
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   543
                // If it's an `array` make a list, otherwise a child.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   544
                if (child instanceof Observe) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   545
                    // We have an `observe` already...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   546
                    // Make sure it is not listening to this already
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   547
                    // It's only listening if it has bindings already.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   548
                    parent._bindings && unhookup([child], parent._cid);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   549
                } else if (can.isArray(child)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   550
                    child = new List(child);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   551
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   552
                    child = new Ob(child);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   553
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   554
                // only listen if something is listening to you
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   555
                if (parent._bindings) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   556
                    // Listen to all changes and `batchTrigger` upwards.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   557
                    bindToChildAndBubbleToParent(child, prop, parent)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   558
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   559
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   560
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   561
                return child;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   562
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   563
            bindToChildAndBubbleToParent = function(child, prop, parent) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   564
                child.bind("change" + parent._cid, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   565
                    // `batchTrigger` the type on this...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   566
                    var args = can.makeArray(arguments),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   567
                        ev = args.shift();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   568
                    args[0] = (prop === "*" ? [parent.indexOf(child), args[0]] : [prop, args[0]]).join(".");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   569
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   570
                    // track objects dispatched on this observe		
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   571
                    ev.triggeredNS = ev.triggeredNS || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   572
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   573
                    // if it has already been dispatched exit
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   574
                    if (ev.triggeredNS[parent._cid]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   575
                        return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   576
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   577
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   578
                    ev.triggeredNS[parent._cid] = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   579
                    // send change event with modified attr to parent	
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   580
                    can.trigger(parent, ev, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   581
                    // send modified attr event to parent
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   582
                    //can.trigger(parent, args[0], args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   583
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   584
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   585
            // An `id` to track events for a given observe.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   586
        observeId = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   587
        // A helper used to serialize an `Observe` or `Observe.List`.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   588
        // `observe` - The observable.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   589
        // `how` - To serialize with `attr` or `serialize`.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   590
        // `where` - To put properties, in an `{}` or `[]`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   591
        serialize = function(observe, how, where) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   592
            // Go through each property.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   593
            observe.each(function(val, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   594
                // If the value is an `object`, and has an `attrs` or `serialize` function.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   595
                where[name] = canMakeObserve(val) && can.isFunction(val[how]) ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   596
                // Call `attrs` or `serialize` to get the original data back.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   597
                val[how]() :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   598
                // Otherwise return the value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   599
                val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   600
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   601
            return where;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   602
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   603
        attrParts = function(attr, keepKey) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   604
            if (keepKey) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   605
                return [attr];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   606
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   607
            return can.isArray(attr) ? attr : ("" + attr).split(".");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   608
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   609
        // Which batch of events this is for -- might not want to send multiple
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   610
        // messages on the same batch.  This is mostly for event delegation.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   611
        batchNum = 1,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   612
        // how many times has start been called without a stop
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   613
        transactions = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   614
        // an array of events within a transaction
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   615
        batchEvents = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   616
        stopCallbacks = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   617
        makeBindSetup = function(wildcard) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   618
            return function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   619
                var parent = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   620
                this._each(function(child, prop) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   621
                    if (child && child.bind) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   622
                        bindToChildAndBubbleToParent(child, wildcard || prop, parent)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   623
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   624
                })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   625
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   626
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   627
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   628
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   629
        var Observe = can.Map = can.Observe = can.Construct({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   630
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   631
                // keep so it can be overwritten
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   632
                bind: can.bindAndSetup,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   633
                unbind: can.unbindAndTeardown,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   634
                id: "id",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   635
                canMakeObserve: canMakeObserve,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   636
                // starts collecting events
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   637
                // takes a callback for after they are updated
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   638
                // how could you hook into after ejs
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   639
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   640
                startBatch: function(batchStopHandler) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   641
                    transactions++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   642
                    batchStopHandler && stopCallbacks.push(batchStopHandler);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   643
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   644
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   645
                stopBatch: function(force, callStart) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   646
                    if (force) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   647
                        transactions = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   648
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   649
                        transactions--;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   650
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   651
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   652
                    if (transactions == 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   653
                        var items = batchEvents.slice(0),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   654
                            callbacks = stopCallbacks.slice(0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   655
                        batchEvents = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   656
                        stopCallbacks = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   657
                        batchNum++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   658
                        callStart && this.startBatch();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   659
                        can.each(items, function(args) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   660
                            can.trigger.apply(can, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   661
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   662
                        can.each(callbacks, function(cb) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   663
                            cb();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   664
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   665
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   666
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   667
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   668
                triggerBatch: function(item, event, args) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   669
                    // Don't send events if initalizing.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   670
                    if (!item._init) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   671
                        if (transactions == 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   672
                            return can.trigger(item, event, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   673
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   674
                            event = typeof event === "string" ? {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   675
                                type: event
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   676
                            } :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   677
                                event;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   678
                            event.batchNum = batchNum;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   679
                            batchEvents.push([
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   680
                                    item,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   681
                                    event,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   682
                                    args
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   683
                                ]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   684
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   685
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   686
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   687
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   688
                keys: function(observe) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   689
                    var keys = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   690
                    Observe.__reading && Observe.__reading(observe, '__keys');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   691
                    for (var keyName in observe._data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   692
                        keys.push(keyName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   693
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   694
                    return keys;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   695
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   696
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   697
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   698
            {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   699
                setup: function(obj) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   700
                    // `_data` is where we keep the properties.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   701
                    this._data = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   702
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   703
                    // The namespace this `object` uses to listen to events.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   704
                    can.cid(this, ".observe");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   705
                    // Sets all `attrs`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   706
                    this._init = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   707
                    this.attr(obj);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   708
                    this.bind('change' + this._cid, can.proxy(this._changes, this));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   709
                    delete this._init;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   710
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   711
                _bindsetup: makeBindSetup(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   712
                _bindteardown: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   713
                    var cid = this._cid;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   714
                    this._each(function(child) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   715
                        unhookup([child], cid)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   716
                    })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   717
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   718
                _changes: function(ev, attr, how, newVal, oldVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   719
                    Observe.triggerBatch(this, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   720
                            type: attr,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   721
                            batchNum: ev.batchNum
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   722
                        }, [newVal, oldVal]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   723
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   724
                _triggerChange: function(attr, how, newVal, oldVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   725
                    Observe.triggerBatch(this, "change", can.makeArray(arguments))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   726
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   727
                // no live binding iterator
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   728
                _each: function(callback) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   729
                    var data = this.__get();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   730
                    for (var prop in data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   731
                        if (data.hasOwnProperty(prop)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   732
                            callback(data[prop], prop)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   733
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   734
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   735
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   736
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   737
                attr: function(attr, val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   738
                    // This is super obfuscated for space -- basically, we're checking
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   739
                    // if the type of the attribute is not a `number` or a `string`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   740
                    var type = typeof attr;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   741
                    if (type !== "string" && type !== "number") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   742
                        return this._attrs(attr, val)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   743
                    } else if (arguments.length === 1) { // If we are getting a value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   744
                        // Let people know we are reading.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   745
                        Observe.__reading && Observe.__reading(this, attr)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   746
                        return this._get(attr)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   747
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   748
                        // Otherwise we are setting.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   749
                        this._set(attr, val);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   750
                        return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   751
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   752
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   753
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   754
                each: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   755
                    Observe.__reading && Observe.__reading(this, '__keys');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   756
                    return can.each.apply(undefined, [this.__get()].concat(can.makeArray(arguments)))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   757
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   758
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   759
                removeAttr: function(attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   760
                    // Info if this is List or not
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   761
                    var isList = this instanceof can.Observe.List,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   762
                        // Convert the `attr` into parts (if nested).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   763
                        parts = attrParts(attr),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   764
                        // The actual property to remove.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   765
                        prop = parts.shift(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   766
                        // The current value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   767
                        current = isList ? this[prop] : this._data[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   768
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   769
                    // If we have more parts, call `removeAttr` on that part.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   770
                    if (parts.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   771
                        return current.removeAttr(parts)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   772
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   773
                        if (isList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   774
                            this.splice(prop, 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   775
                        } else if (prop in this._data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   776
                            // Otherwise, `delete`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   777
                            delete this._data[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   778
                            // Create the event.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   779
                            if (!(prop in this.constructor.prototype)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   780
                                delete this[prop]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   781
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   782
                            // Let others know the number of keys have changed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   783
                            Observe.triggerBatch(this, "__keys");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   784
                            this._triggerChange(prop, "remove", undefined, current);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   785
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   786
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   787
                        return current;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   788
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   789
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   790
                // Reads a property from the `object`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   791
                _get: function(attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   792
                    var value = typeof attr === 'string' && !! ~attr.indexOf('.') && this.__get(attr);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   793
                    if (value) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   794
                        return value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   795
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   796
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   797
                    // break up the attr (`"foo.bar"`) into `["foo","bar"]`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   798
                    var parts = attrParts(attr),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   799
                        // get the value of the first attr name (`"foo"`)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   800
                        current = this.__get(parts.shift());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   801
                    // if there are other attributes to read
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   802
                    return parts.length ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   803
                    // and current has a value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   804
                    current ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   805
                    // lookup the remaining attrs on current
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   806
                    current._get(parts) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   807
                    // or if there's no current, return undefined
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   808
                    undefined :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   809
                    // if there are no more parts, return current
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   810
                    current;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   811
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   812
                // Reads a property directly if an `attr` is provided, otherwise
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   813
                // returns the "real" data object itself.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   814
                __get: function(attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   815
                    return attr ? this._data[attr] : this._data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   816
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   817
                // Sets `attr` prop as value on this object where.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   818
                // `attr` - Is a string of properties or an array  of property values.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   819
                // `value` - The raw value to set.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   820
                _set: function(attr, value, keepKey) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   821
                    // Convert `attr` to attr parts (if it isn't already).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   822
                    var parts = attrParts(attr, keepKey),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   823
                        // The immediate prop we are setting.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   824
                        prop = parts.shift(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   825
                        // The current value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   826
                        current = this.__get(prop);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   827
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   828
                    // If we have an `object` and remaining parts.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   829
                    if (canMakeObserve(current) && parts.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   830
                        // That `object` should set it (this might need to call attr).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   831
                        current._set(parts, value)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   832
                    } else if (!parts.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   833
                        // We're in "real" set territory.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   834
                        if (this.__convert) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   835
                            value = this.__convert(prop, value)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   836
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   837
                        this.__set(prop, value, current)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   838
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   839
                        throw "can.Observe: Object does not exist"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   840
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   841
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   842
                __set: function(prop, value, current) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   843
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   844
                    // Otherwise, we are setting it on this `object`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   845
                    // TODO: Check if value is object and transform
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   846
                    // are we changing the value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   847
                    if (value !== current) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   848
                        // Check if we are adding this for the first time --
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   849
                        // if we are, we need to create an `add` event.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   850
                        var changeType = this.__get().hasOwnProperty(prop) ? "set" : "add";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   851
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   852
                        // Set the value on data.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   853
                        this.___set(prop,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   854
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   855
                            // If we are getting an object.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   856
                            canMakeObserve(value) ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   857
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   858
                            // Hook it up to send event.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   859
                            hookupBubble(value, prop, this) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   860
                            // Value is normal.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   861
                            value);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   862
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   863
                        if (changeType == "add") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   864
                            // If there is no current value, let others know that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   865
                            // the the number of keys have changed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   866
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   867
                            Observe.triggerBatch(this, "__keys", undefined);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   868
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   869
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   870
                        // `batchTrigger` the change event.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   871
                        this._triggerChange(prop, changeType, value, current);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   872
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   873
                        //Observe.triggerBatch(this, prop, [value, current]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   874
                        // If we can stop listening to our old value, do it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   875
                        current && unhookup([current], this._cid);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   876
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   877
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   878
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   879
                // Directly sets a property on this `object`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   880
                ___set: function(prop, val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   881
                    this._data[prop] = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   882
                    // Add property directly for easy writing.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   883
                    // Check if its on the `prototype` so we don't overwrite methods like `attrs`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   884
                    if (!(prop in this.constructor.prototype)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   885
                        this[prop] = val
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   886
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   887
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   888
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   889
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   890
                bind: can.bindAndSetup,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   891
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   892
                unbind: can.unbindAndTeardown,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   893
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   894
                serialize: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   895
                    return serialize(this, 'serialize', {});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   896
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   897
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   898
                _attrs: function(props, remove) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   899
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   900
                    if (props === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   901
                        return serialize(this, 'attr', {})
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   902
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   903
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   904
                    props = can.extend({}, props);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   905
                    var prop,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   906
                        self = this,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   907
                        newVal;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   908
                    Observe.startBatch();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   909
                    this.each(function(curVal, prop) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   910
                        newVal = props[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   911
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   912
                        // If we are merging...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   913
                        if (newVal === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   914
                            remove && self.removeAttr(prop);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   915
                            return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   916
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   917
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   918
                        if (self.__convert) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   919
                            newVal = self.__convert(prop, newVal)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   920
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   921
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   922
                        // if we're dealing with models, want to call _set to let converter run
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   923
                        if (newVal instanceof can.Observe) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   924
                            self.__set(prop, newVal, curVal)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   925
                            // if its an object, let attr merge
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   926
                        } else if (canMakeObserve(curVal) && canMakeObserve(newVal) && curVal.attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   927
                            curVal.attr(newVal, remove)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   928
                            // otherwise just set
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   929
                        } else if (curVal != newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   930
                            self.__set(prop, newVal, curVal)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   931
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   932
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   933
                        delete props[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   934
                    })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   935
                    // Add remaining props.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   936
                    for (var prop in props) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   937
                        newVal = props[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   938
                        this._set(prop, newVal, true)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   939
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   940
                    Observe.stopBatch()
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   941
                    return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   942
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   943
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   944
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   945
                compute: function(prop) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   946
                    return can.compute(this, prop);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   947
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   948
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   949
        // Helpers for `observable` lists.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   950
        var splice = [].splice,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   951
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   952
            list = Observe(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   953
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   954
                {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   955
                    setup: function(instances, options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   956
                        this.length = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   957
                        can.cid(this, ".observe")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   958
                        this._init = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   959
                        if (can.isDeferred(instances)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   960
                            this.replace(instances)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   961
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   962
                            this.push.apply(this, can.makeArray(instances || []));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   963
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   964
                        // this change needs to be ignored
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   965
                        this.bind('change' + this._cid, can.proxy(this._changes, this));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   966
                        can.extend(this, options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   967
                        delete this._init;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   968
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   969
                    _triggerChange: function(attr, how, newVal, oldVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   970
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   971
                        Observe.prototype._triggerChange.apply(this, arguments)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   972
                        // `batchTrigger` direct add and remove events...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   973
                        if (!~attr.indexOf('.')) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   974
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   975
                            if (how === 'add') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   976
                                Observe.triggerBatch(this, how, [newVal, +attr]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   977
                                Observe.triggerBatch(this, 'length', [this.length]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   978
                            } else if (how === 'remove') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   979
                                Observe.triggerBatch(this, how, [oldVal, +attr]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   980
                                Observe.triggerBatch(this, 'length', [this.length]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   981
                            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   982
                                Observe.triggerBatch(this, how, [newVal, +attr])
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   983
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   984
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   985
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   986
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   987
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   988
                    __get: function(attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   989
                        return attr ? this[attr] : this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   990
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   991
                    ___set: function(attr, val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   992
                        this[attr] = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   993
                        if (+attr >= this.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   994
                            this.length = (+attr + 1)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   995
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   996
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   997
                    _each: function(callback) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   998
                        var data = this.__get();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   999
                        for (var i = 0; i < data.length; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1000
                            callback(data[i], i)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1001
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1002
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1003
                    _bindsetup: makeBindSetup("*"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1004
                    // Returns the serialized form of this list.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1005
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1006
                    serialize: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1007
                        return serialize(this, 'serialize', []);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1008
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1009
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1010
                    splice: function(index, howMany) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1011
                        var args = can.makeArray(arguments),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1012
                            i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1013
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1014
                        for (i = 2; i < args.length; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1015
                            var val = args[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1016
                            if (canMakeObserve(val)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1017
                                args[i] = hookupBubble(val, "*", this, this.constructor.Observe, this.constructor)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1018
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1019
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1020
                        if (howMany === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1021
                            howMany = args[1] = this.length - index;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1022
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1023
                        var removed = splice.apply(this, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1024
                        can.Observe.startBatch();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1025
                        if (howMany > 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1026
                            this._triggerChange("" + index, "remove", undefined, removed);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1027
                            unhookup(removed, this._cid);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1028
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1029
                        if (args.length > 2) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1030
                            this._triggerChange("" + index, "add", args.slice(2), removed);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1031
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1032
                        can.Observe.stopBatch();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1033
                        return removed;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1034
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1035
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1036
                    _attrs: function(items, remove) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1037
                        if (items === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1038
                            return serialize(this, 'attr', []);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1039
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1040
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1041
                        // Create a copy.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1042
                        items = can.makeArray(items);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1043
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1044
                        Observe.startBatch();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1045
                        this._updateAttrs(items, remove);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1046
                        Observe.stopBatch()
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1047
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1048
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1049
                    _updateAttrs: function(items, remove) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1050
                        var len = Math.min(items.length, this.length);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1051
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1052
                        for (var prop = 0; prop < len; prop++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1053
                            var curVal = this[prop],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1054
                                newVal = items[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1055
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1056
                            if (canMakeObserve(curVal) && canMakeObserve(newVal)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1057
                                curVal.attr(newVal, remove)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1058
                            } else if (curVal != newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1059
                                this._set(prop, newVal)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1060
                            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1061
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1062
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1063
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1064
                        if (items.length > this.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1065
                            // Add in the remaining props.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1066
                            this.push.apply(this, items.slice(this.length));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1067
                        } else if (items.length < this.length && remove) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1068
                            this.splice(items.length)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1069
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1070
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1071
                }),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1072
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1073
            // Converts to an `array` of arguments.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1074
            getArgs = function(args) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1075
                return args[0] && can.isArray(args[0]) ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1076
                    args[0] :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1077
                    can.makeArray(args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1078
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1079
        // Create `push`, `pop`, `shift`, and `unshift`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1080
        can.each({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1081
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1082
                push: "length",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1083
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1084
                unshift: 0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1085
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1086
            // Adds a method
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1087
            // `name` - The method name.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1088
            // `where` - Where items in the `array` should be added.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1089
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1090
            function(where, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1091
                var orig = [][name]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1092
                list.prototype[name] = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1093
                    // Get the items being added.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1094
                    var args = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1095
                        // Where we are going to add items.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1096
                        len = where ? this.length : 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1097
                        i = arguments.length,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1098
                        res,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1099
                        val,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1100
                        constructor = this.constructor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1101
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1102
                    // Go through and convert anything to an `observe` that needs to be converted.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1103
                    while (i--) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1104
                        val = arguments[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1105
                        args[i] = canMakeObserve(val) ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1106
                            hookupBubble(val, "*", this, this.constructor.Observe, this.constructor) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1107
                            val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1108
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1109
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1110
                    // Call the original method.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1111
                    res = orig.apply(this, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1112
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1113
                    if (!this.comparator || args.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1114
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1115
                        this._triggerChange("" + len, "add", args, undefined);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1116
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1117
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1118
                    return res;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1119
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1120
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1121
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1122
        can.each({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1123
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1124
                pop: "length",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1125
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1126
                shift: 0
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1127
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1128
            // Creates a `remove` type method
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1129
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1130
            function(where, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1131
                list.prototype[name] = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1132
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1133
                    var args = getArgs(arguments),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1134
                        len = where && this.length ? this.length - 1 : 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1135
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1136
                    var res = [][name].apply(this, args)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1137
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1138
                    // Create a change where the args are
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1139
                    // `len` - Where these items were removed.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1140
                    // `remove` - Items removed.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1141
                    // `undefined` - The new values (there are none).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1142
                    // `res` - The old, removed values (should these be unbound).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1143
                    this._triggerChange("" + len, "remove", undefined, [res])
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1144
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1145
                    if (res && res.unbind) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1146
                        res.unbind("change" + this._cid)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1147
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1148
                    return res;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1149
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1150
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1151
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1152
        can.extend(list.prototype, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1153
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1154
                indexOf: function(item) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1155
                    this.attr('length')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1156
                    return can.inArray(item, this)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1157
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1158
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1159
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1160
                join: [].join,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1161
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1162
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1163
                reverse: [].reverse,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1164
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1165
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1166
                slice: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1167
                    var temp = Array.prototype.slice.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1168
                    return new this.constructor(temp);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1169
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1170
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1171
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1172
                concat: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1173
                    var args = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1174
                    can.each(can.makeArray(arguments), function(arg, i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1175
                        args[i] = arg instanceof can.Observe.List ? arg.serialize() : arg;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1176
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1177
                    return new this.constructor(Array.prototype.concat.apply(this.serialize(), args));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1178
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1179
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1180
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1181
                forEach: function(cb, thisarg) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1182
                    can.each(this, cb, thisarg || this);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1183
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1184
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1185
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1186
                replace: function(newList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1187
                    if (can.isDeferred(newList)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1188
                        newList.then(can.proxy(this.replace, this));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1189
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1190
                        this.splice.apply(this, [0, this.length].concat(can.makeArray(newList || [])));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1191
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1192
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1193
                    return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1194
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1195
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1196
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1197
        can.List = Observe.List = list;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1198
        Observe.setup = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1199
            can.Construct.setup.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1200
            // I would prefer not to do it this way. It should
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1201
            // be using the attributes plugin to do this type of conversion.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1202
            this.List = Observe.List({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1203
                    Observe: this
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1204
                }, {});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1205
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1206
        return Observe;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1207
    })(__m3, __m8, __m1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1208
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1209
    // ## can/observe/compute/compute.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1210
    var __m9 = (function(can, bind) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1211
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1212
        // returns the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1213
        // - observes and attr methods are called by func
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1214
        // - the value returned by func
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1215
        // ex: `{value: 100, observed: [{obs: o, attr: "completed"}]}`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1216
        var getValueAndObserved = function(func, self) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1217
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1218
            var oldReading;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1219
            if (can.Observe) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1220
                // Set a callback on can.Observe to know
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1221
                // when an attr is read.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1222
                // Keep a reference to the old reader
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1223
                // if there is one.  This is used
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1224
                // for nested live binding.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1225
                oldReading = can.Observe.__reading;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1226
                can.Observe.__reading = function(obj, attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1227
                    // Add the observe and attr that was read
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1228
                    // to `observed`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1229
                    observed.push({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1230
                            obj: obj,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1231
                            attr: attr + ""
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1232
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1233
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1234
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1235
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1236
            var observed = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1237
                // Call the "wrapping" function to get the value. `observed`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1238
                // will have the observe/attribute pairs that were read.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1239
                value = func.call(self);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1240
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1241
            // Set back so we are no longer reading.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1242
            if (can.Observe) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1243
                can.Observe.__reading = oldReading;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1244
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1245
            return {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1246
                value: value,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1247
                observed: observed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1248
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1249
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1250
            // Calls `callback(newVal, oldVal)` everytime an observed property
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1251
            // called within `getterSetter` is changed and creates a new result of `getterSetter`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1252
            // Also returns an object that can teardown all event handlers.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1253
            computeBinder = function(getterSetter, context, callback, computeState) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1254
                // track what we are observing
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1255
                var observing = {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1256
                    // a flag indicating if this observe/attr pair is already bound
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1257
                    matched = true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1258
                    // the data to return 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1259
                    data = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1260
                        // we will maintain the value while live-binding is taking place
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1261
                        value: undefined,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1262
                        // a teardown method that stops listening
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1263
                        teardown: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1264
                            for (var name in observing) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1265
                                var ob = observing[name];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1266
                                ob.observe.obj.unbind(ob.observe.attr, onchanged);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1267
                                delete observing[name];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1268
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1269
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1270
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1271
                    batchNum;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1272
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1273
                // when a property value is changed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1274
                var onchanged = function(ev) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1275
                    // If the compute is no longer bound (because the same change event led to an unbind)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1276
                    // then do not call getValueAndBind, or we will leak bindings.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1277
                    if (computeState && !computeState.bound) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1278
                        return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1279
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1280
                    if (ev.batchNum === undefined || ev.batchNum !== batchNum) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1281
                        // store the old value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1282
                        var oldValue = data.value,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1283
                            // get the new value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1284
                            newvalue = getValueAndBind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1285
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1286
                        // update the value reference (in case someone reads)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1287
                        data.value = newvalue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1288
                        // if a change happened
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1289
                        if (newvalue !== oldValue) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1290
                            callback(newvalue, oldValue);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1291
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1292
                        batchNum = batchNum = ev.batchNum;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1293
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1294
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1295
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1296
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1297
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1298
                // gets the value returned by `getterSetter` and also binds to any attributes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1299
                // read by the call
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1300
                var getValueAndBind = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1301
                    var info = getValueAndObserved(getterSetter, context),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1302
                        newObserveSet = info.observed;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1303
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1304
                    var value = info.value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1305
                    matched = !matched;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1306
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1307
                    // go through every attribute read by this observe
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1308
                    can.each(newObserveSet, function(ob) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1309
                        // if the observe/attribute pair is being observed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1310
                        if (observing[ob.obj._cid + "|" + ob.attr]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1311
                            // mark at as observed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1312
                            observing[ob.obj._cid + "|" + ob.attr].matched = matched;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1313
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1314
                            // otherwise, set the observe/attribute on oldObserved, marking it as being observed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1315
                            observing[ob.obj._cid + "|" + ob.attr] = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1316
                                matched: matched,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1317
                                observe: ob
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1318
                            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1319
                            ob.obj.bind(ob.attr, onchanged);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1320
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1321
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1322
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1323
                    // Iterate through oldObserved, looking for observe/attributes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1324
                    // that are no longer being bound and unbind them
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1325
                    for (var name in observing) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1326
                        var ob = observing[name];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1327
                        if (ob.matched !== matched) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1328
                            ob.observe.obj.unbind(ob.observe.attr, onchanged);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1329
                            delete observing[name];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1330
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1331
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1332
                    return value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1333
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1334
                // set the initial value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1335
                data.value = getValueAndBind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1336
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1337
                data.isListening = !can.isEmptyObject(observing);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1338
                return data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1339
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1340
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1341
            // if no one is listening ... we can not calculate every time
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1342
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1343
        can.compute = function(getterSetter, context, eventName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1344
            if (getterSetter && getterSetter.isComputed) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1345
                return getterSetter;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1346
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1347
            // stores the result of computeBinder
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1348
            var computedData,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1349
                // how many listeners to this this compute
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1350
                bindings = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1351
                // the computed object
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1352
                computed,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1353
                // an object that keeps track if the computed is bound
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1354
                // onchanged needs to know this. It's possible a change happens and results in
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1355
                // something that unbinds the compute, it needs to not to try to recalculate who it
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1356
                // is listening to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1357
                computeState = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1358
                    bound: false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1359
                    // true if this compute is calculated from other computes and observes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1360
                    hasDependencies: false
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1361
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1362
                // The following functions are overwritten depending on how compute() is called
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1363
                // a method to setup listening
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1364
                on = function() {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1365
                // a method to teardown listening
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1366
                off = function() {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1367
                // the current cached value (only valid if bound = true)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1368
                value,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1369
                // how to read the value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1370
                get = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1371
                    return value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1372
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1373
                // sets the value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1374
                set = function(newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1375
                    value = newVal;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1376
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1377
                // this compute can be a dependency of other computes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1378
                canReadForChangeEvent = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1379
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1380
            computed = function(newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1381
                // setting ...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1382
                if (arguments.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1383
                    // save a reference to the old value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1384
                    var old = value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1385
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1386
                    // setter may return a value if 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1387
                    // setter is for a value maintained exclusively by this compute
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1388
                    var setVal = set.call(context, newVal, old);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1389
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1390
                    // if this has dependencies return the current value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1391
                    if (computed.hasDependencies) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1392
                        return get.call(context);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1393
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1394
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1395
                    if (setVal === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1396
                        // it's possible, like with the DOM, setting does not
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1397
                        // fire a change event, so we must read
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1398
                        value = get.call(context);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1399
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1400
                        value = setVal;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1401
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1402
                    // fire the change
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1403
                    if (old !== value) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1404
                        can.Observe.triggerBatch(computed, "change", [value, old]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1405
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1406
                    return value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1407
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1408
                    // Let others know to listen to changes in this compute
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1409
                    if (can.Observe.__reading && canReadForChangeEvent) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1410
                        can.Observe.__reading(computed, 'change');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1411
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1412
                    // if we are bound, use the cached value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1413
                    if (computeState.bound) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1414
                        return value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1415
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1416
                        return get.call(context);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1417
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1418
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1419
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1420
            if (typeof getterSetter === "function") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1421
                set = getterSetter;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1422
                get = getterSetter;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1423
                canReadForChangeEvent = eventName === false ? false : true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1424
                computed.hasDependencies = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1425
                on = function(update) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1426
                    computedData = computeBinder(getterSetter, context || this, update, computeState);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1427
                    computed.hasDependencies = computedData.isListening
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1428
                    value = computedData.value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1429
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1430
                off = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1431
                    computedData && computedData.teardown();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1432
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1433
            } else if (context) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1434
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1435
                if (typeof context == "string") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1436
                    // `can.compute(obj, "propertyName", [eventName])`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1437
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1438
                    var propertyName = context,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1439
                        isObserve = getterSetter instanceof can.Observe;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1440
                    if (isObserve) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1441
                        computed.hasDependencies = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1442
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1443
                    get = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1444
                        if (isObserve) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1445
                            return getterSetter.attr(propertyName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1446
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1447
                            return getterSetter[propertyName];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1448
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1449
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1450
                    set = function(newValue) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1451
                        if (isObserve) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1452
                            getterSetter.attr(propertyName, newValue)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1453
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1454
                            getterSetter[propertyName] = newValue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1455
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1456
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1457
                    var handler;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1458
                    on = function(update) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1459
                        handler = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1460
                            update(get(), value)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1461
                        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1462
                        can.bind.call(getterSetter, eventName || propertyName, handler)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1463
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1464
                        // use getValueAndObserved because
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1465
                        // we should not be indicating that some parent
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1466
                        // reads this property if it happens to be binding on it
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1467
                        value = getValueAndObserved(get).value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1468
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1469
                    off = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1470
                        can.unbind.call(getterSetter, eventName || propertyName, handler)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1471
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1472
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1473
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1474
                    // `can.compute(initialValue, setter)`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1475
                    if (typeof context === "function") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1476
                        value = getterSetter;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1477
                        set = context;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1478
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1479
                        // `can.compute(initialValue,{get:, set:, on:, off:})`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1480
                        value = getterSetter;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1481
                        var options = context;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1482
                        get = options.get || get;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1483
                        set = options.set || set;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1484
                        on = options.on || on;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1485
                        off = options.off || off;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1486
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1487
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1488
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1489
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1490
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1491
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1492
            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1493
                // `can.compute(5)`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1494
                value = getterSetter;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1495
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1496
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1497
            computed.isComputed = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1498
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1499
            can.cid(computed, "compute")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1500
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1501
            var updater = function(newValue, oldValue) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1502
                value = newValue;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1503
                // might need a way to look up new and oldVal
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1504
                can.Observe.triggerBatch(computed, "change", [newValue, oldValue])
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1505
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1506
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1507
            return can.extend(computed, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1508
                    _bindsetup: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1509
                        computeState.bound = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1510
                        // setup live-binding
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1511
                        on.call(this, updater)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1512
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1513
                    _bindteardown: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1514
                        off.call(this, updater)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1515
                        computeState.bound = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1516
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1517
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1518
                    bind: can.bindAndSetup,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1519
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1520
                    unbind: can.unbindAndTeardown
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1521
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1522
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1523
        can.compute.binder = computeBinder;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1524
        return can.compute;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1525
    })(__m3, __m8);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1526
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1527
    // ## can/model/model.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1528
    var __m10 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1529
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1530
        // ## model.js  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1531
        // `can.Model`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1532
        // _A `can.Observe` that connects to a RESTful interface._
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1533
        // Generic deferred piping function
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1534
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1535
        var pipe = function(def, model, func) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1536
            var d = new can.Deferred();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1537
            def.then(function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1538
                var args = can.makeArray(arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1539
                args[0] = model[func](args[0]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1540
                d.resolveWith(d, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1541
            }, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1542
                d.rejectWith(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1543
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1544
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1545
            if (typeof def.abort === 'function') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1546
                d.abort = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1547
                    return def.abort();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1548
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1549
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1550
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1551
            return d;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1552
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1553
            modelNum = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1554
            ignoreHookup = /change.observe\d+/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1555
            getId = function(inst) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1556
                // Instead of using attr, use __get for performance.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1557
                // Need to set reading
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1558
                can.Observe.__reading && can.Observe.__reading(inst, inst.constructor.id)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1559
                return inst.__get(inst.constructor.id);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1560
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1561
            // Ajax `options` generator function
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1562
            ajax = function(ajaxOb, data, type, dataType, success, error) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1563
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1564
                var params = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1565
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1566
                // If we get a string, handle it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1567
                if (typeof ajaxOb == "string") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1568
                    // If there's a space, it's probably the type.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1569
                    var parts = ajaxOb.split(/\s+/);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1570
                    params.url = parts.pop();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1571
                    if (parts.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1572
                        params.type = parts.pop();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1573
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1574
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1575
                    can.extend(params, ajaxOb);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1576
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1577
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1578
                // If we are a non-array object, copy to a new attrs.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1579
                params.data = typeof data == "object" && !can.isArray(data) ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1580
                    can.extend(params.data || {}, data) : data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1581
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1582
                // Get the url with any templated values filled out.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1583
                params.url = can.sub(params.url, params.data, true);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1584
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1585
                return can.ajax(can.extend({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1586
                            type: type || "post",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1587
                            dataType: dataType || "json",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1588
                            success: success,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1589
                            error: error
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1590
                        }, params));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1591
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1592
            makeRequest = function(self, type, success, error, method) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1593
                var args;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1594
                // if we pass an array as `self` it it means we are coming from
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1595
                // the queued request, and we're passing already serialized data
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1596
                // self's signature will be: [self, serializedData]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1597
                if (can.isArray(self)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1598
                    args = self[1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1599
                    self = self[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1600
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1601
                    args = self.serialize();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1602
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1603
                args = [args];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1604
                var deferred,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1605
                    // The model.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1606
                    model = self.constructor,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1607
                    jqXHR;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1608
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1609
                // `destroy` does not need data.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1610
                if (type == 'destroy') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1611
                    args.shift();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1612
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1613
                // `update` and `destroy` need the `id`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1614
                if (type !== 'create') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1615
                    args.unshift(getId(self));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1616
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1617
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1618
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1619
                jqXHR = model[type].apply(model, args);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1620
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1621
                deferred = jqXHR.pipe(function(data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1622
                    self[method || type + "d"](data, jqXHR);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1623
                    return self;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1624
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1625
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1626
                // Hook up `abort`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1627
                if (jqXHR.abort) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1628
                    deferred.abort = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1629
                        jqXHR.abort();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1630
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1631
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1632
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1633
                deferred.then(success, error);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1634
                return deferred;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1635
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1636
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1637
            // This object describes how to make an ajax request for each ajax method.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1638
            // The available properties are:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1639
            //		`url` - The default url to use as indicated as a property on the model.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1640
            //		`type` - The default http request type
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1641
            //		`data` - A method that takes the `arguments` and returns `data` used for ajax.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1642
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1643
            ajaxMethods = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1644
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1645
                create: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1646
                    url: "_shortName",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1647
                    type: "post"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1648
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1649
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1650
                update: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1651
                    data: function(id, attrs) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1652
                        attrs = attrs || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1653
                        var identity = this.id;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1654
                        if (attrs[identity] && attrs[identity] !== id) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1655
                            attrs["new" + can.capitalize(id)] = attrs[identity];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1656
                            delete attrs[identity];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1657
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1658
                        attrs[identity] = id;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1659
                        return attrs;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1660
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1661
                    type: "put"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1662
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1663
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1664
                destroy: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1665
                    type: "delete",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1666
                    data: function(id) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1667
                        var args = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1668
                        args.id = args[this.id] = id;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1669
                        return args;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1670
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1671
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1672
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1673
                findAll: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1674
                    url: "_shortName"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1675
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1676
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1677
                findOne: {}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1678
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1679
            // Makes an ajax request `function` from a string.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1680
            //		`ajaxMethod` - The `ajaxMethod` object defined above.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1681
            //		`str` - The string the user provided. Ex: `findAll: "/recipes.json"`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1682
            ajaxMaker = function(ajaxMethod, str) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1683
                // Return a `function` that serves as the ajax method.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1684
                return function(data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1685
                    // If the ajax method has it's own way of getting `data`, use that.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1686
                    data = ajaxMethod.data ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1687
                        ajaxMethod.data.apply(this, arguments) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1688
                    // Otherwise use the data passed in.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1689
                    data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1690
                    // Return the ajax method with `data` and the `type` provided.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1691
                    return ajax(str || this[ajaxMethod.url || "_url"], data, ajaxMethod.type || "get")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1692
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1693
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1694
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1695
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1696
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1697
        can.Model = can.Observe({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1698
                fullName: "can.Model",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1699
                _reqs: 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1700
                setup: function(base) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1701
                    // create store here if someone wants to use model without inheriting from it
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1702
                    this.store = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1703
                    can.Observe.setup.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1704
                    // Set default list as model list
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1705
                    if (!can.Model) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1706
                        return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1707
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1708
                    this.List = ML({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1709
                            Observe: this
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1710
                        }, {});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1711
                    var self = this,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1712
                        clean = can.proxy(this._clean, self);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1713
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1714
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1715
                    // go through ajax methods and set them up
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1716
                    can.each(ajaxMethods, function(method, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1717
                        // if an ajax method is not a function, it's either
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1718
                        // a string url like findAll: "/recipes" or an
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1719
                        // ajax options object like {url: "/recipes"}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1720
                        if (!can.isFunction(self[name])) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1721
                            // use ajaxMaker to convert that into a function
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1722
                            // that returns a deferred with the data
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1723
                            self[name] = ajaxMaker(method, self[name]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1724
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1725
                        // check if there's a make function like makeFindAll
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1726
                        // these take deferred function and can do special
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1727
                        // behavior with it (like look up data in a store)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1728
                        if (self["make" + can.capitalize(name)]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1729
                            // pass the deferred method to the make method to get back
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1730
                            // the "findAll" method.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1731
                            var newMethod = self["make" + can.capitalize(name)](self[name]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1732
                            can.Construct._overwrite(self, base, name, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1733
                                // increment the numer of requests
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1734
                                can.Model._reqs++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1735
                                var def = newMethod.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1736
                                var then = def.then(clean, clean);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1737
                                then.abort = def.abort;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1738
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1739
                                // attach abort to our then and return it
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1740
                                return then;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1741
                            })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1742
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1743
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1744
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1745
                    if (self.fullName == "can.Model" || !self.fullName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1746
                        self.fullName = "Model" + (++modelNum);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1747
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1748
                    // Add ajax converters.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1749
                    can.Model._reqs = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1750
                    this._url = this._shortName + "/{" + this.id + "}"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1751
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1752
                _ajax: ajaxMaker,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1753
                _makeRequest: makeRequest,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1754
                _clean: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1755
                    can.Model._reqs--;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1756
                    if (!can.Model._reqs) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1757
                        for (var id in this.store) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1758
                            if (!this.store[id]._bindings) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1759
                                delete this.store[id];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1760
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1761
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1762
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1763
                    return arguments[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1764
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1765
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1766
                models: function(instancesRawData, oldList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1767
                    // until "end of turn", increment reqs counter so instances will be added to the store
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1768
                    can.Model._reqs++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1769
                    if (!instancesRawData) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1770
                        return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1771
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1772
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1773
                    if (instancesRawData instanceof this.List) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1774
                        return instancesRawData;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1775
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1776
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1777
                    // Get the list type.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1778
                    var self = this,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1779
                        tmp = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1780
                        res = oldList instanceof can.Observe.List ? oldList : new(self.List || ML),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1781
                        // Did we get an `array`?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1782
                        arr = can.isArray(instancesRawData),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1783
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1784
                        // Did we get a model list?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1785
                        ml = (instancesRawData instanceof ML),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1786
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1787
                        // Get the raw `array` of objects.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1788
                        raw = arr ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1789
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1790
                        // If an `array`, return the `array`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1791
                        instancesRawData :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1792
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1793
                        // Otherwise if a model list.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1794
                        (ml ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1795
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1796
                            // Get the raw objects from the list.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1797
                            instancesRawData.serialize() :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1798
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1799
                            // Get the object's data.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1800
                            instancesRawData.data),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1801
                        i = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1802
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1803
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1804
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1805
                    if (res.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1806
                        res.splice(0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1807
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1808
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1809
                    can.each(raw, function(rawPart) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1810
                        tmp.push(self.model(rawPart));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1811
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1812
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1813
                    // We only want one change event so push everything at once
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1814
                    res.push.apply(res, tmp);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1815
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1816
                    if (!arr) { // Push other stuff onto `array`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1817
                        can.each(instancesRawData, function(val, prop) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1818
                            if (prop !== 'data') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1819
                                res.attr(prop, val);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1820
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1821
                        })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1822
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1823
                    // at "end of turn", clean up the store
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1824
                    setTimeout(can.proxy(this._clean, this), 1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1825
                    return res;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1826
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1827
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1828
                model: function(attributes) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1829
                    if (!attributes) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1830
                        return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1831
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1832
                    if (attributes instanceof this) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1833
                        attributes = attributes.serialize();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1834
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1835
                    var id = attributes[this.id],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1836
                        model = (id || id === 0) && this.store[id] ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1837
                            this.store[id].attr(attributes, this.removeAttr || false) : new this(attributes);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1838
                    if (can.Model._reqs) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1839
                        this.store[attributes[this.id]] = model;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1840
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1841
                    return model;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1842
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1843
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1844
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1845
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1846
            {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1847
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1848
                isNew: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1849
                    var id = getId(this);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1850
                    return !(id || id === 0); // If `null` or `undefined`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1851
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1852
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1853
                save: function(success, error) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1854
                    return makeRequest(this, this.isNew() ? 'create' : 'update', success, error);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1855
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1856
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1857
                destroy: function(success, error) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1858
                    if (this.isNew()) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1859
                        var self = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1860
                        var def = can.Deferred();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1861
                        def.then(success, error);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1862
                        return def.done(function(data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1863
                            self.destroyed(data)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1864
                        }).resolve(self);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1865
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1866
                    return makeRequest(this, 'destroy', success, error, 'destroyed');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1867
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1868
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1869
                _bindsetup: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1870
                    this.constructor.store[this.__get(this.constructor.id)] = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1871
                    return can.Observe.prototype._bindsetup.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1872
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1873
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1874
                _bindteardown: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1875
                    delete this.constructor.store[getId(this)];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1876
                    return can.Observe.prototype._bindteardown.apply(this, arguments)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1877
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1878
                // Change `id`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1879
                ___set: function(prop, val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1880
                    can.Observe.prototype.___set.call(this, prop, val)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1881
                    // If we add an `id`, move it to the store.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1882
                    if (prop === this.constructor.id && this._bindings) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1883
                        this.constructor.store[getId(this)] = this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1884
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1885
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1886
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1887
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1888
        can.each({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1889
                makeFindAll: "models",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1890
                makeFindOne: "model",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1891
                makeCreate: "model",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1892
                makeUpdate: "model"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1893
            }, function(method, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1894
                can.Model[name] = function(oldMethod) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1895
                    return function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1896
                        var args = can.makeArray(arguments),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1897
                            oldArgs = can.isFunction(args[1]) ? args.splice(0, 1) : args.splice(0, 2),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1898
                            def = pipe(oldMethod.apply(this, oldArgs), this, method);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1899
                        def.then(args[0], args[1]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1900
                        // return the original promise
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1901
                        return def;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1902
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1903
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1904
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1905
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1906
        can.each([
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1907
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1908
                "created",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1909
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1910
                "updated",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1911
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1912
                "destroyed"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1913
            ], function(funcName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1914
                can.Model.prototype[funcName] = function(attrs) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1915
                    var stub,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1916
                        constructor = this.constructor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1917
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1918
                    // Update attributes if attributes have been passed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1919
                    stub = attrs && typeof attrs == 'object' && this.attr(attrs.attr ? attrs.attr() : attrs);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1920
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1921
                    // triggers change event that bubble's like
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1922
                    // handler( 'change','1.destroyed' ). This is used
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1923
                    // to remove items on destroyed from Model Lists.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1924
                    // but there should be a better way.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1925
                    can.trigger(this, "change", funcName)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1926
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1927
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1928
                    // Call event on the instance's Class
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1929
                    can.trigger(constructor, funcName, this);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1930
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1931
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1932
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1933
        // Model lists are just like `Observe.List` except that when their items are 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1934
        // destroyed, it automatically gets removed from the list.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1935
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1936
        var ML = can.Model.List = can.Observe.List({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1937
                setup: function(params) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1938
                    if (can.isPlainObject(params) && !can.isArray(params)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1939
                        can.Observe.List.prototype.setup.apply(this);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1940
                        this.replace(this.constructor.Observe.findAll(params))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1941
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1942
                        can.Observe.List.prototype.setup.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1943
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1944
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1945
                _changes: function(ev, attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1946
                    can.Observe.List.prototype._changes.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1947
                    if (/\w+\.destroyed/.test(attr)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1948
                        var index = this.indexOf(ev.target);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1949
                        if (index != -1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1950
                            this.splice(index, 1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1951
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1952
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1953
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1954
            })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1955
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1956
        return can.Model;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1957
    })(__m3, __m7);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1958
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1959
    // ## can/view/view.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1960
    var __m11 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1961
        // ## view.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1962
        // `can.view`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1963
        // _Templating abstraction._
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1964
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1965
        var isFunction = can.isFunction,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1966
            makeArray = can.makeArray,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1967
            // Used for hookup `id`s.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1968
            hookupId = 1,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1969
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1970
            $view = can.view = can.template = function(view, data, helpers, callback) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1971
                // If helpers is a `function`, it is actually a callback.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1972
                if (isFunction(helpers)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1973
                    callback = helpers;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1974
                    helpers = undefined;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1975
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1976
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1977
                var pipe = function(result) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1978
                    return $view.frag(result);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1979
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1980
                    // In case we got a callback, we need to convert the can.view.render
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1981
                    // result to a document fragment
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1982
                    wrapCallback = isFunction(callback) ? function(frag) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1983
                        callback(pipe(frag));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1984
                    } : null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1985
                    // Get the result.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1986
                    result = $view.render(view, data, helpers, wrapCallback),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1987
                    deferred = can.Deferred();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1988
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1989
                if (isFunction(result)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1990
                    return result;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1991
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1992
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1993
                if (can.isDeferred(result)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1994
                    result.then(function(result, data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1995
                        deferred.resolve.call(deferred, pipe(result), data);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1996
                    }, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1997
                        deferred.fail.apply(deferred, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1998
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  1999
                    return deferred;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2000
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2001
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2002
                // Convert it into a dom frag.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2003
                return pipe(result);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2004
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2005
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2006
        can.extend($view, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2007
                // creates a frag and hooks it up all at once
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2008
                frag: function(result, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2009
                    return $view.hookup($view.fragment(result), parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2010
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2011
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2012
                // simply creates a frag
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2013
                // this is used internally to create a frag
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2014
                // insert it
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2015
                // then hook it up
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2016
                fragment: function(result) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2017
                    var frag = can.buildFragment(result, document.body);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2018
                    // If we have an empty frag...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2019
                    if (!frag.childNodes.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2020
                        frag.appendChild(document.createTextNode(''));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2021
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2022
                    return frag;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2023
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2024
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2025
                // Convert a path like string into something that's ok for an `element` ID.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2026
                toId: function(src) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2027
                    return can.map(src.toString().split(/\/|\./g), function(part) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2028
                        // Dont include empty strings in toId functions
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2029
                        if (part) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2030
                            return part;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2031
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2032
                    }).join("_");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2033
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2034
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2035
                hookup: function(fragment, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2036
                    var hookupEls = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2037
                        id,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2038
                        func;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2039
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2040
                    // Get all `childNodes`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2041
                    can.each(fragment.childNodes ? can.makeArray(fragment.childNodes) : fragment, function(node) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2042
                        if (node.nodeType === 1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2043
                            hookupEls.push(node);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2044
                            hookupEls.push.apply(hookupEls, can.makeArray(node.getElementsByTagName('*')));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2045
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2046
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2047
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2048
                    // Filter by `data-view-id` attribute.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2049
                    can.each(hookupEls, function(el) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2050
                        if (el.getAttribute && (id = el.getAttribute('data-view-id')) && (func = $view.hookups[id])) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2051
                            func(el, parentNode, id);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2052
                            delete $view.hookups[id];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2053
                            el.removeAttribute('data-view-id');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2054
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2055
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2056
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2057
                    return fragment;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2058
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2059
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2060
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2061
                hookups: {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2062
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2063
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2064
                hook: function(cb) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2065
                    $view.hookups[++hookupId] = cb;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2066
                    return " data-view-id='" + hookupId + "'";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2067
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2068
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2069
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2070
                cached: {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2071
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2072
                cachedRenderers: {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2073
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2074
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2075
                cache: true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2076
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2077
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2078
                register: function(info) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2079
                    this.types["." + info.suffix] = info;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2080
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2081
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2082
                types: {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2083
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2084
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2085
                ext: ".ejs",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2086
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2087
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2088
                registerScript: function() {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2089
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2090
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2091
                preload: function() {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2092
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2093
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2094
                render: function(view, data, helpers, callback) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2095
                    // If helpers is a `function`, it is actually a callback.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2096
                    if (isFunction(helpers)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2097
                        callback = helpers;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2098
                        helpers = undefined;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2099
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2100
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2101
                    // See if we got passed any deferreds.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2102
                    var deferreds = getDeferreds(data);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2103
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2104
                    if (deferreds.length) { // Does data contain any deferreds?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2105
                        // The deferred that resolves into the rendered content...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2106
                        var deferred = new can.Deferred(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2107
                            dataCopy = can.extend({}, data);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2108
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2109
                        // Add the view request to the list of deferreds.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2110
                        deferreds.push(get(view, true))
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2111
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2112
                        // Wait for the view and all deferreds to finish...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2113
                        can.when.apply(can, deferreds).then(function(resolved) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2114
                            // Get all the resolved deferreds.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2115
                            var objs = makeArray(arguments),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2116
                                // Renderer is the last index of the data.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2117
                                renderer = objs.pop(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2118
                                // The result of the template rendering with data.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2119
                                result;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2120
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2121
                            // Make data look like the resolved deferreds.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2122
                            if (can.isDeferred(data)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2123
                                dataCopy = usefulPart(resolved);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2124
                            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2125
                                // Go through each prop in data again and
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2126
                                // replace the defferreds with what they resolved to.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2127
                                for (var prop in data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2128
                                    if (can.isDeferred(data[prop])) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2129
                                        dataCopy[prop] = usefulPart(objs.shift());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2130
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2131
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2132
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2133
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2134
                            // Get the rendered result.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2135
                            result = renderer(dataCopy, helpers);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2136
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2137
                            // Resolve with the rendered view.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2138
                            deferred.resolve(result, dataCopy);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2139
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2140
                            // If there's a `callback`, call it back with the result.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2141
                            callback && callback(result, dataCopy);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2142
                        }, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2143
                            deferred.reject.apply(deferred, arguments)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2144
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2145
                        // Return the deferred...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2146
                        return deferred;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2147
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2148
                        // No deferreds! Render this bad boy.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2149
                        var response,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2150
                            // If there's a `callback` function
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2151
                            async = isFunction(callback),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2152
                            // Get the `view` type
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2153
                            deferred = get(view, async);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2154
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2155
                        // If we are `async`...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2156
                        if (async) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2157
                            // Return the deferred
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2158
                            response = deferred;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2159
                            // And fire callback with the rendered result.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2160
                            deferred.then(function(renderer) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2161
                                callback(data ? renderer(data, helpers) : renderer);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2162
                            })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2163
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2164
                            // if the deferred is resolved, call the cached renderer instead
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2165
                            // this is because it's possible, with recursive deferreds to
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2166
                            // need to render a view while its deferred is _resolving_.  A _resolving_ deferred
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2167
                            // is a deferred that was just resolved and is calling back it's success callbacks.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2168
                            // If a new success handler is called while resoliving, it does not get fired by
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2169
                            // jQuery's deferred system.  So instead of adding a new callback
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2170
                            // we use the cached renderer.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2171
                            // We also add __view_id on the deferred so we can look up it's cached renderer.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2172
                            // In the future, we might simply store either a deferred or the cached result.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2173
                            if (deferred.state() === "resolved" && deferred.__view_id) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2174
                                var currentRenderer = $view.cachedRenderers[deferred.__view_id];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2175
                                return data ? currentRenderer(data, helpers) : currentRenderer;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2176
                            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2177
                                // Otherwise, the deferred is complete, so
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2178
                                // set response to the result of the rendering.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2179
                                deferred.then(function(renderer) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2180
                                    response = data ? renderer(data, helpers) : renderer;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2181
                                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2182
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2183
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2184
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2185
                        return response;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2186
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2187
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2188
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2189
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2190
                registerView: function(id, text, type, def) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2191
                    // Get the renderer function.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2192
                    var func = (type || $view.types[$view.ext]).renderer(id, text);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2193
                    def = def || new can.Deferred();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2194
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2195
                    // Cache if we are caching.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2196
                    if ($view.cache) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2197
                        $view.cached[id] = def;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2198
                        def.__view_id = id;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2199
                        $view.cachedRenderers[id] = func;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2200
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2201
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2202
                    // Return the objects for the response's `dataTypes`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2203
                    // (in this case view).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2204
                    return def.resolve(func);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2205
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2206
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2207
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2208
        // Makes sure there's a template, if not, have `steal` provide a warning.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2209
        var checkText = function(text, url) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2210
            if (!text.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2211
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2212
                throw "can.view: No template or empty template:" + url;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2213
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2214
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2215
            // `Returns a `view` renderer deferred.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2216
            // `url` - The url to the template.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2217
            // `async` - If the ajax request should be asynchronous.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2218
            // Returns a deferred.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2219
            get = function(url, async) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2220
                var suffix = url.match(/\.[\w\d]+$/),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2221
                    type,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2222
                    // If we are reading a script element for the content of the template,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2223
                    // `el` will be set to that script element.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2224
                    el,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2225
                    // A unique identifier for the view (used for caching).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2226
                    // This is typically derived from the element id or
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2227
                    // the url for the template.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2228
                    id,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2229
                    // The ajax request used to retrieve the template content.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2230
                    jqXHR;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2231
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2232
                //If the url has a #, we assume we want to use an inline template
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2233
                //from a script element and not current page's HTML
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2234
                if (url.match(/^#/)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2235
                    url = url.substr(1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2236
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2237
                // If we have an inline template, derive the suffix from the `text/???` part.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2238
                // This only supports `<script>` tags.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2239
                if (el = document.getElementById(url)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2240
                    suffix = "." + el.type.match(/\/(x\-)?(.+)/)[2];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2241
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2242
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2243
                // If there is no suffix, add one.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2244
                if (!suffix && !$view.cached[url]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2245
                    url += (suffix = $view.ext);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2246
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2247
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2248
                if (can.isArray(suffix)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2249
                    suffix = suffix[0]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2250
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2251
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2252
                // Convert to a unique and valid id.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2253
                id = $view.toId(url);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2254
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2255
                // If an absolute path, use `steal` to get it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2256
                // You should only be using `//` if you are using `steal`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2257
                if (url.match(/^\/\//)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2258
                    var sub = url.substr(2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2259
                    url = !window.steal ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2260
                        sub :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2261
                        steal.config().root.mapJoin("" + steal.id(sub));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2262
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2263
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2264
                // Set the template engine type.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2265
                type = $view.types[suffix];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2266
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2267
                // If it is cached, 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2268
                if ($view.cached[id]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2269
                    // Return the cached deferred renderer.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2270
                    return $view.cached[id];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2271
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2272
                    // Otherwise if we are getting this from a `<script>` element.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2273
                } else if (el) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2274
                    // Resolve immediately with the element's `innerHTML`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2275
                    return $view.registerView(id, el.innerHTML, type);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2276
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2277
                    // Make an ajax request for text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2278
                    var d = new can.Deferred();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2279
                    can.ajax({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2280
                            async: async,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2281
                            url: url,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2282
                            dataType: "text",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2283
                            error: function(jqXHR) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2284
                                checkText("", url);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2285
                                d.reject(jqXHR);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2286
                            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2287
                            success: function(text) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2288
                                // Make sure we got some text back.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2289
                                checkText(text, url);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2290
                                $view.registerView(id, text, type, d)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2291
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2292
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2293
                    return d;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2294
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2295
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2296
            // Gets an `array` of deferreds from an `object`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2297
            // This only goes one level deep.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2298
            getDeferreds = function(data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2299
                var deferreds = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2300
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2301
                // pull out deferreds
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2302
                if (can.isDeferred(data)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2303
                    return [data]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2304
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2305
                    for (var prop in data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2306
                        if (can.isDeferred(data[prop])) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2307
                            deferreds.push(data[prop]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2308
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2309
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2310
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2311
                return deferreds;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2312
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2313
            // Gets the useful part of a resolved deferred.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2314
            // This is for `model`s and `can.ajax` that resolve to an `array`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2315
            usefulPart = function(resolved) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2316
                return can.isArray(resolved) && resolved[1] === 'success' ? resolved[0] : resolved
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2317
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2318
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2319
        //!steal-pluginify-remove-start
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2320
        if (window.steal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2321
            steal.type("view js", function(options, success, error) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2322
                var type = $view.types["." + options.type],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2323
                    id = $view.toId(options.id);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2324
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2325
                options.text = "steal('" + (type.plugin || "can/view/" + options.type) + "',function(can){return " + "can.view.preload('" + id + "'," + options.text + ");\n})";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2326
                success();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2327
            })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2328
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2329
        //!steal-pluginify-remove-end
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2330
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2331
        can.extend($view, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2332
                register: function(info) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2333
                    this.types["." + info.suffix] = info;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2334
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2335
                    //!steal-pluginify-remove-start
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2336
                    if (window.steal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2337
                        steal.type(info.suffix + " view js", function(options, success, error) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2338
                            var type = $view.types["." + options.type],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2339
                                id = $view.toId(options.id + '');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2340
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2341
                            options.text = type.script(id, options.text)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2342
                            success();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2343
                        })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2344
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2345
                    //!steal-pluginify-remove-end
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2346
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2347
                    $view[info.suffix] = function(id, text) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2348
                        if (!text) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2349
                            // Return a nameless renderer
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2350
                            var renderer = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2351
                                return $view.frag(renderer.render.apply(this, arguments));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2352
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2353
                            renderer.render = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2354
                                var renderer = info.renderer(null, id);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2355
                                return renderer.apply(renderer, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2356
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2357
                            return renderer;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2358
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2359
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2360
                        $view.preload(id, info.renderer(id, text));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2361
                        return can.view(id);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2362
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2363
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2364
                registerScript: function(type, id, src) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2365
                    return "can.view.preload('" + id + "'," + $view.types["." + type].script(id, src) + ");";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2366
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2367
                preload: function(id, renderer) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2368
                    $view.cached[id] = new can.Deferred().resolve(function(data, helpers) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2369
                        return renderer.call(data, data, helpers);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2370
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2371
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2372
                    function frag() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2373
                        return $view.frag(renderer.apply(this, arguments));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2374
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2375
                    // expose the renderer for mustache
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2376
                    frag.render = renderer;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2377
                    return frag;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2378
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2379
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2380
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2381
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2382
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2383
    })(__m3);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2384
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2385
    // ## can/view/elements.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2386
    var __m14 = (function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2387
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2388
        var elements = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2389
            tagToContentPropMap: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2390
                option: "textContent" in document.createElement("option") ? "textContent" : "innerText",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2391
                textarea: "value"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2392
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2393
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2394
            attrMap: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2395
                "class": "className",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2396
                "value": "value",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2397
                "innerText": "innerText",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2398
                "textContent": "textContent",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2399
                "checked": true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2400
                "disabled": true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2401
                "readonly": true,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2402
                "required": true
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2403
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2404
            // elements whos default value we should set
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2405
            defaultValue: ["input", "textarea"],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2406
            // a map of parent element to child elements
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2407
            tagMap: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2408
                "": "span",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2409
                table: "tbody",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2410
                tr: "td",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2411
                ol: "li",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2412
                ul: "li",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2413
                tbody: "tr",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2414
                thead: "tr",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2415
                tfoot: "tr",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2416
                select: "option",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2417
                optgroup: "option"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2418
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2419
            // a tag's parent element
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2420
            reverseTagMap: {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2421
                tr: "tbody",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2422
                option: "select",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2423
                td: "tr",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2424
                th: "tr",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2425
                li: "ul"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2426
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2427
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2428
            getParentNode: function(el, defaultParentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2429
                return defaultParentNode && el.parentNode.nodeType === 11 ? defaultParentNode : el.parentNode;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2430
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2431
            // set an attribute on an element
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2432
            setAttr: function(el, attrName, val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2433
                var tagName = el.nodeName.toString().toLowerCase(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2434
                    prop = elements.attrMap[attrName];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2435
                // if this is a special property
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2436
                if (prop === true) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2437
                    el[attrName] = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2438
                } else if (prop) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2439
                    // set the value as true / false
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2440
                    el[prop] = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2441
                    if (prop === "value" && can.inArray(tagName, elements.defaultValue) >= 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2442
                        el.defaultValue = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2443
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2444
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2445
                    el.setAttribute(attrName, val);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2446
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2447
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2448
            // gets the value of an attribute
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2449
            getAttr: function(el, attrName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2450
                // Default to a blank string for IE7/8
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2451
                return (elements.attrMap[attrName] && el[elements.attrMap[attrName]] ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2452
                    el[elements.attrMap[attrName]] :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2453
                    el.getAttribute(attrName)) || '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2454
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2455
            // removes the attribute
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2456
            removeAttr: function(el, attrName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2457
                if (elements.attrMap[attrName] === true) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2458
                    el[attrName] = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2459
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2460
                    el.removeAttribute(attrName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2461
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2462
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2463
            contentText: function(text) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2464
                if (typeof text == 'string') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2465
                    return text;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2466
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2467
                // If has no value, return an empty string.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2468
                if (!text && text !== 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2469
                    return '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2470
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2471
                return "" + text;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2472
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2473
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2474
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2475
        return elements;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2476
    })();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2477
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2478
    // ## can/view/scanner.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2479
    var __m13 = (function(can, elements) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2480
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2481
        var newLine = /(\r|\n)+/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2482
            // Escapes characters starting with `\`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2483
            clean = function(content) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2484
                return content
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2485
                    .split('\\').join("\\\\")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2486
                    .split("\n").join("\\n")
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2487
                    .split('"').join('\\"')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2488
                    .split("\t").join("\\t");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2489
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2490
            // Returns a tagName to use as a temporary placeholder for live content
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2491
            // looks forward ... could be slow, but we only do it when necessary
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2492
            getTag = function(tagName, tokens, i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2493
                // if a tagName is provided, use that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2494
                if (tagName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2495
                    return tagName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2496
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2497
                    // otherwise go searching for the next two tokens like "<",TAG
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2498
                    while (i < tokens.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2499
                        if (tokens[i] == "<" && elements.reverseTagMap[tokens[i + 1]]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2500
                            return elements.reverseTagMap[tokens[i + 1]];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2501
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2502
                        i++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2503
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2504
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2505
                return '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2506
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2507
            bracketNum = function(content) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2508
                return (--content.split("{").length) - (--content.split("}").length);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2509
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2510
            myEval = function(script) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2511
                eval(script);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2512
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2513
            attrReg = /([^\s]+)[\s]*=[\s]*$/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2514
            // Commands for caching.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2515
            startTxt = 'var ___v1ew = [];',
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2516
            finishTxt = "return ___v1ew.join('')",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2517
            put_cmd = "___v1ew.push(",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2518
            insert_cmd = put_cmd,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2519
            // Global controls (used by other functions to know where we are).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2520
            // Are we inside a tag?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2521
            htmlTag = null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2522
            // Are we within a quote within a tag?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2523
            quote = null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2524
            // What was the text before the current quote? (used to get the `attr` name)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2525
            beforeQuote = null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2526
            // Whether a rescan is in progress
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2527
            rescan = null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2528
            // Used to mark where the element is.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2529
            status = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2530
                // `t` - `1`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2531
                // `h` - `0`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2532
                // `q` - String `beforeQuote`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2533
                return quote ? "'" + beforeQuote.match(attrReg)[1] + "'" : (htmlTag ? 1 : 0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2534
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2535
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2536
        can.view.Scanner = Scanner = function(options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2537
            // Set options on self
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2538
            can.extend(this, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2539
                    text: {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2540
                    tokens: []
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2541
                }, options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2542
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2543
            // Cache a token lookup
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2544
            this.tokenReg = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2545
            this.tokenSimple = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2546
                "<": "<",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2547
                ">": ">",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2548
                '"': '"',
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2549
                "'": "'"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2550
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2551
            this.tokenComplex = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2552
            this.tokenMap = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2553
            for (var i = 0, token; token = this.tokens[i]; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2554
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2555
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2556
                // Save complex mappings (custom regexp)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2557
                if (token[2]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2558
                    this.tokenReg.push(token[2]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2559
                    this.tokenComplex.push({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2560
                            abbr: token[1],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2561
                            re: new RegExp(token[2]),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2562
                            rescan: token[3]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2563
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2564
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2565
                // Save simple mappings (string only, no regexp)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2566
                else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2567
                    this.tokenReg.push(token[1]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2568
                    this.tokenSimple[token[1]] = token[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2569
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2570
                this.tokenMap[token[0]] = token[1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2571
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2572
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2573
            // Cache the token registry.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2574
            this.tokenReg = new RegExp("(" + this.tokenReg.slice(0).concat(["<", ">", '"', "'"]).join("|") + ")", "g");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2575
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2576
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2577
        Scanner.prototype = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2578
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2579
            helpers: [
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2580
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2581
                {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2582
                    name: /\s*\(([\$\w]+)\)\s*->([^\n]*)/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2583
                    fn: function(content) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2584
                        var quickFunc = /\s*\(([\$\w]+)\)\s*->([^\n]*)/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2585
                            parts = content.match(quickFunc);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2586
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2587
                        return "can.proxy(function(__){var " + parts[1] + "=can.$(__);" + parts[2] + "}, this);";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2588
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2589
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2590
            ],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2591
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2592
            scan: function(source, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2593
                var tokens = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2594
                    last = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2595
                    simple = this.tokenSimple,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2596
                    complex = this.tokenComplex;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2597
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2598
                source = source.replace(newLine, "\n");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2599
                if (this.transform) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2600
                    source = this.transform(source);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2601
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2602
                source.replace(this.tokenReg, function(whole, part) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2603
                    // offset is the second to last argument
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2604
                    var offset = arguments[arguments.length - 2];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2605
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2606
                    // if the next token starts after the last token ends
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2607
                    // push what's in between
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2608
                    if (offset > last) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2609
                        tokens.push(source.substring(last, offset));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2610
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2611
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2612
                    // push the simple token (if there is one)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2613
                    if (simple[whole]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2614
                        tokens.push(whole);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2615
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2616
                    // otherwise lookup complex tokens
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2617
                    else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2618
                        for (var i = 0, token; token = complex[i]; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2619
                            if (token.re.test(whole)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2620
                                tokens.push(token.abbr);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2621
                                // Push a rescan function if one exists
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2622
                                if (token.rescan) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2623
                                    tokens.push(token.rescan(part));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2624
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2625
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2626
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2627
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2628
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2629
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2630
                    // update the position of the last part of the last token
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2631
                    last = offset + part.length;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2632
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2633
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2634
                // if there's something at the end, add it
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2635
                if (last < source.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2636
                    tokens.push(source.substr(last));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2637
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2638
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2639
                var content = '',
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2640
                    buff = [startTxt + (this.text.start || '')],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2641
                    // Helper `function` for putting stuff in the view concat.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2642
                    put = function(content, bonus) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2643
                        buff.push(put_cmd, '"', clean(content), '"' + (bonus || '') + ');');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2644
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2645
                    // A stack used to keep track of how we should end a bracket
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2646
                    // `}`.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2647
                    // Once we have a `<%= %>` with a `leftBracket`,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2648
                    // we store how the file should end here (either `))` or `;`).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2649
                    endStack = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2650
                    // The last token, used to remember which tag we are in.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2651
                    lastToken,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2652
                    // The corresponding magic tag.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2653
                    startTag = null,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2654
                    // Was there a magic tag inside an html tag?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2655
                    magicInTag = false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2656
                    // The current tag name.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2657
                    tagName = '',
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2658
                    // stack of tagNames
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2659
                    tagNames = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2660
                    // Pop from tagNames?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2661
                    popTagName = false,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2662
                    // Declared here.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2663
                    bracketCount,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2664
                    i = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2665
                    token,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2666
                    tmap = this.tokenMap;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2667
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2668
                // Reinitialize the tag state goodness.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2669
                htmlTag = quote = beforeQuote = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2670
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2671
                for (;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2672
                    (token = tokens[i++]) !== undefined;) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2673
                    if (startTag === null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2674
                        switch (token) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2675
                            case tmap.left:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2676
                            case tmap.escapeLeft:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2677
                            case tmap.returnLeft:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2678
                                magicInTag = htmlTag && 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2679
                            case tmap.commentLeft:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2680
                                // A new line -- just add whatever content within a clean.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2681
                                // Reset everything.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2682
                                startTag = token;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2683
                                if (content.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2684
                                    put(content);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2685
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2686
                                content = '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2687
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2688
                            case tmap.escapeFull:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2689
                                // This is a full line escape (a line that contains only whitespace and escaped logic)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2690
                                // Break it up into escape left and right
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2691
                                magicInTag = htmlTag && 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2692
                                rescan = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2693
                                startTag = tmap.escapeLeft;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2694
                                if (content.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2695
                                    put(content);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2696
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2697
                                rescan = tokens[i++];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2698
                                content = rescan.content || rescan;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2699
                                if (rescan.before) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2700
                                    put(rescan.before);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2701
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2702
                                tokens.splice(i, 0, tmap.right);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2703
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2704
                            case tmap.commentFull:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2705
                                // Ignore full line comments.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2706
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2707
                            case tmap.templateLeft:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2708
                                content += tmap.left;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2709
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2710
                            case '<':
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2711
                                // Make sure we are not in a comment.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2712
                                if (tokens[i].indexOf("!--") !== 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2713
                                    htmlTag = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2714
                                    magicInTag = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2715
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2716
                                content += token;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2717
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2718
                            case '>':
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2719
                                htmlTag = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2720
                                // content.substr(-1) doesn't work in IE7/8
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2721
                                var emptyElement = content.substr(content.length - 1) == "/" || content.substr(content.length - 2) == "--";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2722
                                // if there was a magic tag
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2723
                                // or it's an element that has text content between its tags, 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2724
                                // but content is not other tags add a hookup
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2725
                                // TODO: we should only add `can.EJS.pending()` if there's a magic tag 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2726
                                // within the html tags.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2727
                                if (magicInTag || !popTagName && elements.tagToContentPropMap[tagNames[tagNames.length - 1]]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2728
                                    // make sure / of /> is on the left of pending
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2729
                                    if (emptyElement) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2730
                                        put(content.substr(0, content.length - 1), ",can.view.pending(),\"/>\"");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2731
                                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2732
                                        put(content, ",can.view.pending(),\">\"");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2733
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2734
                                    content = '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2735
                                    magicInTag = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2736
                                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2737
                                    content += token;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2738
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2739
                                // if it's a tag like <input/>
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2740
                                if (emptyElement || popTagName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2741
                                    // remove the current tag in the stack
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2742
                                    tagNames.pop();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2743
                                    // set the current tag to the previous parent
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2744
                                    tagName = tagNames[tagNames.length - 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2745
                                    // Don't pop next time
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2746
                                    popTagName = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2747
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2748
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2749
                            case "'":
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2750
                            case '"':
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2751
                                // If we are in an html tag, finding matching quotes.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2752
                                if (htmlTag) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2753
                                    // We have a quote and it matches.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2754
                                    if (quote && quote === token) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2755
                                        // We are exiting the quote.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2756
                                        quote = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2757
                                        // Otherwise we are creating a quote.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2758
                                        // TODO: does this handle `\`?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2759
                                    } else if (quote === null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2760
                                        quote = token;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2761
                                        beforeQuote = lastToken;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2762
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2763
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2764
                            default:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2765
                                // Track the current tag
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2766
                                if (lastToken === '<') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2767
                                    tagName = token.split(/\s/)[0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2768
                                    if (tagName.indexOf("/") === 0 && tagNames[tagNames.length - 1] === tagName.substr(1)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2769
                                        // set tagName to the last tagName
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2770
                                        // if there are no more tagNames, we'll rely on getTag.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2771
                                        tagName = tagNames[tagNames.length - 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2772
                                        popTagName = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2773
                                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2774
                                        tagNames.push(tagName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2775
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2776
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2777
                                content += token;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2778
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2779
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2780
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2781
                        // We have a start tag.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2782
                        switch (token) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2783
                            case tmap.right:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2784
                            case tmap.returnRight:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2785
                                switch (startTag) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2786
                                    case tmap.left:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2787
                                        // Get the number of `{ minus }`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2788
                                        bracketCount = bracketNum(content);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2789
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2790
                                        // We are ending a block.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2791
                                        if (bracketCount == 1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2792
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2793
                                            // We are starting on.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2794
                                            buff.push(insert_cmd, "can.view.txt(0,'" + getTag(tagName, tokens, i) + "'," + status() + ",this,function(){", startTxt, content);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2795
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2796
                                            endStack.push({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2797
                                                    before: "",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2798
                                                    after: finishTxt + "}));\n"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2799
                                                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2800
                                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2801
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2802
                                            // How are we ending this statement?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2803
                                            last = // If the stack has value and we are ending a block...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2804
                                            endStack.length && bracketCount == -1 ? // Use the last item in the block stack.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2805
                                            endStack.pop() : // Or use the default ending.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2806
                                            {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2807
                                                after: ";"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2808
                                            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2809
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2810
                                            // If we are ending a returning block, 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2811
                                            // add the finish text which returns the result of the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2812
                                            // block.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2813
                                            if (last.before) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2814
                                                buff.push(last.before);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2815
                                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2816
                                            // Add the remaining content.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2817
                                            buff.push(content, ";", last.after);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2818
                                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2819
                                        break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2820
                                    case tmap.escapeLeft:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2821
                                    case tmap.returnLeft:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2822
                                        // We have an extra `{` -> `block`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2823
                                        // Get the number of `{ minus }`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2824
                                        bracketCount = bracketNum(content);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2825
                                        // If we have more `{`, it means there is a block.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2826
                                        if (bracketCount) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2827
                                            // When we return to the same # of `{` vs `}` end with a `doubleParent`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2828
                                            endStack.push({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2829
                                                    before: finishTxt,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2830
                                                    after: "}));"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2831
                                                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2832
                                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2833
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2834
                                        var escaped = startTag === tmap.escapeLeft ? 1 : 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2835
                                            commands = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2836
                                                insert: insert_cmd,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2837
                                                tagName: getTag(tagName, tokens, i),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2838
                                                status: status()
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2839
                                            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2840
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2841
                                        for (var ii = 0; ii < this.helpers.length; ii++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2842
                                            // Match the helper based on helper
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2843
                                            // regex name value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2844
                                            var helper = this.helpers[ii];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2845
                                            if (helper.name.test(content)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2846
                                                content = helper.fn(content, commands);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2847
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2848
                                                // dont escape partials
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2849
                                                if (helper.name.source == /^>[\s]*\w*/.source) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2850
                                                    escaped = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2851
                                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2852
                                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2853
                                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2854
                                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2855
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2856
                                        // Handle special cases
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2857
                                        if (typeof content == 'object') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2858
                                            if (content.raw) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2859
                                                buff.push(content.raw);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2860
                                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2861
                                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2862
                                            // If we have `<%== a(function(){ %>` then we want
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2863
                                            // `can.EJS.text(0,this, function(){ return a(function(){ var _v1ew = [];`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2864
                                            buff.push(insert_cmd, "can.view.txt(" + escaped + ",'" + tagName + "'," + status() + ",this,function(){ " + (this.text.escape || '') + "return ", content,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2865
                                                // If we have a block.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2866
                                                bracketCount ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2867
                                                // Start with startTxt `"var _v1ew = [];"`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2868
                                                startTxt :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2869
                                                // If not, add `doubleParent` to close push and text.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2870
                                                "}));");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2871
                                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2872
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2873
                                        if (rescan && rescan.after && rescan.after.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2874
                                            put(rescan.after.length);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2875
                                            rescan = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2876
                                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2877
                                        break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2878
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2879
                                startTag = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2880
                                content = '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2881
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2882
                            case tmap.templateLeft:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2883
                                content += tmap.left;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2884
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2885
                            default:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2886
                                content += token;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2887
                                break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2888
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2889
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2890
                    lastToken = token;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2891
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2892
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2893
                // Put it together...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2894
                if (content.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2895
                    // Should be `content.dump` in Ruby.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2896
                    put(content);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2897
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2898
                buff.push(";");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2899
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2900
                var template = buff.join(''),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2901
                    out = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2902
                        out: 'with(_VIEW) { with (_CONTEXT) {' + template + " " + finishTxt + "}}"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2903
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2904
                // Use `eval` instead of creating a function, because it is easier to debug.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2905
                myEval.call(out, 'this.fn = (function(_CONTEXT,_VIEW){' + out.out + '});\r\n//@ sourceURL=' + name + ".js");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2906
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2907
                return out;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2908
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2909
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2910
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2911
        return Scanner;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2912
    })(__m11, __m14);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2913
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2914
    // ## can/view/node_lists.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2915
    var __m17 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2916
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2917
        // text node expando test
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2918
        var canExpando = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2919
        try {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2920
            document.createTextNode('')._ = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2921
        } catch (ex) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2922
            canExpando = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2923
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2924
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2925
        // a mapping of element ids to nodeList ids
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2926
        var nodeMap = {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2927
            // a mapping of ids to text nodes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2928
            textNodeMap = {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2929
            // a mapping of nodeList ids to nodeList
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2930
            nodeListMap = {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2931
            expando = "ejs_" + Math.random(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2932
            _id = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2933
            id = function(node) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2934
                if (canExpando || node.nodeType !== 3) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2935
                    if (node[expando]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2936
                        return node[expando];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2937
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2938
                        return node[expando] = (node.nodeName ? "element_" : "obj_") + (++_id);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2939
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2940
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2941
                    for (var textNodeID in textNodeMap) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2942
                        if (textNodeMap[textNodeID] === node) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2943
                            return textNodeID;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2944
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2945
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2946
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2947
                    textNodeMap["text_" + (++_id)] = node;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2948
                    return "text_" + _id;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2949
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2950
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2951
            // removes a nodeListId from a node's nodeListIds
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2952
            removeNodeListId = function(node, nodeListId) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2953
                var nodeListIds = nodeMap[id(node)];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2954
                if (nodeListIds) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2955
                    var index = can.inArray(nodeListId, nodeListIds);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2956
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2957
                    if (index >= 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2958
                        nodeListIds.splice(index, 1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2959
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2960
                    if (!nodeListIds.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2961
                        delete nodeMap[id(node)];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2962
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2963
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2964
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2965
            addNodeListId = function(node, nodeListId) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2966
                var nodeListIds = nodeMap[id(node)];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2967
                if (!nodeListIds) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2968
                    nodeListIds = nodeMap[id(node)] = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2969
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2970
                nodeListIds.push(nodeListId);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2971
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2972
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2973
        var nodeLists = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2974
            id: id,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2975
            // replaces the contents of one node list with the nodes in another list
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2976
            replace: function(oldNodeList, newNodes) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2977
                // for each node in the node list
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2978
                oldNodeList = can.makeArray(oldNodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2979
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2980
                // try every set
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2981
                //can.each( oldNodeList, function(node){
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2982
                var node = oldNodeList[0]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2983
                // for each nodeList the node is in
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2984
                can.each(can.makeArray(nodeMap[id(node)]), function(nodeListId) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2985
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2986
                    // if startNode to endNode is 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2987
                    // within list, replace that list
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2988
                    // I think the problem is not the WHOLE part is being 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2989
                    // matched
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2990
                    var nodeList = nodeListMap[nodeListId],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2991
                        startIndex = can.inArray(node, nodeList),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2992
                        endIndex = can.inArray(oldNodeList[oldNodeList.length - 1], nodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2993
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2994
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2995
                    // remove this nodeListId from each node
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2996
                    if (startIndex >= 0 && endIndex >= 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2997
                        for (var i = startIndex; i <= endIndex; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2998
                            var n = nodeList[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  2999
                            removeNodeListId(n, nodeListId);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3000
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3001
                        // swap in new nodes into the nodeLIst
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3002
                        nodeList.splice.apply(nodeList, [startIndex, endIndex - startIndex + 1].concat(newNodes));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3003
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3004
                        // tell these new nodes they belong to the nodeList
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3005
                        can.each(newNodes, function(node) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3006
                            addNodeListId(node, nodeListId);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3007
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3008
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3009
                        nodeLists.unregister(nodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3010
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3011
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3012
                //});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3013
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3014
            // registers a list of nodes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3015
            register: function(nodeList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3016
                var nLId = id(nodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3017
                nodeListMap[nLId] = nodeList;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3018
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3019
                can.each(nodeList, function(node) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3020
                    addNodeListId(node, nLId);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3021
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3022
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3023
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3024
            // removes mappings
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3025
            unregister: function(nodeList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3026
                var nLId = id(nodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3027
                can.each(nodeList, function(node) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3028
                    removeNodeListId(node, nLId);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3029
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3030
                delete nodeListMap[nLId];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3031
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3032
            nodeMap: nodeMap,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3033
            nodeListMap: nodeListMap
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3034
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3035
        var ids = function(nodeList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3036
            return nodeList.map(function(n) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3037
                return id(n) + ":" + (n.innerHTML || n.nodeValue)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3038
            })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3039
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3040
        return nodeLists;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3041
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3042
    })(__m3);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3043
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3044
    // ## can/view/live.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3045
    var __m16 = (function(can, elements, view, nodeLists) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3046
        // ## live.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3047
        // The live module provides live binding for computes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3048
        // and can.Observe.List.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3049
        // Currently, it's API is designed for `can/view/render`, but
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3050
        // it could easily be used for other purposes.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3051
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3052
        // ### Helper methods
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3053
        // #### setup
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3054
        // `setup(HTMLElement, bind(data), unbind(data)) -> data`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3055
        // Calls bind right away, but will call unbind
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3056
        // if the element is "destroyed" (removed from the DOM).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3057
        var setup = function(el, bind, unbind) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3058
            var teardown = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3059
                unbind(data)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3060
                can.unbind.call(el, 'destroyed', teardown);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3061
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3062
                data = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3063
                    teardownCheck: function(parent) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3064
                        if (!parent) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3065
                            teardown();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3066
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3067
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3068
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3069
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3070
            can.bind.call(el, 'destroyed', teardown);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3071
            bind(data)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3072
            return data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3073
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3074
            // #### listen
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3075
            // Calls setup, but presets bind and unbind to 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3076
            // operate on a compute
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3077
            listen = function(el, compute, change) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3078
                return setup(el, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3079
                    compute.bind("change", change);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3080
                }, function(data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3081
                    compute.unbind("change", change);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3082
                    if (data.nodeList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3083
                        nodeLists.unregister(data.nodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3084
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3085
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3086
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3087
            // #### getAttributeParts
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3088
            // Breaks up a string like foo='bar' into ["foo","'bar'""]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3089
            getAttributeParts = function(newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3090
                return (newVal || "").replace(/['"]/g, '').split('=')
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3091
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3092
            // #### insertElementsAfter
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3093
            // Appends elements after the last item in oldElements.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3094
        insertElementsAfter = function(oldElements, newFrag) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3095
            var last = oldElements[oldElements.length - 1];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3096
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3097
            // Insert it in the `document` or `documentFragment`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3098
            if (last.nextSibling) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3099
                last.parentNode.insertBefore(newFrag, last.nextSibling);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3100
            } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3101
                last.parentNode.appendChild(newFrag);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3102
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3103
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3104
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3105
        var live = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3106
            nodeLists: nodeLists,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3107
            list: function(el, list, func, context, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3108
                // A mapping of the index to an array
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3109
                // of elements that represent the item.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3110
                // Each array is registered so child or parent
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3111
                // live structures can update the elements
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3112
                var nodesMap = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3113
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3114
                    add = function(ev, items, index) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3115
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3116
                        // Collect new html and mappings
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3117
                        var frag = document.createDocumentFragment(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3118
                            newMappings = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3119
                        can.each(items, function(item) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3120
                            var itemHTML = func.call(context, item),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3121
                                itemFrag = can.view.frag(itemHTML, parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3122
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3123
                            newMappings.push(can.makeArray(itemFrag.childNodes));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3124
                            frag.appendChild(itemFrag);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3125
                        })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3126
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3127
                        // Inserting at the end of the list
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3128
                        if (!nodesMap[index]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3129
                            insertElementsAfter(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3130
                                index == 0 ? [text] :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3131
                                nodesMap[index - 1], frag)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3132
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3133
                            var el = nodesMap[index][0];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3134
                            el.parentNode.insertBefore(frag, el)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3135
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3136
                        // register each item
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3137
                        can.each(newMappings, function(nodeList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3138
                            nodeLists.register(nodeList)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3139
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3140
                        [].splice.apply(nodesMap, [index, 0].concat(newMappings));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3141
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3142
                    remove = function(ev, items, index) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3143
                        var removedMappings = nodesMap.splice(index, items.length),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3144
                            itemsToRemove = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3145
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3146
                        can.each(removedMappings, function(nodeList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3147
                            // add items that we will remove all at once
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3148
                            [].push.apply(itemsToRemove, nodeList)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3149
                            // Update any parent lists to remove these items
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3150
                            nodeLists.replace(nodeList, []);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3151
                            // unregister the list
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3152
                            nodeLists.unregister(nodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3153
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3154
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3155
                        can.remove(can.$(itemsToRemove));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3156
                    },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3157
                    parentNode = elements.getParentNode(el, parentNode),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3158
                    text = document.createTextNode("");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3159
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3160
                // Setup binding and teardown to add and remove events
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3161
                setup(parentNode, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3162
                    list.bind("add", add).bind("remove", remove)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3163
                }, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3164
                    list.unbind("add", add).unbind("remove", remove);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3165
                    can.each(nodesMap, function(nodeList) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3166
                        nodeLists.unregister(nodeList);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3167
                    })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3168
                })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3169
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3170
                insertElementsAfter([el], text);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3171
                can.remove(can.$(el));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3172
                add({}, list, 0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3173
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3174
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3175
            html: function(el, compute, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3176
                var parentNode = elements.getParentNode(el, parentNode),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3177
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3178
                    data = listen(parentNode, compute, function(ev, newVal, oldVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3179
                        var attached = nodes[0].parentNode;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3180
                        // update the nodes in the DOM with the new rendered value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3181
                        if (attached) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3182
                            makeAndPut(newVal);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3183
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3184
                        data.teardownCheck(nodes[0].parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3185
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3186
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3187
                var nodes,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3188
                    makeAndPut = function(val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3189
                        // create the fragment, but don't hook it up
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3190
                        // we need to insert it into the document first
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3191
                        var frag = can.view.frag(val, parentNode),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3192
                            // keep a reference to each node
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3193
                            newNodes = can.makeArray(frag.childNodes);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3194
                        // Insert it in the `document` or `documentFragment`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3195
                        insertElementsAfter(nodes || [el], frag)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3196
                        // nodes hasn't been set yet
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3197
                        if (!nodes) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3198
                            can.remove(can.$(el));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3199
                            nodes = newNodes;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3200
                            // set the teardown nodeList
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3201
                            data.nodeList = nodes;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3202
                            nodeLists.register(nodes);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3203
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3204
                            // Update node Array's to point to new nodes
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3205
                            // and then remove the old nodes.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3206
                            // It has to be in this order for Mootools
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3207
                            // and IE because somehow, after an element
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3208
                            // is removed from the DOM, it loses its
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3209
                            // expando values.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3210
                            var nodesToRemove = can.makeArray(nodes);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3211
                            nodeLists.replace(nodes, newNodes);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3212
                            can.remove(can.$(nodesToRemove));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3213
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3214
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3215
                makeAndPut(compute(), [el]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3216
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3217
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3218
            text: function(el, compute, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3219
                var parent = elements.getParentNode(el, parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3220
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3221
                // setup listening right away so we don't have to re-calculate value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3222
                var data = listen(el.parentNode !== parent ? el.parentNode : parent, compute, function(ev, newVal, oldVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3223
                    // Sometimes this is 'unknown' in IE and will throw an exception if it is
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3224
                    if (typeof node.nodeValue != 'unknown') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3225
                        node.nodeValue = "" + newVal;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3226
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3227
                    data.teardownCheck(node.parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3228
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3229
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3230
                var node = document.createTextNode(compute());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3231
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3232
                if (el.parentNode !== parent) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3233
                    parent = el.parentNode;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3234
                    parent.insertBefore(node, el);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3235
                    parent.removeChild(el);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3236
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3237
                    parent.insertBefore(node, el);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3238
                    parent.removeChild(el);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3239
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3240
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3241
            attributes: function(el, compute, currentValue) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3242
                var setAttrs = function(newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3243
                    var parts = getAttributeParts(newVal),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3244
                        newAttrName = parts.shift();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3245
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3246
                    // Remove if we have a change and used to have an `attrName`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3247
                    if ((newAttrName != attrName) && attrName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3248
                        elements.removeAttr(el, attrName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3249
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3250
                    // Set if we have a new `attrName`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3251
                    if (newAttrName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3252
                        elements.setAttr(el, newAttrName, parts.join('='));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3253
                        attrName = newAttrName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3254
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3255
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3256
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3257
                listen(el, compute, function(ev, newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3258
                    setAttrs(newVal)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3259
                })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3260
                // current value has been set
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3261
                if (arguments.length >= 3) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3262
                    var attrName = getAttributeParts(currentValue)[0]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3263
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3264
                    setAttrs(compute())
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3265
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3266
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3267
            attributePlaceholder: '__!!__',
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3268
            attributeReplace: /__!!__/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3269
            attribute: function(el, attributeName, compute) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3270
                listen(el, compute, function(ev, newVal) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3271
                    elements.setAttr(el, attributeName, hook.render());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3272
                })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3273
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3274
                var wrapped = can.$(el),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3275
                    hooks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3276
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3277
                // Get the list of hookups or create one for this element.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3278
                // Hooks is a map of attribute names to hookup `data`s.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3279
                // Each hookup data has:
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3280
                // `render` - A `function` to render the value of the attribute.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3281
                // `funcs` - A list of hookup `function`s on that attribute.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3282
                // `batchNum` - The last event `batchNum`, used for performance.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3283
                hooks = can.data(wrapped, 'hooks');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3284
                if (!hooks) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3285
                    can.data(wrapped, 'hooks', hooks = {});
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3286
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3287
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3288
                // Get the attribute value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3289
                var attr = elements.getAttr(el, attributeName),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3290
                    // Split the attribute value by the template.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3291
                    // Only split out the first __!!__ so if we have multiple hookups in the same attribute, 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3292
                    // they will be put in the right spot on first render
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3293
                    parts = attr.split(live.attributePlaceholder),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3294
                    goodParts = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3295
                    hook;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3296
                goodParts.push(parts.shift(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3297
                    parts.join(live.attributePlaceholder));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3298
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3299
                // If we already had a hookup for this attribute...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3300
                if (hooks[attributeName]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3301
                    // Just add to that attribute's list of `function`s.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3302
                    hooks[attributeName].computes.push(compute);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3303
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3304
                    // Create the hookup data.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3305
                    hooks[attributeName] = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3306
                        render: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3307
                            var i = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3308
                                // attr doesn't have a value in IE
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3309
                                newAttr = attr ? attr.replace(live.attributeReplace, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3310
                                    return elements.contentText(hook.computes[i++]());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3311
                                }) : elements.contentText(hook.computes[i++]());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3312
                            return newAttr;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3313
                        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3314
                        computes: [compute],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3315
                        batchNum: undefined
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3316
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3317
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3318
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3319
                // Save the hook for slightly faster performance.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3320
                hook = hooks[attributeName];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3321
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3322
                // Insert the value in parts.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3323
                goodParts.splice(1, 0, compute());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3324
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3325
                // Set the attribute.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3326
                elements.setAttr(el, attributeName, goodParts.join(""));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3327
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3328
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3329
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3330
        return live;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3331
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3332
    })(__m3, __m14, __m11, __m17);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3333
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3334
    // ## can/view/render.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3335
    var __m15 = (function(can, elements, live) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3336
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3337
        var pendingHookups = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3338
            tagChildren = function(tagName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3339
                var newTag = elements.tagMap[tagName] || "span";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3340
                if (newTag === "span") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3341
                    //innerHTML in IE doesn't honor leading whitespace after empty elements
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3342
                    return "@@!!@@";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3343
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3344
                return "<" + newTag + ">" + tagChildren(newTag) + "</" + newTag + ">";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3345
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3346
            contentText = function(input, tag) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3347
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3348
                // If it's a string, return.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3349
                if (typeof input == 'string') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3350
                    return input;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3351
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3352
                // If has no value, return an empty string.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3353
                if (!input && input !== 0) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3354
                    return '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3355
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3356
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3357
                // If it's an object, and it has a hookup method.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3358
                var hook = (input.hookup &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3359
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3360
                    // Make a function call the hookup method.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3361
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3362
                    function(el, id) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3363
                        input.hookup.call(input, el, id);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3364
                    }) ||
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3365
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3366
                // Or if it's a `function`, just use the input.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3367
                (typeof input == 'function' && input);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3368
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3369
                // Finally, if there is a `function` to hookup on some dom,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3370
                // add it to pending hookups.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3371
                if (hook) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3372
                    if (tag) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3373
                        return "<" + tag + " " + can.view.hook(hook) + "></" + tag + ">"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3374
                    } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3375
                        pendingHookups.push(hook);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3376
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3377
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3378
                    return '';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3379
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3380
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3381
                // Finally, if all else is `false`, `toString()` it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3382
                return "" + input;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3383
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3384
            // Returns escaped/sanatized content for anything other than a live-binding
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3385
            contentEscape = function(txt) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3386
                return (typeof txt == 'string' || typeof txt == 'number') ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3387
                    can.esc(txt) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3388
                    contentText(txt);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3389
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3390
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3391
        var current;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3392
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3393
        can.extend(can.view, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3394
                live: live,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3395
                setupLists: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3396
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3397
                    var old = can.view.lists,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3398
                        data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3399
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3400
                    can.view.lists = function(list, renderer) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3401
                        data = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3402
                            list: list,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3403
                            renderer: renderer
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3404
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3405
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3406
                    return function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3407
                        can.view.lists = old;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3408
                        return data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3409
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3410
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3411
                pending: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3412
                    // TODO, make this only run for the right tagName
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3413
                    var hooks = pendingHookups.slice(0);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3414
                    lastHookups = hooks;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3415
                    pendingHookups = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3416
                    return can.view.hook(function(el) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3417
                        can.each(hooks, function(fn) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3418
                            fn(el);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3419
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3420
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3421
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3422
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3423
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3424
                txt: function(escape, tagName, status, self, func) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3425
                    var listTeardown = can.view.setupLists(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3426
                        emptyHandler = function() {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3427
                        unbind = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3428
                            compute.unbind("change", emptyHandler)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3429
                        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3430
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3431
                    var compute = can.compute(func, self, false);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3432
                    // bind to get and temporarily cache the value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3433
                    compute.bind("change", emptyHandler);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3434
                    // call the "wrapping" function and get the binding information
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3435
                    var tag = (elements.tagMap[tagName] || "span"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3436
                        listData = listTeardown(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3437
                        value = compute();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3438
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3439
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3440
                    if (listData) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3441
                        return "<" + tag + can.view.hook(function(el, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3442
                            live.list(el, listData.list, listData.renderer, self, parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3443
                        }) + "></" + tag + ">";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3444
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3445
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3446
                    // If we had no observes just return the value returned by func.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3447
                    if (!compute.hasDependencies) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3448
                        unbind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3449
                        return (escape || status !== 0 ? contentEscape : contentText)(value, status === 0 && tag);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3450
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3451
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3452
                    // the property (instead of innerHTML elements) to adjust. For
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3453
                    // example options should use textContent
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3454
                    var contentProp = elements.tagToContentPropMap[tagName];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3455
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3456
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3457
                    // The magic tag is outside or between tags.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3458
                    if (status === 0 && !contentProp) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3459
                        // Return an element tag with a hookup in place of the content
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3460
                        return "<" + tag + can.view.hook(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3461
                            escape ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3462
                            // If we are escaping, replace the parentNode with 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3463
                            // a text node who's value is `func`'s return value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3464
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3465
                            function(el, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3466
                                live.text(el, compute, parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3467
                                unbind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3468
                            } :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3469
                            // If we are not escaping, replace the parentNode with a
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3470
                            // documentFragment created as with `func`'s return value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3471
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3472
                            function(el, parentNode) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3473
                                live.html(el, compute, parentNode);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3474
                                unbind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3475
                                //children have to be properly nested HTML for buildFragment to work properly
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3476
                            }) + ">" + tagChildren(tag) + "</" + tag + ">";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3477
                        // In a tag, but not in an attribute
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3478
                    } else if (status === 1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3479
                        // remember the old attr name
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3480
                        pendingHookups.push(function(el) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3481
                            live.attributes(el, compute, compute());
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3482
                            unbind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3483
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3484
                        return compute();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3485
                    } else { // In an attribute...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3486
                        var attributeName = status === 0 ? contentProp : status;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3487
                        // if the magic tag is inside the element, like `<option><% TAG %></option>`,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3488
                        // we add this hookup to the last element (ex: `option`'s) hookups.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3489
                        // Otherwise, the magic tag is in an attribute, just add to the current element's
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3490
                        // hookups.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3491
                        (status === 0 ? lastHookups : pendingHookups).push(function(el) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3492
                            live.attribute(el, attributeName, compute);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3493
                            unbind();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3494
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3495
                        return live.attributePlaceholder;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3496
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3497
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3498
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3499
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3500
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3501
    })(__m11, __m14, __m16, __m2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3502
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3503
    // ## can/view/ejs/ejs.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3504
    var __m12 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3505
        // ## ejs.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3506
        // `can.EJS`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3507
        // _Embedded JavaScript Templates._
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3508
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3509
        // Helper methods.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3510
        var extend = can.extend,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3511
            EJS = function(options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3512
                // Supports calling EJS without the constructor
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3513
                // This returns a function that renders the template.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3514
                if (this.constructor != EJS) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3515
                    var ejs = new EJS(options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3516
                    return function(data, helpers) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3517
                        return ejs.render(data, helpers);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3518
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3519
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3520
                // If we get a `function` directly, it probably is coming from
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3521
                // a `steal`-packaged view.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3522
                if (typeof options == "function") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3523
                    this.template = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3524
                        fn: options
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3525
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3526
                    return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3527
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3528
                // Set options on self.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3529
                extend(this, options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3530
                this.template = this.scanner.scan(this.text, this.name);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3531
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3532
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3533
        can.EJS = EJS;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3534
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3535
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3536
        EJS.prototype.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3537
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3538
        render = function(object, extraHelpers) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3539
            object = object || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3540
            return this.template.fn.call(object, object, new EJS.Helpers(object, extraHelpers || {}));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3541
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3542
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3543
        extend(EJS.prototype, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3544
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3545
                scanner: new can.view.Scanner({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3546
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3547
                        tokens: [
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3548
                            ["templateLeft", "<%%"], // Template
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3549
                            ["templateRight", "%>"], // Right Template
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3550
                            ["returnLeft", "<%=="], // Return Unescaped
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3551
                            ["escapeLeft", "<%="], // Return Escaped
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3552
                            ["commentLeft", "<%#"], // Comment
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3553
                            ["left", "<%"], // Run --- this is hack for now
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3554
                            ["right", "%>"], // Right -> All have same FOR Mustache ...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3555
                            ["returnRight", "%>"]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3556
                        ],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3557
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3558
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3559
                        transform: function(source) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3560
                            return source.replace(/<%([\s\S]+?)%>/gm, function(whole, part) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3561
                                var brackets = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3562
                                    foundBracketPair,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3563
                                    i;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3564
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3565
                                // Look for brackets (for removing self-contained blocks)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3566
                                part.replace(/[{}]/gm, function(bracket, offset) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3567
                                    brackets.push([bracket, offset]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3568
                                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3569
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3570
                                // Remove bracket pairs from the list of replacements
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3571
                                do {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3572
                                    foundBracketPair = false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3573
                                    for (i = brackets.length - 2; i >= 0; i--) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3574
                                        if (brackets[i][0] == '{' && brackets[i + 1][0] == '}') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3575
                                            brackets.splice(i, 2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3576
                                            foundBracketPair = true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3577
                                            break;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3578
                                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3579
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3580
                                } while (foundBracketPair);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3581
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3582
                                // Unmatched brackets found, inject EJS tags
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3583
                                if (brackets.length >= 2) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3584
                                    var result = ['<%'],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3585
                                        bracket,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3586
                                        last = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3587
                                    for (i = 0; bracket = brackets[i]; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3588
                                        result.push(part.substring(last, last = bracket[1]));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3589
                                        if ((bracket[0] == '{' && i < brackets.length - 1) || (bracket[0] == '}' && i > 0)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3590
                                            result.push(bracket[0] == '{' ? '{ %><% ' : ' %><% }');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3591
                                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3592
                                            result.push(bracket[0]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3593
                                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3594
                                        ++last;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3595
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3596
                                    result.push(part.substring(last), '%>');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3597
                                    return result.join('');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3598
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3599
                                // Otherwise return the original
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3600
                                else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3601
                                    return '<%' + part + '%>';
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3602
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3603
                            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3604
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3605
                    })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3606
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3607
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3608
        EJS.Helpers = function(data, extras) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3609
            this._data = data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3610
            this._extras = extras;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3611
            extend(this, extras);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3612
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3613
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3614
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3615
        EJS.Helpers.prototype = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3616
            // TODO Deprecated!!
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3617
            list: function(list, cb) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3618
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3619
                can.each(list, function(item, i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3620
                    cb(item, i, list)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3621
                })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3622
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3623
            each: function(list, cb) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3624
                // Normal arrays don't get live updated
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3625
                if (can.isArray(list)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3626
                    this.list(list, cb);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3627
                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3628
                    can.view.lists(list, cb);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3629
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3630
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3631
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3632
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3633
        // Options for `steal`'s build.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3634
        can.view.register({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3635
                suffix: "ejs",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3636
                // returns a `function` that renders the view.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3637
                script: function(id, src) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3638
                    return "can.EJS(function(_CONTEXT,_VIEW) { " + new EJS({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3639
                            text: src,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3640
                            name: id
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3641
                        }).template.out + " })";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3642
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3643
                renderer: function(id, text) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3644
                    return EJS({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3645
                            text: text,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3646
                            name: id
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3647
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3648
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3649
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3650
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3651
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3652
    })(__m3, __m11, __m2, __m9, __m13, __m15);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3653
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3654
    // ## can/control/control.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3655
    var __m18 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3656
        // ## control.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3657
        // `can.Control`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3658
        // _Controller_
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3659
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3660
        // Binds an element, returns a function that unbinds.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3661
        var bind = function(el, ev, callback) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3662
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3663
            can.bind.call(el, ev, callback);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3664
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3665
            return function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3666
                can.unbind.call(el, ev, callback);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3667
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3668
        },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3669
            isFunction = can.isFunction,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3670
            extend = can.extend,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3671
            each = can.each,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3672
            slice = [].slice,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3673
            paramReplacer = /\{([^\}]+)\}/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3674
            special = can.getObject("$.event.special", [can]) || {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3675
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3676
            // Binds an element, returns a function that unbinds.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3677
            delegate = function(el, selector, ev, callback) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3678
                can.delegate.call(el, selector, ev, callback);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3679
                return function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3680
                    can.undelegate.call(el, selector, ev, callback);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3681
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3682
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3683
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3684
            // Calls bind or unbind depending if there is a selector.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3685
            binder = function(el, ev, callback, selector) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3686
                return selector ?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3687
                    delegate(el, can.trim(selector), ev, callback) :
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3688
                    bind(el, ev, callback);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3689
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3690
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3691
            basicProcessor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3692
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3693
        var Control = can.Control = can.Construct(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3694
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3695
            {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3696
                // Setup pre-processes which methods are event listeners.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3697
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3698
                setup: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3699
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3700
                    // Allow contollers to inherit "defaults" from super-classes as it 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3701
                    // done in `can.Construct`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3702
                    can.Construct.setup.apply(this, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3703
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3704
                    // If you didn't provide a name, or are `control`, don't do anything.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3705
                    if (can.Control) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3706
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3707
                        // Cache the underscored names.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3708
                        var control = this,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3709
                            funcName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3710
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3711
                        // Calculate and cache actions.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3712
                        control.actions = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3713
                        for (funcName in control.prototype) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3714
                            if (control._isAction(funcName)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3715
                                control.actions[funcName] = control._action(funcName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3716
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3717
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3718
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3719
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3720
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3721
                // Moves `this` to the first argument, wraps it with `jQuery` if it's an element
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3722
                _shifter: function(context, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3723
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3724
                    var method = typeof name == "string" ? context[name] : name;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3725
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3726
                    if (!isFunction(method)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3727
                        method = context[method];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3728
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3729
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3730
                    return function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3731
                        context.called = name;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3732
                        return method.apply(context, [this.nodeName ? can.$(this) : this].concat(slice.call(arguments, 0)));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3733
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3734
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3735
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3736
                // Return `true` if is an action.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3737
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3738
                _isAction: function(methodName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3739
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3740
                    var val = this.prototype[methodName],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3741
                        type = typeof val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3742
                    // if not the constructor
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3743
                    return (methodName !== 'constructor') &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3744
                    // and is a function or links to a function
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3745
                    (type == "function" || (type == "string" && isFunction(this.prototype[val]))) &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3746
                    // and is in special, a processor, or has a funny character
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3747
                    !! (special[methodName] || processors[methodName] || /[^\w]/.test(methodName));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3748
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3749
                // Takes a method name and the options passed to a control
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3750
                // and tries to return the data necessary to pass to a processor
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3751
                // (something that binds things).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3752
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3753
                _action: function(methodName, options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3754
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3755
                    // If we don't have options (a `control` instance), we'll run this 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3756
                    // later.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3757
                    paramReplacer.lastIndex = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3758
                    if (options || !paramReplacer.test(methodName)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3759
                        // If we have options, run sub to replace templates `{}` with a
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3760
                        // value from the options or the window
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3761
                        var convertedName = options ? can.sub(methodName, [options, window]) : methodName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3762
                        if (!convertedName) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3763
                            return null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3764
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3765
                        // If a `{}` template resolves to an object, `convertedName` will be
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3766
                        // an array
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3767
                        var arr = can.isArray(convertedName),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3768
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3769
                            // Get the name
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3770
                            name = arr ? convertedName[1] : convertedName,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3771
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3772
                            // Grab the event off the end
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3773
                            parts = name.split(/\s+/g),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3774
                            event = parts.pop();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3775
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3776
                        return {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3777
                            processor: processors[event] || basicProcessor,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3778
                            parts: [name, parts.join(" "), event],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3779
                            delegate: arr ? convertedName[0] : undefined
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3780
                        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3781
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3782
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3783
                // An object of `{eventName : function}` pairs that Control uses to 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3784
                // hook up events auto-magically.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3785
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3786
                processors: {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3787
                // A object of name-value pairs that act as default values for a 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3788
                // control instance
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3789
                defaults: {}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3790
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3791
            }, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3792
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3793
                // Sets `this.element`, saves the control in `data, binds event
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3794
                // handlers.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3795
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3796
                setup: function(element, options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3797
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3798
                    var cls = this.constructor,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3799
                        pluginname = cls.pluginName || cls._fullName,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3800
                        arr;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3801
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3802
                    // Want the raw element here.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3803
                    this.element = can.$(element)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3804
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3805
                    if (pluginname && pluginname !== 'can_control') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3806
                        // Set element and `className` on element.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3807
                        this.element.addClass(pluginname);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3808
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3809
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3810
                    (arr = can.data(this.element, "controls")) || can.data(this.element, "controls", arr = []);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3811
                    arr.push(this);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3812
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3813
                    // Option merging.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3814
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3815
                    this.options = extend({}, cls.defaults, options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3816
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3817
                    // Bind all event handlers.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3818
                    this.on();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3819
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3820
                    // Gets passed into `init`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3821
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3822
                    return [this.element, this.options];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3823
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3824
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3825
                on: function(el, selector, eventName, func) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3826
                    if (!el) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3827
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3828
                        // Adds bindings.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3829
                        this.off();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3830
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3831
                        // Go through the cached list of actions and use the processor 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3832
                        // to bind
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3833
                        var cls = this.constructor,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3834
                            bindings = this._bindings,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3835
                            actions = cls.actions,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3836
                            element = this.element,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3837
                            destroyCB = can.Control._shifter(this, "destroy"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3838
                            funcName, ready;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3839
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3840
                        for (funcName in actions) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3841
                            // Only push if we have the action and no option is `undefined`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3842
                            if (actions.hasOwnProperty(funcName) &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3843
                                (ready = actions[funcName] || cls._action(funcName, this.options))) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3844
                                bindings.push(ready.processor(ready.delegate || element,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3845
                                        ready.parts[2], ready.parts[1], funcName, this));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3846
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3847
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3848
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3849
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3850
                        // Setup to be destroyed...  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3851
                        // don't bind because we don't want to remove it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3852
                        can.bind.call(element, "destroyed", destroyCB);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3853
                        bindings.push(function(el) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3854
                            can.unbind.call(el, "destroyed", destroyCB);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3855
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3856
                        return bindings.length;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3857
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3858
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3859
                    if (typeof el == 'string') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3860
                        func = eventName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3861
                        eventName = selector;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3862
                        selector = el;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3863
                        el = this.element;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3864
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3865
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3866
                    if (func === undefined) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3867
                        func = eventName;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3868
                        eventName = selector;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3869
                        selector = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3870
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3871
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3872
                    if (typeof func == 'string') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3873
                        func = can.Control._shifter(this, func);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3874
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3875
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3876
                    this._bindings.push(binder(el, eventName, func, selector));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3877
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3878
                    return this._bindings.length;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3879
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3880
                // Unbinds all event handlers on the controller.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3881
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3882
                off: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3883
                    var el = this.element[0]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3884
                    each(this._bindings || [], function(value) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3885
                        value(el);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3886
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3887
                    // Adds bindings.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3888
                    this._bindings = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3889
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3890
                // Prepares a `control` for garbage collection
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3891
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3892
                destroy: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3893
                    //Control already destroyed
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3894
                    if (this.element === null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3895
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3896
                        return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3897
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3898
                    var Class = this.constructor,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3899
                        pluginName = Class.pluginName || Class._fullName,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3900
                        controls;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3901
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3902
                    // Unbind bindings.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3903
                    this.off();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3904
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3905
                    if (pluginName && pluginName !== 'can_control') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3906
                        // Remove the `className`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3907
                        this.element.removeClass(pluginName);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3908
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3909
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3910
                    // Remove from `data`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3911
                    controls = can.data(this.element, "controls");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3912
                    controls.splice(can.inArray(this, controls), 1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3913
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3914
                    can.trigger(this, "destroyed"); // In case we want to know if the `control` is removed.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3915
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3916
                    this.element = null;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3917
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3918
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3919
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3920
        var processors = can.Control.processors,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3921
            // Processors do the binding.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3922
            // They return a function that unbinds when called.  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3923
            // The basic processor that binds events.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3924
            basicProcessor = function(el, event, selector, methodName, control) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3925
                return binder(el, event, can.Control._shifter(control, methodName), selector);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3926
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3927
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3928
        // Set common events to be processed as a `basicProcessor`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3929
        each(["change", "click", "contextmenu", "dblclick", "keydown", "keyup",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3930
                "keypress", "mousedown", "mousemove", "mouseout", "mouseover",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3931
                "mouseup", "reset", "resize", "scroll", "select", "submit", "focusin",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3932
                "focusout", "mouseenter", "mouseleave",
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3933
                // #104 - Add touch events as default processors
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3934
                // TOOD feature detect?
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3935
                "touchstart", "touchmove", "touchcancel", "touchend", "touchleave"
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3936
            ], function(v) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3937
                processors[v] = basicProcessor;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3938
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3939
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3940
        return Control;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3941
    })(__m3, __m1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3942
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3943
    // ## can/util/string/deparam/deparam.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3944
    var __m20 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3945
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3946
        // ## deparam.js  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3947
        // `can.deparam`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3948
        // _Takes a string of name value pairs and returns a Object literal that represents those params._
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3949
        var digitTest = /^\d+$/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3950
            keyBreaker = /([^\[\]]+)|(\[\])/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3951
            paramTest = /([^?#]*)(#.*)?$/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3952
            prep = function(str) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3953
                return decodeURIComponent(str.replace(/\+/g, " "));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3954
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3955
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3956
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3957
        can.extend(can, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3958
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3959
                deparam: function(params) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3960
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3961
                    var data = {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3962
                        pairs, lastPart;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3963
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3964
                    if (params && paramTest.test(params)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3965
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3966
                        pairs = params.split('&'),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3967
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3968
                        can.each(pairs, function(pair) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3969
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3970
                            var parts = pair.split('='),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3971
                                key = prep(parts.shift()),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3972
                                value = prep(parts.join("=")),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3973
                                current = data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3974
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3975
                            if (key) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3976
                                parts = key.match(keyBreaker);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3977
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3978
                                for (var j = 0, l = parts.length - 1; j < l; j++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3979
                                    if (!current[parts[j]]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3980
                                        // If what we are pointing to looks like an `array`
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3981
                                        current[parts[j]] = digitTest.test(parts[j + 1]) || parts[j + 1] == "[]" ? [] : {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3982
                                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3983
                                    current = current[parts[j]];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3984
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3985
                                lastPart = parts.pop();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3986
                                if (lastPart == "[]") {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3987
                                    current.push(value);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3988
                                } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3989
                                    current[lastPart] = value;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3990
                                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3991
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3992
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3993
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3994
                    return data;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3995
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3996
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3997
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3998
    })(__m3, __m2);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  3999
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4000
    // ## can/route/route.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4001
    var __m19 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4002
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4003
        // ## route.js  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4004
        // `can.route`  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4005
        // _Helps manage browser history (and client state) by synchronizing the 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4006
        // `window.location.hash` with a `can.Observe`._  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4007
        // Helper methods used for matching routes.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4008
        var
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4009
        // `RegExp` used to match route variables of the type ':name'.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4010
        // Any word character or a period is matched.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4011
        matcher = /\:([\w\.]+)/g,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4012
            // Regular expression for identifying &amp;key=value lists.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4013
            paramsMatcher = /^(?:&[^=]+=[^&]*)+/,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4014
            // Converts a JS Object into a list of parameters that can be 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4015
            // inserted into an html element tag.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4016
            makeProps = function(props) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4017
                var tags = [];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4018
                can.each(props, function(val, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4019
                    tags.push((name === 'className' ? 'class' : name) + '="' +
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4020
                        (name === "href" ? val : can.esc(val)) + '"');
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4021
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4022
                return tags.join(" ");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4023
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4024
            // Checks if a route matches the data provided. If any route variable
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4025
            // is not present in the data, the route does not match. If all route
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4026
            // variables are present in the data, the number of matches is returned 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4027
            // to allow discerning between general and more specific routes. 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4028
            matchesData = function(route, data) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4029
                var count = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4030
                    i = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4031
                    defaults = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4032
                // look at default values, if they match ...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4033
                for (var name in route.defaults) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4034
                    if (route.defaults[name] === data[name]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4035
                        // mark as matched
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4036
                        defaults[name] = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4037
                        count++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4038
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4039
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4040
                for (; i < route.names.length; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4041
                    if (!data.hasOwnProperty(route.names[i])) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4042
                        return -1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4043
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4044
                    if (!defaults[route.names[i]]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4045
                        count++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4046
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4047
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4048
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4049
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4050
                return count;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4051
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4052
            onready = !0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4053
            location = window.location,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4054
            wrapQuote = function(str) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4055
                return (str + '').replace(/([.?*+\^$\[\]\\(){}|\-])/g, "\\$1");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4056
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4057
            each = can.each,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4058
            extend = can.extend;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4059
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4060
        can.route = function(url, defaults) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4061
            defaults = defaults || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4062
            // Extract the variable names and replace with `RegExp` that will match
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4063
            // an atual URL with values.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4064
            var names = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4065
                test = url.replace(matcher, function(whole, name, i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4066
                    names.push(name);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4067
                    var next = "\\" + (url.substr(i + whole.length, 1) || can.route._querySeparator);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4068
                    // a name without a default value HAS to have a value
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4069
                    // a name that has a default value can be empty
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4070
                    // The `\\` is for string-escaping giving single `\` for `RegExp` escaping.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4071
                    return "([^" + next + "]" + (defaults[name] ? "*" : "+") + ")";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4072
                });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4073
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4074
            // Add route in a form that can be easily figured out.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4075
            can.route.routes[url] = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4076
                // A regular expression that will match the route when variable values 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4077
                // are present; i.e. for `:page/:type` the `RegExp` is `/([\w\.]*)/([\w\.]*)/` which
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4078
                // will match for any value of `:page` and `:type` (word chars or period).
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4079
                test: new RegExp("^" + test + "($|" + wrapQuote(can.route._querySeparator) + ")"),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4080
                // The original URL, same as the index for this entry in routes.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4081
                route: url,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4082
                // An `array` of all the variable names in this route.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4083
                names: names,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4084
                // Default values provided for the variables.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4085
                defaults: defaults,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4086
                // The number of parts in the URL separated by `/`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4087
                length: url.split('/').length
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4088
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4089
            return can.route;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4090
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4091
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4092
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4093
        extend(can.route, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4094
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4095
                _querySeparator: '&',
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4096
                _paramsMatcher: paramsMatcher,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4097
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4098
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4099
                param: function(data, _setRoute) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4100
                    // Check if the provided data keys match the names in any routes;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4101
                    // Get the one with the most matches.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4102
                    var route,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4103
                        // Need to have at least 1 match.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4104
                        matches = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4105
                        matchCount,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4106
                        routeName = data.route,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4107
                        propCount = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4108
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4109
                    delete data.route;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4110
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4111
                    each(data, function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4112
                        propCount++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4113
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4114
                    // Otherwise find route.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4115
                    each(can.route.routes, function(temp, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4116
                        // best route is the first with all defaults matching
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4117
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4118
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4119
                        matchCount = matchesData(temp, data);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4120
                        if (matchCount > matches) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4121
                            route = temp;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4122
                            matches = matchCount;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4123
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4124
                        if (matchCount >= propCount) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4125
                            return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4126
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4127
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4128
                    // If we have a route name in our `can.route` data, and it's
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4129
                    // just as good as what currently matches, use that
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4130
                    if (can.route.routes[routeName] && matchesData(can.route.routes[routeName], data) === matches) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4131
                        route = can.route.routes[routeName];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4132
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4133
                    // If this is match...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4134
                    if (route) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4135
                        var cpy = extend({}, data),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4136
                            // Create the url by replacing the var names with the provided data.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4137
                            // If the default value is found an empty string is inserted.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4138
                            res = route.route.replace(matcher, function(whole, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4139
                                delete cpy[name];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4140
                                return data[name] === route.defaults[name] ? "" : encodeURIComponent(data[name]);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4141
                            }),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4142
                            after;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4143
                        // Remove matching default values
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4144
                        each(route.defaults, function(val, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4145
                            if (cpy[name] === val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4146
                                delete cpy[name];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4147
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4148
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4149
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4150
                        // The remaining elements of data are added as 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4151
                        // `&amp;` separated parameters to the url.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4152
                        after = can.param(cpy);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4153
                        // if we are paraming for setting the hash
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4154
                        // we also want to make sure the route value is updated
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4155
                        if (_setRoute) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4156
                            can.route.attr('route', route.route);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4157
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4158
                        return res + (after ? can.route._querySeparator + after : "");
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4159
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4160
                    // If no route was found, there is no hash URL, only paramters.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4161
                    return can.isEmptyObject(data) ? "" : can.route._querySeparator + can.param(data);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4162
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4163
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4164
                deparam: function(url) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4165
                    // See if the url matches any routes by testing it against the `route.test` `RegExp`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4166
                    // By comparing the URL length the most specialized route that matches is used.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4167
                    var route = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4168
                        length: -1
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4169
                    };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4170
                    each(can.route.routes, function(temp, name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4171
                        if (temp.test.test(url) && temp.length > route.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4172
                            route = temp;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4173
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4174
                    });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4175
                    // If a route was matched.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4176
                    if (route.length > -1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4177
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4178
                        var // Since `RegExp` backreferences are used in `route.test` (parens)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4179
                        // the parts will contain the full matched string and each variable (back-referenced) value.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4180
                        parts = url.match(route.test),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4181
                            // Start will contain the full matched string; parts contain the variable values.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4182
                            start = parts.shift(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4183
                            // The remainder will be the `&amp;key=value` list at the end of the URL.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4184
                            remainder = url.substr(start.length - (parts[parts.length - 1] === can.route._querySeparator ? 1 : 0)),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4185
                            // If there is a remainder and it contains a `&amp;key=value` list deparam it.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4186
                            obj = (remainder && can.route._paramsMatcher.test(remainder)) ? can.deparam(remainder.slice(1)) : {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4187
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4188
                        // Add the default values for this route.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4189
                        obj = extend(true, {}, route.defaults, obj);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4190
                        // Overwrite each of the default values in `obj` with those in 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4191
                        // parts if that part is not empty.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4192
                        each(parts, function(part, i) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4193
                            if (part && part !== can.route._querySeparator) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4194
                                obj[route.names[i]] = decodeURIComponent(part);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4195
                            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4196
                        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4197
                        obj.route = route.route;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4198
                        return obj;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4199
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4200
                    // If no route was matched, it is parsed as a `&amp;key=value` list.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4201
                    if (url.charAt(0) !== can.route._querySeparator) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4202
                        url = can.route._querySeparator + url;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4203
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4204
                    return can.route._paramsMatcher.test(url) ? can.deparam(url.slice(1)) : {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4205
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4206
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4207
                data: new can.Observe({}),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4208
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4209
                routes: {},
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4210
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4211
                ready: function(val) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4212
                    if (val === false) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4213
                        onready = val;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4214
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4215
                    if (val === true || onready === true) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4216
                        can.route._setup();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4217
                        setState();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4218
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4219
                    return can.route;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4220
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4221
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4222
                url: function(options, merge) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4223
                    if (merge) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4224
                        options = extend({}, curParams, options)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4225
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4226
                    return "#!" + can.route.param(options);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4227
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4228
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4229
                link: function(name, options, props, merge) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4230
                    return "<a " + makeProps(
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4231
                        extend({
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4232
                                href: can.route.url(options, merge)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4233
                            }, props)) + ">" + name + "</a>";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4234
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4235
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4236
                current: function(options) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4237
                    return location.hash == "#!" + can.route.param(options)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4238
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4239
                _setup: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4240
                    // If the hash changes, update the `can.route.data`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4241
                    can.bind.call(window, 'hashchange', setState);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4242
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4243
                _getHash: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4244
                    return location.href.split(/#!?/)[1] || "";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4245
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4246
                _setHash: function(serialized) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4247
                    var path = (can.route.param(serialized, true));
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4248
                    location.hash = "#!" + path;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4249
                    return path;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4250
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4251
            });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4252
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4253
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4254
        // The functions in the following list applied to `can.route` (e.g. `can.route.attr('...')`) will
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4255
        // instead act on the `can.route.data` observe.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4256
        each(['bind', 'unbind', 'delegate', 'undelegate', 'attr', 'removeAttr'], function(name) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4257
            can.route[name] = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4258
                // `delegate` and `undelegate` require
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4259
                // the `can/observe/delegate` plugin
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4260
                if (!can.route.data[name]) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4261
                    return;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4262
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4263
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4264
                return can.route.data[name].apply(can.route.data, arguments);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4265
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4266
        })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4267
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4268
        var // A ~~throttled~~ debounced function called multiple times will only fire once the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4269
        // timer runs down. Each call resets the timer.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4270
        timer,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4271
            // Intermediate storage for `can.route.data`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4272
            curParams,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4273
            // Deparameterizes the portion of the hash of interest and assign the
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4274
            // values to the `can.route.data` removing existing values no longer in the hash.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4275
            // setState is called typically by hashchange which fires asynchronously
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4276
            // So it's possible that someone started changing the data before the 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4277
            // hashchange event fired.  For this reason, it will not set the route data
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4278
            // if the data is changing or the hash already matches the hash that was set.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4279
            setState = can.route.setState = function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4280
                var hash = can.route._getHash();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4281
                curParams = can.route.deparam(hash);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4282
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4283
                // if the hash data is currently changing, or
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4284
                // the hash is what we set it to anyway, do NOT change the hash
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4285
                if (!changingData || hash !== lastHash) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4286
                    can.route.attr(curParams, true);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4287
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4288
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4289
            // The last hash caused by a data change
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4290
            lastHash,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4291
            // Are data changes pending that haven't yet updated the hash
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4292
            changingData;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4293
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4294
        // If the `can.route.data` changes, update the hash.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4295
        // Using `.serialize()` retrieves the raw data contained in the `observable`.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4296
        // This function is ~~throttled~~ debounced so it only updates once even if multiple values changed.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4297
        // This might be able to use batchNum and avoid this.
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4298
        can.route.bind("change", function(ev, attr) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4299
            // indicate that data is changing
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4300
            changingData = 1;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4301
            clearTimeout(timer);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4302
            timer = setTimeout(function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4303
                // indicate that the hash is set to look like the data
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4304
                changingData = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4305
                var serialized = can.route.data.serialize();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4306
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4307
                lastHash = can.route._setHash(serialized);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4308
            }, 1);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4309
        });
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4310
        // `onready` event...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4311
        can.bind.call(document, "ready", can.route.ready);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4312
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4313
        // Libraries other than jQuery don't execute the document `ready` listener
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4314
        // if we are already DOM ready
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4315
        if ((document.readyState === 'complete' || document.readyState === "interactive") && onready) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4316
            can.route.ready();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4317
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4318
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4319
        // extend route to have a similar property 
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4320
        // that is often checked in mustache to determine
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4321
        // an object's observability
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4322
        can.route.constructor.canMakeObserve = can.Observe.canMakeObserve;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4323
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4324
        return can.route;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4325
    })(__m3, __m7, __m20);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4326
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4327
    // ## can/control/route/route.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4328
    var __m21 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4329
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4330
        // ## control/route.js  
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4331
        // _Controller route integration._
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4332
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4333
        can.Control.processors.route = function(el, event, selector, funcName, controller) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4334
            selector = selector || "";
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4335
            can.route(selector);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4336
            var batchNum,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4337
                check = function(ev, attr, how) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4338
                    if (can.route.attr('route') === (selector) &&
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4339
                        (ev.batchNum === undefined || ev.batchNum !== batchNum)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4340
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4341
                        batchNum = ev.batchNum;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4342
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4343
                        var d = can.route.attr();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4344
                        delete d.route;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4345
                        if (can.isFunction(controller[funcName])) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4346
                            controller[funcName](d);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4347
                        } else {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4348
                            controller[controller[funcName]](d);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4349
                        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4350
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4351
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4352
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4353
            can.route.bind('change', check);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4354
            return function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4355
                can.route.unbind('change', check);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4356
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4357
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4358
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4359
        return can;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4360
    })(__m3, __m19, __m18);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4361
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4362
    // ## can/util/object/object.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4363
    var __m23 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4364
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4365
        var isArray = can.isArray,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4366
            // essentially returns an object that has all the must have comparisons ...
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4367
            // must haves, do not return true when provided undefined
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4368
            cleanSet = function(obj, compares) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4369
                var copy = can.extend({}, obj);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4370
                for (var prop in copy) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4371
                    var compare = compares[prop] === undefined ? compares["*"] : compares[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4372
                    if (same(copy[prop], undefined, compare)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4373
                        delete copy[prop]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4374
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4375
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4376
                return copy;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4377
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4378
            propCount = function(obj) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4379
                var count = 0;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4380
                for (var prop in obj) count++;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4381
                return count;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4382
            };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4383
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4384
        can.Object = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4385
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4386
        var same = can.Object.same = function(a, b, compares, aParent, bParent, deep) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4387
            var aType = typeof a,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4388
                aArray = isArray(a),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4389
                comparesType = typeof compares,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4390
                compare;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4391
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4392
            if (comparesType == 'string' || compares === null) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4393
                compares = compareMethods[compares];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4394
                comparesType = 'function'
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4395
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4396
            if (comparesType == 'function') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4397
                return compares(a, b, aParent, bParent)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4398
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4399
            compares = compares || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4400
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4401
            if (a instanceof Date) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4402
                return a === b;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4403
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4404
            if (deep === -1) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4405
                return aType === 'object' || a === b;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4406
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4407
            if (aType !== typeof b || aArray !== isArray(b)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4408
                return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4409
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4410
            if (a === b) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4411
                return true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4412
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4413
            if (aArray) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4414
                if (a.length !== b.length) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4415
                    return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4416
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4417
                for (var i = 0; i < a.length; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4418
                    compare = compares[i] === undefined ? compares["*"] : compares[i]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4419
                    if (!same(a[i], b[i], a, b, compare)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4420
                        return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4421
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4422
                };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4423
                return true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4424
            } else if (aType === "object" || aType === 'function') {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4425
                var bCopy = can.extend({}, b);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4426
                for (var prop in a) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4427
                    compare = compares[prop] === undefined ? compares["*"] : compares[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4428
                    if (!same(a[prop], b[prop], compare, a, b, deep === false ? -1 : undefined)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4429
                        return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4430
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4431
                    delete bCopy[prop];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4432
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4433
                // go through bCopy props ... if there is no compare .. return false
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4434
                for (prop in bCopy) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4435
                    if (compares[prop] === undefined || !same(undefined, b[prop], compares[prop], a, b, deep === false ? -1 : undefined)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4436
                        return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4437
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4438
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4439
                return true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4440
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4441
            return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4442
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4443
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4444
        can.Object.subsets = function(checkSet, sets, compares) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4445
            var len = sets.length,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4446
                subsets = [],
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4447
                checkPropCount = propCount(checkSet),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4448
                setLength;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4449
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4450
            for (var i = 0; i < len; i++) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4451
                //check this subset
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4452
                var set = sets[i];
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4453
                if (can.Object.subset(checkSet, set, compares)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4454
                    subsets.push(set)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4455
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4456
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4457
            return subsets;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4458
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4459
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4460
        can.Object.subset = function(subset, set, compares) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4461
            // go through set {type: 'folder'} and make sure every property
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4462
            // is in subset {type: 'folder', parentId :5}
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4463
            // then make sure that set has fewer properties
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4464
            // make sure we are only checking 'important' properties
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4465
            // in subset (ones that have to have a value)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4466
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4467
            var setPropCount = 0,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4468
                compares = compares || {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4469
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4470
            for (var prop in set) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4471
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4472
                if (!same(subset[prop], set[prop], compares[prop], subset, set)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4473
                    return false;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4474
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4475
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4476
            return true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4477
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4478
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4479
        var compareMethods = {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4480
            "null": function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4481
                return true;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4482
            },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4483
            i: function(a, b) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4484
                return ("" + a).toLowerCase() == ("" + b).toLowerCase()
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4485
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4486
        }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4487
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4488
        return can.Object;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4489
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4490
    })(__m3);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4491
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4492
    // ## can/observe/backup/backup.js
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4493
    var __m22 = (function(can) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4494
        var flatProps = function(a) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4495
            var obj = {};
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4496
            for (var prop in a) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4497
                if (typeof a[prop] !== 'object' || a[prop] === null || a[prop] instanceof Date) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4498
                    obj[prop] = a[prop]
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4499
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4500
            }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4501
            return obj;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4502
        };
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4503
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4504
        can.extend(can.Observe.prototype, {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4505
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4506
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4507
                backup: function() {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4508
                    this._backupStore = this._attrs();
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4509
                    return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4510
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4511
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4512
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4513
                isDirty: function(checkAssociations) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4514
                    return this._backupStore && !can.Object.same(this._attrs(),
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4515
                        this._backupStore,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4516
                        undefined,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4517
                        undefined,
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4518
                        undefined, !! checkAssociations);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4519
                },
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4520
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4521
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4522
                restore: function(restoreAssociations) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4523
                    var props = restoreAssociations ? this._backupStore : flatProps(this._backupStore)
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4524
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4525
                    if (this.isDirty(restoreAssociations)) {
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4526
                        this._attrs(props);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4527
                    }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4528
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4529
                    return this;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4530
                }
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4531
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4532
            })
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4533
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4534
        return can.Observe;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4535
    })(__m3, __m7, __m23);
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4536
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4537
    window['can'] = __m5;
80c32ef237c6 Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
  4538
})();