Esempio n. 1
0
function dataValue(view, ctx, model, name, escape, forceEscape) {
  var macroPath, path, value;
  if (~name.indexOf('(')) {
    value = fnValue(view, ctx, model, name);
    return escapeValue(value, escape);
  }
  path = ctxPath(view, ctx, name);
  macroPath = macroName(view, ctx, path);
  if (macroPath) {
    if (macroPath.$matchName) {
      path = macroPath.$matchName;
    } else {
      value = lookup(macroPath, ctx.$macroCtx);
      if (typeof value === 'function') {
        if (value.unescaped && !forceEscape) return value(ctx, model);
        value = value(ctx, model);
      }
      return escapeValue(value, escape);
    }
  }
  value = lookup(path, ctx);
  if (value !== void 0) return escapeValue(value, escape);
  value = model.get(path);
  value = value !== void 0 ? value : model[path];
  return escapeValue(value, escape);
}
Esempio n. 2
0
function dataValue(view, ctx, model, name, macro) {
  var path, value;
  if (~name.indexOf('(')) {
    return fnValue(view, ctx, model, name, macro);
  }
  if (macro) {
    // Get macro content sections
    value = lookup(name.toLowerCase(), ctx.$macroCtx);
    if (value && !value.$matchName) {
      return typeof value === 'function' ? value(ctx, model) : value;
    }
  }
  path = ctxPath(ctx, name, macro);
  value = lookup(path, ctx);
  if (value !== void 0) return value;
  value = model.get(path);
  return value !== void 0 ? value : model[path];
}
Esempio n. 3
0
function macroName(ctx, name, noReplace) {
  var macroCtx = ctx.$macroCtx
    , path = ctxPath(macroCtx, name, false, noReplace)
    , segments = path.split('.')
    , base = segments[0].toLowerCase()
    , remainder = segments[1]
    , value = lookup(base, macroCtx)
    , matchName = value && value.$matchName;
  if (!matchName) return remainder ? base + '.' + remainder : base;
  return remainder ?
    (/\.+/.test(matchName) ? matchName.slice(1) : matchName) + '.' + remainder :
    matchName;
}
Esempio n. 4
0
  return function(ctx, model) {
    var value = dataValue(view, ctx, model, name, macro)
      , text = valueText(value);

    // TODO: DRY. This is duplicating logic in dataValue()
    if (macro) {
      value = lookup(name.toLowerCase(), ctx.$macroCtx);
      if (typeof value === 'function' && value.unescaped) {
        return text;
      }
    }
    return escape ? escape(text) : text;
  }
Esempio n. 5
0
function macroName(view, ctx, name) {
  if (name.charAt(0) !== '@') return;

  var macroCtx = ctx.$macroCtx
    , segments = name.slice(1).split('.')
    , base = segments.shift().toLowerCase()
    , remainder = segments.join('.')
    , value = lookup(base, macroCtx)
    , matchName = value && value.$matchName
  if (matchName) {
    if (!remainder) return value;
    return {$matchName: matchName + '.' + remainder};
  }
  return remainder ? base + '.' + remainder : base;
}
Esempio n. 6
0
  return function(ctx, model) {
    var value = dataValue(view, ctx, model, name, macro)
      , text = typeof value === 'string' ? value
          : value == null ? ''
          : value.toString === objectToString ? JSON.stringify(value)
          : value.toString();

    // TODO: DRY. This is duplicating logic in dataValue()
    if (macro) {
      value = lookup(name.toLowerCase(), ctx.$macroCtx);
      if (typeof value === 'function' && value.unescaped) {
        return text;
      }
    }
    return escape ? escape(text) : text;
  }
Esempio n. 7
0
  , fn: function() {
      var fnName, fn, fnCtxs, i, fnCtx;

      fnName = typeof fnObj === 'object'
        ? dataValue(view, ctx, view.model, fnObj.name, fnObj.macro)
        : fnName = fnObj;

      // If a placeholder for an event name does not have a value, do nothing
      if (!fnName) return listener.fn = empty;

      // See if it is a built-in function
      fn = dom.fns[fnName];

      // Lookup the function name on the component script or app

      // TODO: This simply looks in the local scope for the function
      // and then goes up the scope if a function name is not found.
      // Better would be to actually figure out the scope of where the
      // function name is specfied, since there could easily be namespace
      // conflicts between functions in a component and functions in an
      // app using that component. How to implement this correctly is not
      // obvious at the moment.
      if (!fn) {
        fnCtxs = ctx.$fnCtx;
        for (i = fnCtxs.length; i--;) {
          fnCtx = fnCtxs[i];
          fn = fnCtx[fnName] || lookup(fnName, fnCtx);
          if (fn) break;
        }
      }
      if (!fn) throw new Error('Bound function not found: ' + fnName);

      // Bind the listener to the app or component object on which it
      // was defined so that the `this` context will be the instance
      listener.fn = fn.bind(fnCtx);
      fn.apply(fnCtx, arguments);
    }
Esempio n. 8
0
 return name.replace(/\[([^\]]+)\]/g, function(match, name) {
   return lookup(name, ctx);
 });
Esempio n. 9
0
 , fn: function() {
     listener.fn = dom.fns[fnName] || fnCtx[fnName] || lookup(fnName, fnCtx);
     listener.fn.apply(null, arguments);
   }