From 263a2f3727a000955942a4f3449dd50d76851977 Mon Sep 17 00:00:00 2001
From: Thomas NOEL <tnoel@entrouvert.com>
Date: Fri, 22 Feb 2019 12:13:23 +0100
Subject: [PATCH] portal-agent: add phone-calls cells (#30827)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

  "JSON_CELL_TYPES": {
    "phone-calls": {
      "url": "{% if callee %}{{passerelle_url}}phonecalls/{{system}}/calls?callee={{callee}}{% endif %}",
      "name": "Appels téléphoniques",
      "force_async": false,
      "varnames": ["callee"],
      "cache_duration": 0,
      "form": [
        {"label": "Slug du connecteur", "varname": "system", "required": true}
      ],
      "actions": {
        "refresh": {
           "url": "{{passerelle_url}}phonecalls/{{system}}/calls?callee={{ callee }}",
           "method": "GET"
        }
      }
    }
---
 .../portal-agent/combo/json/phone-calls.html  | 100 ++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 templates/portal-agent/combo/json/phone-calls.html

diff --git a/templates/portal-agent/combo/json/phone-calls.html b/templates/portal-agent/combo/json/phone-calls.html
new file mode 100644
index 00000000..e6d1550d
--- /dev/null
+++ b/templates/portal-agent/combo/json/phone-calls.html
@@ -0,0 +1,100 @@
+{% load combo %}
+<h2>Appels{% if callee %} (ligne {{callee}}){% endif %}</h2>
+
+<div id="calls" data-calls="{{ json|as_json }}">
+{% if json %}  {# callee has been set #}
+<strong>Appel en cours</strong>
+{% for call in json.data.current %}
+{% if forloop.first %}
+<script>
+$(function() {
+  var $search = $('.combo-search-input');
+  if (! $search.is(':focus')) {
+      $search.val('{{ call.caller }}').trigger('change');
+  }
+});
+</script>
+{% endif %}
+<div class="caller" data-caller="{{ call.caller }}">
+  {{call.start|parse_datetime|date:"H\hi"}} : {{ call.caller }}
+</div>
+{% empty %}
+<div>Pas d'appel en cours</div>
+{% endfor %}
+
+{% for call in json.data.past|slice:":10" %}
+{% if forloop.first %}<strong>Appels précédents</strong>{% endif %}
+<div class="caller" data-caller="{{ call.caller }}">
+  {{call.start|parse_datetime|date:"H\hi"}} : {{ call.caller }}
+</div>
+{% endfor %}
+{% endif %}
+</div>
+
+<div>
+  <form>
+    {% if not callee %}
+    <div class="infonotice">
+      Indiquer le numéro de ligne à suivre.
+    </div>
+    {% endif %}
+    <input {% if callee %}type="hidden"{% else %}type="text"{% endif %} name="callee" value="{{ callee }}">
+    {% if not callee %}<button>✓</button>{% endif %}
+  </form>
+  {% if callee %}<button class="release-line">Ne plus suivre la ligne {{callee}}</button>{% endif %}
+</div>
+
+<script>
+$(function() {
+  $('div.caller').on('click', function(event) {
+    var $search = $('.combo-search-input');
+    $search.val($(event.target).data('caller')).trigger('change');
+  });
+  var $cell = $('.configjsoncell.phone-calls');
+  {% if not callee %}
+  var callee = window.localStorage.publik_phone_callee;
+  if (callee) {
+    window.history.replaceState({}, "", "?callee=" + callee);
+    combo_load_cell($cell);
+  }
+  {% else %}
+  window.localStorage.publik_phone_callee = "{{ callee }}";
+  if (window.location.search != "?callee={{ callee }}") {
+    window.history.replaceState({}, "", "?callee={{ callee }}");
+  }
+  var current_calls = $cell.find('#calls').data('calls');
+  function get_caller(calls) {
+    if (calls && calls.data && calls.data.current && calls.data.current.length) return calls.data.current[0].caller;
+    return null;
+  }
+  function refresh_calls() {
+    $.ajax({url: $cell.data('ajax-cell-url') + '?callee={{ callee }}',
+            type: 'POST',
+            data: {'action': 'refresh'},
+            success: function(data) {
+              new_content = $('<div>' + data + '</div>');
+              var new_calls = $(new_content).find('#calls').data('calls');
+              var new_caller = get_caller(new_calls);
+              if (get_caller(current_calls) != new_caller) {
+                current_calls = new_calls;
+                $('#calls').replaceWith($(new_content).find('#calls'));
+                if (new_caller) {
+                  var $search = $('.combo-search-input');
+                  if (! $search.is(':focus')) {
+                    $search.val(new_caller).trigger('change');
+                  }
+                }
+              }
+            }
+     });
+  }
+  setInterval(refresh_calls, 2500);
+  $('button.release-line').on('click', function() {
+    window.localStorage.publik_phone_callee = "";
+    window.history.replaceState({}, "", "?callee=");
+    combo_load_cell($cell);
+    return false;
+  });
+  {% endif %}
+});
+</script>
-- 
2.20.1

