/** * Define the angular app */ 'use strict'; var ObjectStorage = require("js/storage"); var Helpers = require("js/helpers"); var GameObjects = require("js/gameobjects"); var analytics = require("js/analytics"); var Game = require("js/game"); var Rules = require("js/rules.js"); var app = (function (Helpers,analytics,Game,Rules) { Helpers.validateSaveVersion(); var app = angular.module('scienceAlchemy', ['ngDragDrop','ngAnimate']); // directives /** * Make little "+2" "-1" score animations when score changes requires ng-model="score" * Associated css: * ``` * .update-value { // set constant height, and the position position: relative; right: -2em; top: -1.42857em; height: 1.42857em; } .update-plus { // if the change is +ve color: green; position: relative; } .update-minus { color: red; position: relative; } */ function cfsScoreChange($compile) { return { link: function (scope, element, attrs) { scope.$watch(attrs.ngModel, function (newValue, oldValue) { // showUpdateValue var num = newValue-oldValue; var formatted = Helpers.formatNumberPostfix(num); var insert; if (num > 0) { insert = angular.element("
") .attr("class", "update-plus") .html("+" + formatted); } else { insert = angular.element("
") .attr("class", "update-minus") .html(formatted); } // TODO it would be better to use an ::after element for this // showUpdate element.append(insert); insert.animate({ "bottom":"+=30px", "opacity": 0 }, { duration: 500, complete: function() { angular.element(this).remove(); }}); }); } }; }; cfsScoreChange.$inject = ['$compile']; app.directive('cfsScoreChange', cfsScoreChange); /** * Directive to render a rule and bind it's option with select boxes * This expects ng-model="rule" as an attribute */ function cfsRule($compile) { return { link: function (scope, element, attrs) { var rule = scope.$eval(attrs.ngModel); // first generate a select box for each option (using lodash templating) _.templateSettings.interpolate = /<%=([\s\S]+?)%>/g; var optionTmpl = '' + '\n'; var tmplParams = _.defaults({},rule.options,rule.otherOptions); for (var option in rule.optionDesc) { if (rule.optionDesc.hasOwnProperty(option)) { var vals = rule.optionDesc[option].possibleVals; if (vals) { tmplParams[option] = _.template(optionTmpl)({ option: option }); } else { // if there are no options replace '{{color}}' with 'color' tmplParams[option] = option; } } } // now put each select box into description // replace '{{color}}' with '