Skip to content
Snippets Groups Projects
Select Git revision
  • 9852f8312949192f3963eaa6b4061f542a508e56
  • main default protected
2 results

Shortcut.pm

Blame
  • content.js 34.50 KiB
    /*jslint eqeq: true, forin: true, nomen: true*/
    /*jshint unused:false, loopfunc:true */
    /*global _, MAPJS, observable*/
    MAPJS.content = function (contentAggregate, sessionKey) {
      'use strict';
      var cachedId,
          invalidateIdCache = function () {
            cachedId = undefined;
          },
          maxId = function maxId(idea) {
            idea = idea || contentAggregate;
            if (!idea.ideas) {
              return parseInt(idea.id, 10) || 0;
            }
            return _.reduce(
                idea.ideas,
                function (result, subidea) {
                  return Math.max(result, maxId(subidea));
                },
                parseInt(idea.id, 10) || 0
            );
          },
          nextId = function nextId(originSession) {
            originSession = originSession || sessionKey;
            if (!cachedId) {
              cachedId = maxId();
            }
            cachedId += 1;
            if (originSession) {
              return cachedId + '.' + originSession;
            }
            return cachedId;
          },
          init = function (contentIdea, originSession) {
            if (!contentIdea.id) {
              contentIdea.id = nextId(originSession);
            } else {
              invalidateIdCache();
            }
            if (contentIdea.ideas) {
              _.each(contentIdea.ideas, function (value, key) {
                contentIdea.ideas[parseFloat(key)] = init(value, originSession);
              });
            }
            if (!contentIdea.title) {
              contentIdea.title = '';
            }
            contentIdea.containsDirectChild = contentIdea.findChildRankById = function (childIdeaId) {
              return parseFloat(
                  _.reduce(
                      contentIdea.ideas,
                      function (res, value, key) {
                        return value.id == childIdeaId ? key : res;
                      },
                      undefined
                  )
              );
            };
            contentIdea.findSubIdeaById = function (childIdeaId) {
              var myChild = _.find(contentIdea.ideas, function (idea) {
                return idea.id == childIdeaId;
              });
              return myChild || _.reduce(contentIdea.ideas, function (result, idea) {
                    return result || idea.findSubIdeaById(childIdeaId);
                  }, undefined);
            };
            contentIdea.find = function (predicate) {
              var current = predicate(contentIdea) ? [_.pick(contentIdea, 'id', 'title')] : [];
              if (_.size(contentIdea.ideas) === 0) {
                return current;