OPiQuotations  v.03.00.00 — April 5, 2019
one-OPiQuotation.js
Go to the documentation of this file.
1 /* -*- coding: utf-8 -*- */
2 /** \file one-OPiQuotation.js
3  * (September 11, 2016)
4  *
5  * \brief
6  * JavaScript functions to open/close AJAX sharing panel.
7  *
8  * Piece of OPiQuotations.
9  * https://bitbucket.org/OPiMedia/opiquotations
10  *
11  * GPLv3 --- Copyright (C) 2014, 2015, 2016 Olivier Pirson
12  * http://www.opimedia.be/
13  */
14 
15 /**
16  * \brief
17  * Close this sharing panel.
18  *
19  * @param: DOM liCloseItem
20  */
21 function sharingClose(liCloseItem) {
22  "use strict";
23 
24  console.assert(liCloseItem instanceof HTMLElement, liCloseItem);
25 
26  var ul = liCloseItem.parentNode;
27 
28  ul.parentNode.removeChild(ul);
29 }
30 
31 
32 /**
33  * \brief
34  * Open the sharing panel for the quotation id
35  * in a <ul> sibling of item.
36  *
37  * @param DOM item
38  * @param integer id > 0
39  */
40 function sharingOpen(item, id) {
41  "use strict";
42 
43  console.assert(item instanceof HTMLElement, item);
44  console.assert(Number.isInteger(id), id);
45  console.assert(id > 0, id);
46 
47  if (!item || !id || (id <= 0)) {
48  return;
49  }
50 
51  var parent = item.parentNode;
52 
53  // If panel already open, then remove sibling items of item
54  if (parent.children.length > 1) {
55  while (parent.children.length > 1) {
56  parent.removeChild(parent.children[1]);
57  }
58 
59  return;
60  }
61 
62  // Create <ul> and fill it in AJAX
63  var ul = document.createElement("ul");
64 
65  ul.setAttribute("aria-haspopup", "true");
66 
67  ul.innerHTML = "<li>Chargement&hellip;</li>";
68  parent.appendChild(ul);
69 
70  var xhr = new XMLHttpRequest();
71 
72  xhr.open("GET", "OPiQuotations-sharing.php?id=" + id, true);
73  xhr.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
74 
75  xhr.onreadystatechange = function() { // callback
76  if ((xhr.readyState !== 4) || (xhr.status !== 200 || !xhr.responseText)) {
77  return;
78  }
79  ul.innerHTML = xhr.responseText;
80  };
81 
82  xhr.send(null);
83 }