Projet

Général

Profil

0001-add-template-for-FranceConnect-connector-item-field-.patch

Benjamin Dauvergne, 27 mai 2021 11:11

Télécharger (6,55 ko)

Voir les différences:

Subject: [PATCH] add template for FranceConnect connector item field (#54003)

 .../widgets/select_jsonp--franceconnect.html  | 159 ++++++++++++++++++
 1 file changed, 159 insertions(+)
 create mode 100644 templates/qommon/forms/widgets/select_jsonp--franceconnect.html
templates/qommon/forms/widgets/select_jsonp--franceconnect.html
1
{% extends "qommon/forms/widgets/select_jsonp.html" %}
2
{% load i18n %}
3
{% block widget-control %}
4
<style>
5
  div.gru-content .franceconnect--button {
6
      background: #034ea2;
7
      width: 300px;
8
      display: block;
9
  }
10
  .franceconnect--image {
11
      float: left;
12
      width: 100px;
13
      height: 100px;
14
      display: block;
15
  }
16
  .franceconnect--data {
17
      display: none;
18
  }
19
  .franceconnect--caption {
20
      float: right;
21
      display: block;
22
      width: 150px;
23
      font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
24
      font-weight: 100;
25
      text-align: center;
26
  }
27
  .franceconnect--text {
28
      width: calc(100% - 3em);
29
  }
30
  .franceconnect--clear {
31
      float: right;
32
  }
33
</style>
34
<div class="wcs-widget-select2-container">
35
  <button class="franceconnect--button" >
36
    <img class="franceconnect--image" src="https://fcp.integ01.dev-franceconnect.fr/images/fc-avatar.png"/>
37
    <span class="franceconnect--caption">Valider mon identité avec FranceConnect</span>
38
  </button>
39
  <div class="franceconnect--result" data-initial-display-value="{{widget.get_display_value|default_if_none:''}}">
40
    <span class="franceconnect--value"><span>
41
  </div>
42
  <div class="franceconnect--data">
43
    <input class="franceconnect--input" type="hidden" id="form_{{widget.name}}" name="{{widget.name}}" {% if widget.value %}value="{{ widget.value }}" {% endif %}data-select2-url="{{widget.get_select2_url}}" data-initial-display-value="{{widget.get_display_value|default_if_none:''}}"/>
44
    <input class="franceconnect--text" readonly/>
45
    <span class="franceconnect--clear"><a>&#128465;</a></span>
46
  </div>
47
</div>
48
<script>
49
$(function () {
50
    const popup = function(url, title, w, h) {
51
        // Method to open a centered popup on desktop browsers using window.open(),
52
        // it's useless on mobile which will always open a tab.
53
        // I don't know if I can force opening a tab on desktop too.
54

  
55
        const dualScreenLeft = window.screenLeft !==  undefined ? window.screenLeft : window.screenX;
56
        const dualScreenTop = window.screenTop !==  undefined   ? window.screenTop  : window.screenY;
57

  
58
        const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
59
        const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
60

  
61
        const systemZoom = width / window.screen.availWidth;
62
        const left = (width - w) / 2 / systemZoom + dualScreenLeft
63
        const top = (height - h) / 2 / systemZoom + dualScreenTop
64
        const popup = window.open(url, title, 
65
          `
66
          scrollbars=yes,
67
          width=${w / systemZoom}, 
68
          height=${h / systemZoom}, 
69
          top=${top}, 
70
          left=${left}
71
          `
72
        )
73

  
74
        if (window.focus) popup.focus();
75
        return popup;
76
    };
77

  
78
   const $input = $('#form_{{widget.name}}');
79
   const $input_display_value = $('<input>', {
80
          type: 'hidden',
81
          name: $input.attr('name') + '_display',
82
          value: $input.data('initial-display-value')
83
   });
84
   $input_display_value.insertAfter($input);
85
   const $container = $input.parents('.wcs-widget-select2-container');
86
   const ds_url = $input.data('select2-url');
87
   const $button = $container.find('button');
88

  
89
   if ($input.val() != '') {
90
       $container.find('.franceconnect--text').val($input.data('initial-display-value'));
91
       $container.find('.franceconnect--data').show();
92
       $container.find('.franceconnect--button').hide();
93
   }
94

  
95
   var popup_window = null;
96

  
97
   /* Button to reset the data and show the FC button again. */
98
   $container.find('.franceconnect--clear').on('click', function () {
99
       $container.find('.franceconnect--button').show();
100
       $input.val('');
101
       $input_display_value.val('');
102
       $container.find('.franceconnect--data').hide();
103
   });
104

  
105
   /* Use a query call to the fake data source to obtain the URL for the popup.
106
    * The `q` parameter is mandatory to respect the contract of the
107
    * Autocomplete w.c.s. API.
108
    */
109
   var service_origin = null;
110

  
111
   $.getJSON(ds_url + '?q=', function (response) {
112
       const init_request_url = response.data[0].init_request_url;
113
       service_origin = response.data[0].service_origin;
114

  
115
       $button.on('click', function (event) {
116
           event.preventDefault();
117
           if (popup_window) {
118
               try {
119
                   popup_window.close();
120
               } finally {
121
               }
122
           }
123
           if (init_request_url.indexOf('?') == -1) {
124
               url = init_request_url + '?origin=' + encodeURIComponent(window.location.origin);
125
           } else {
126
               url = init_request_url + '&origin=' + encodeURIComponent(window.location.origin);
127
           }
128
           popup_window = popup(url, 'FranceConnect', 1000, 670);
129
       });
130
   });
131

  
132
   /* React to response from the popup sent using window.opener.postMessage() :
133
    */
134
   $(window).on('message', function (event) {
135
       const origin = event.originalEvent.origin;
136

  
137
       if (origin != service_origin) {
138
           console.log('wrong origin', origin, service_origin);
139
           /* ignore message from wrong origin */
140
           return;
141
       }
142
       $container.find('.franceconnect--text').val(event.originalEvent.data.text);
143
       $input.val(event.originalEvent.data.id);
144
       $input_display_value.val(event.originalEvent.data.text);
145
       $container.find('.franceconnect--data').show();
146
       $container.find('.franceconnect--button').hide();
147

  
148
       if (popup_window) {
149
           try {
150
               popup_window.close();
151
           } finally {
152
           }
153
       }
154
       popup_window = null;
155
   });
156

  
157
});
158
</script>
159
{% endblock %}
0
-