cirandas.net

ref: master

plugins/orders_cycle/public/javascripts/orders_cycle.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
orders_cycle = {

  cycle: {

    edit: {
      openingMessage: {
        onKeyup: function(textArea) {
          textArea = $(textArea)
          var checked = textArea.val() ? true : false;
          var checkBox = textArea.parents('#cycle-new-mail').find('input[type=checkbox]')
          checkBox.prop('checked', checked)
        },
      },
    },

    products: {
      load_url: null,

      load: function () {
        $.get(orders_cycle.cycle.products.load_url, function(data) {
          if (data.length > 10)
            $('#cycle-products .table').html(data)
          else
            setTimeout(orders_cycle.cycle.products.load, 5*1000);
        });

      },
    },
  },

  /* ----- cycle ----- */

  in_cycle_order_toggle: function (context) {
    container = $(context).hasClass('cycle-orders') ? $(context) : $(context).parents('.cycle-orders');
    container.toggleClass('show');
    container.find('.order-content').toggle();
    sortable_table.edit_arrow_toggle(container);
  },

  /* ----- order ----- */

  order: {

    load: function() {
      $('html').click(function(e) {
        $('.popover').remove()
      })
    },

    reload_order_items: function(order) {
      $('order-items-table')[0]._tag.updateOrder(order)
    },
    product: {
      include_message: '',
      order_id: 0,
      redirect_after_include: '',
      add_url: '',
      remove_url: '',
      balloon_url: '',

      load: function (id, state) {
        var product = $('#cycle-product-'+id);
        product.toggleClass('in-order', state);
        product.find('input').get(0).checked = state;
        toggle_edit.value_row.reload();
        return product;
      },

      showMore: function (url) {
        $.get(url, function (data) {
          var newProducts = $(data).filter('#cycle-products').find('.table-content').children()
          $('.pagination').replaceWith(newProducts)
          pagination.loading = false
        })
      },

      click: function (event, id) {
        // was this a child click?
        if (event != null && event.target != this && event.target.onclick)
          return;

        var product = $('#cycle-product-'+id);
        if (! product.hasClass('editable'))
          return;

        var state = !product.hasClass('in-order');
        if (state == true)
          this.add(id);
        else 
          this.remove(id);
        product.find('input').get(0).checked = state;
      },

      setEditable: function (editable) {
        $('.order-cycle-product').toggleClass('editable', editable)
        if (editable)
          $('.order-cycle-product #product_ids_').removeAttr('disabled')
        else
          $('.order-cycle-product #product_ids_').attr('disabled', 'disabled')
      },

      add: function (id) {
        var product = this.load(id, true);

        if (this.include_message)
          alert(this.include_message);

        loading_overlay.show(product);

        $.post(this.add_url, {order_id: this.order_id, redirect: this.redirect_after_include, offered_product_id: id},
          function () { loading_overlay.hide(product); }, 'script')
          .always( function () { loading_overlay.hide(product); } );
      },

      remove: function (id) {
        var product = this.load(id, false);

        loading_overlay.show(product);
        $.post(this.remove_url, {id: id, order_id: this.order_id}, function (order) {
          loading_overlay.hide(product);
        }, 'script');
      },

      supplier: {
        balloon_url: '',

        balloon: function (id) {
          var product = $('#cycle-product-'+id)
          var target = product.find('.supplier')
          var supplier_id = product.attr('supplier-id')
          $.get(this.balloon_url+'/'+supplier_id, function(data) {
            var html = $(data)
            var title = orders_cycle.order.product.balloon_title(html)
            // use container to avoid conflict with row click
            var options = {html: true, content: html, container: 'body', title: title}
            target.popover(options).popover('show')
          })
        },
      },

      balloon: function (id) {
        var product = $('#cycle-product-'+id);
        var target = product.find('.product');
        $.get(this.balloon_url+'/'+id, function(data) {
          var html = $(data)
          var title = orders_cycle.order.product.balloon_title(html)
          // use container to avoid conflict with row click
          var options = {html: true, content: html, container: 'body', title: title}
          target.popover(options).popover('show')
        })
      },

      balloon_title: function(content) {
        var titleElement = $(content).find('.popover-title')
        var title = titleElement.html()
        titleElement.hide()
        return title
      },
    }, // product
  }, // order

  /* ----- cycle editions ----- */

  offered_product: {

    pmsync: function (context, to_price) {
      p = $(context).parents('.cycle-product .box-edit');
      margin = p.find('#product_margin_percentage');
      price = p.find('#product_price');
      buy_price = p.find('#product_buy_price');
      original_price = p.find('#product_original_price');
      base_price = unlocalize_currency(buy_price.val()) ? buy_price : original_price;

      if (to_price)
        suppliers.price.calculate(price, margin, base_price);
      else
        suppliers.margin.calculate(margin, price, base_price);
    },

    edit: function () {
      toggle_edit.editing().find('.box-edit').toggle(toggle_edit.isEditing());
    },
  },

  /* ----- toggle edit ----- */

  cycle_mail_message_toggle: function () {
    if ($('#cycle-new-mail-send').prop('checked')) {
      $('#cycle-new-mail').removeClass('disabled');
      $('#cycle-new-mail textarea').removeAttr('disabled');
    } else {
      $('#cycle-new-mail').addClass('disabled');
      $('#cycle-new-mail textarea').attr('disabled', true);
    }
  },

  ajaxifyPagination: function(selector) {
    $(selector).find(".pagination a").click(function() {
      loading_overlay.show(selector);
      $.ajax({
        type: "GET",
        url: $(this).attr("href"),
        dataType: "script"
      });
      return false;
    });
  },

  toggleCancelledOrders: function () {
    $('.consumers-coop #show-cancelled-orders a').toggle();
    $('.consumers-coop #hide-cancelled-orders a').toggle();
    $('.consumers-coop .consumer-order.cancelled').not('.comsumer-order.active-order').toggle();
  },

};