cirandas.net

ref: master

plugins/currency/public/javascripts/currency.js


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
currency = {
  list: null,

  find: function(id) {
    id = parseInt(id);
    return currency.list.find(function(c) { return c.id == id; });
  },
};

currency.search = {

  load: function() {
    this.input = jQuery();
    this.getResults = function(data) {};
    this.query = null;
    this.last_query = null;
    this.query_url = null;
    this.typing = false;
    this.pending = false;
    this.timeout = 200;
  },

  do: function (input, url, getResults) {
    this.input = jQuery(input);
    this.typing = true;
    this.query = input.value;
    this.query_url = url;
    this.getResults = getResults;
    setTimeout(this.expire, this.timeout);
  },

  start: function () {
    if (!this.query || (this.last_query && this.query == this.last_query))
      return;
    this.pending = false;
    this.typing = false;
    this.last_query = this.query;
    this.pending = true;
    this.input.addClass('loading');
    this.getResults();
  },
  finish: function () {
    this.pending = false;
    this.input.removeClass('loading');
  },

  expire: function () {
    currency.search.start();
  },
};
currency.search.load();

currency.popin = {
  current: jQuery(),

  show: function (parent) {
    var popin = jQuery(parent).find('.currency-popin');
    currency.popin.current.hide();
    currency.popin.current = popin;
    popin.addClass('current');
    popin.show();
    return popin;
  },
  close: function (button) {
    var popin = jQuery(button).parents('.currency-popin');
    popin.removeClass('current');
    popin.hide();
  },
};

currency.disassociate = {

  search: function () {
    jQuery.get(currency.search.query_url, {query: currency.search.query}, function (data) {
      jQuery('#enterprise-results').html(data);
      currency.search.finish();
    });
  },
};

currency.accept = {

  load: function (hint) {
    input = jQuery('#search-query');
    input.hint(hint);
  },

  search: function () {
    jQuery.get(currency.search.query_url, {query: currency.search.query}, function (data) {
      jQuery('#currency-search').html(data);
      currency.search.finish();
    });
  },
};

currency.product = {

  priceRow: null,
  discountRow: null,

  setId: function (pc) {
    pc.id = pc.currency_id;
    return pc;
  },

  load: function(options) {
    currency.list = options.currencies;
    currency.product.priceRow = jQuery('#price-row');
    currency.product.discountRow = jQuery('#discount-row');

    currency.product.add('price', options.prices.map(currency.product.setId));
    currency.product.add('discount', options.discounts.map(currency.product.setId));

    jQuery('#price-currency-select').change(function () {
      var item = currency.find(jQuery(this).val());
      currency.product.add('price', [item]);
    });
    jQuery('#discount-currency-select').change(function () {
      var item = currency.find(jQuery(this).val());
      currency.product.add('discount', [item]);
    });
  },

  add: function(field, currencies) {
    currency.product[field+'Row'].after(currency.product.template(field, currencies));
  },

  template: function (field, currencies) {
    var template = jQuery('#currency-template');
    return _.template(template.html(), {field: field, currencies: currencies});
  },
};

// don't strip underscore templates within ruby templates
String.prototype.stripScripts = function () { return this; };

// sync get
jQuery.syncGet = function (url, data, success, dataType) {
  jQuery.ajax({async: false, method: 'GET', url: url,
    data: data, success: success, dataType: dataType});
};

window.addedScripts = window.addedScripts || [];
function addScript(src, onload) {
  if (window.addedScripts.indexOf(src) != -1)
    return;
  window.addedScripts.push(src);
  jQuery.syncGet(src, {}, function(js) { jQuery.globalEval(js); });
  if (onload != undefined)
    onload();
}

currency.underscore_settings = function () {
  // underscore use of <@ instead of <%
  _.templateSettings = {
    interpolate: /\<\@\=(.+?)\@\>/gim,
    evaluate: /\<\@(.+?)\@\>/gim
  };
};

// from http://stackoverflow.com/questions/17033397/javascript-strings-with-keyword-parameters
String.prototype.format = function(obj) {
  return this.replace(/%\{([^}]+)\}/g,function(_,k){ return obj[k] });
};