johnhamelink
12/2/2013 - 2:59 PM

make_badges.js

var BadgeMaker;

Array.prototype.compact = function() {
  return this.filter(function(n) {
    return n;
  });
};

$(function() {
  return $('.menu-inner').accordion({
    heightStyle: "content",
    collapsible: true
  });
});

BadgeMaker = angular.module('BadgeMaker', []);

BadgeMaker.controller('MakeBadgesCtrl', [
  '$scope', '$http', '$timeout', function($scope, $http, $timeout) {
    $scope.reload = function() {
      return location.reload();
    };
    $scope.reset = function() {
      return $scope.badge = {
        finished: false,
        uid: "",
        category: 0,
        colour: "00",
        customisation: [],
        name: "",
        level: "",
        description: "",
        icon: ""
      };
    };
    $scope.reset();
    $scope.swatch_scaffolding = function() {
      if (typeof $scope.swatches_cache !== 'object') {
        $http.get("/swatches.json").success(function(data) {
          var swatch;
          return $scope.swatches_cache = [
            (function() {
              var _i, _len, _results;
              _results = [];
              for (_i = 0, _len = data.length; _i < _len; _i++) {
                swatch = data[_i];
                _results.push(swatch[0]);
              }
              return _results;
            })()
          ][0];
        });
      }
      return $scope.swatches_cache;
    };
    $scope.icon_scaffolding = function() {
      if (typeof $scope.icons_cache !== 'object') {
        $http.get("/icons.json").success(function(data) {
          return $scope.icons_cache = data[0];
        });
      }
      return $scope.icons_cache;
    };
    $scope.scaffolding_requested = false;
    $scope.scaffolding = function() {
      if (!$scope.scaffolding_requested) {
        $scope.scaffolding_requested = true;
        $http.get("/scaffolding.json").success(function(data) {
          var scaffold;
          return $scope.scaffolding_cache = [
            (function() {
              var _i, _len, _results;
              _results = [];
              for (_i = 0, _len = data.length; _i < _len; _i++) {
                scaffold = data[_i];
                _results.push(scaffold[0][0]);
              }
              return _results;
            })()
          ][0];
        }).error(function(data, status, headers, config) {
          return $scope.scaffolding_requested = false;
        });
      }
      return $scope.scaffolding_cache || [];
    };
    $scope.categories = function() {
      var shape, _i, _len, _ref;
      if (typeof $scope.categories_cache !== 'object' || Object.keys($scope.categories_cache).length === 0) {
        $scope.categories_cache = [];
        _ref = $scope.scaffolding();
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          shape = _ref[_i];
          $scope.categories_cache.push(shape[0][0]);
        }
        if (!$scope.badge.customisation[0]) {
          $scope.badge.customisation[0] = $scope.categories_cache[$scope.badge.category].char;
        }
      }
      return $scope.categories_cache;
    };
    $scope.customisations = function() {
      if ("" === $scope.badge.category) {
        return [];
      }
      return $scope.scaffolding()[$scope.badge.category];
    };
    $scope.icons = function() {
      if ("" === $scope.badge.colour) {
        return [];
      }
      return $scope.icon_scaffolding();
    };
    $scope.base_thumbnail = function(attributes) {
      while (void 0 === attributes['thumb'] || void 0 === attributes) {
        attributes = attributes[0];
      }
      return attributes['thumb'];
    };
    $scope.colours = function() {
      if ("" === $scope.badge.category) {
        return [];
      }
      return $scope.swatch_scaffolding();
    };
    $scope.badge_changed = function() {
      if (JSON.stringify($scope.badge) === $scope.badge_old) {
        return false;
      }
      $scope.badge_old = JSON.stringify($scope.badge);
      return true;
    };
    $scope.badge_preview_url = function() {
      if ($scope.badge_changed()) {
        $scope.badge_preview_url_cache = "/b/" + [$scope.badge.colour, $scope.badge.customisation.join('-').replace(/-+/g, "-"), encodeURIComponent($scope.badge.name || " "), encodeURIComponent($scope.badge.level || " "), $scope.badge.icon].join('/');
      }
      return $scope.badge_preview_url_cache;
    };
    $scope.badge_url = "";
    $scope.save = function() {
      $scope.badge.finished = true;
      $scope.badge_url = $scope.badge_preview_url();
      return $http({
        method: "POST",
        url: "/save",
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: $.param({
          path: $scope.badge_url,
          title: $scope.badge.name,
          level: $scope.badge.level,
          description: $scope.badge.description
        })
      }).success(function(data) {
        return $scope.badge.uid = data;
      });
    };
    $scope.shared_by_email = false;
    $scope.share_email = function() {
      return $http({
        method: "POST",
        url: "/share/email",
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: $.param({
          image_link: $scope.badge_url,
          email: $scope.share_email_address
        })
      }).success(function(data) {
        return $scope.shared_by_email = true;
      });
    };
    $scope.shared_on_twitter = false;
    return $scope.share_twitter = function() {
      return $http({
        method: "POST",
        url: "/share/twitter",
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: $.param({
          link: $scope.badge_url,
          handle: $scope.share_twitter_handle
        })
      }).success(function(data) {
        return $scope.shared_on_twitter = true;
      });
    };
  }
]);