Commit 4eb4efd5 authored by Eugene Shen's avatar Eugene Shen

Load contacts from Person module in ERP5

parent ef07ce8b
......@@ -228,12 +228,12 @@ div[data-gadget-scope='header'] .ui-header {
}
.contact-list li:hover {
font-weight: bold;
text-shadow: 1px 0 0 black;
cursor: pointer;
}
.contact-list li.notify {
font-weight: bold;
text-shadow: 1px 0 0 black;
color: red;
}
......
......@@ -6,45 +6,65 @@
<title>>Chat Room Gadget</title>
<script src="rsvp.js"></script>
<script src="renderjs.js"></script>
<script src="handlebars.js"></script>
<script src="gadget_erp5_chat_room.js"></script>
</head>
<body>
<h1>Choose Your Shared Storage</h1>
<form class="connect-form">
<script class="connect-form-template" type="text/x-handlebars-template">
<label>
<input type="radio" name="jio_type" value="erp5" required="required" />
<input type="radio" name="jio_type" value="erp5" required="required" {{#if jio_type_erp5}}checked="checked"{{/if}} />
ERP5,
</label>
<label>
URL:
<input type="text" name="erp5_url" placeholder="https://softinst75770.host.vifib.net/erp5/web_site_module/hateoas" />
<input type="text" name="erp5_url" value="{{erp5_url}}" />
</label>
<label>
<input type="radio" name="jio_type" value="dav" required="required" />
<input type="radio" name="jio_type" value="dav" required="required" {{#if jio_type_dav}}checked="checked"{{/if}} />
WebDAV,
</label>
<label>
URL:
<input type="text" name="dav_url" placeholder="https://softinst75722.host.vifib.net/share" />
<input type="text" name="dav_url" placeholder="{{dav_url}}" />
</label>
<label>
WebDAV Username:
<input type="text" name="dav_user" placeholder="eyqs" />
<input type="text" name="dav_user" />
</label>
<label>
WebDAV Password:
<input type="text" name="dav_pass" placeholder="Aoeuidhtns" />
<input type="text" name="dav_pass" />
</label>
<label>
<input type="radio" name="jio_type" value="custom" required="required" />
<input type="radio" name="jio_type" value="custom" required="required" {{#if jio_type_custom}}checked="checked"{{/if}} />
Custom,
</label>
<label>
jIO Configuration:
<input type="text" name="custom_configuration" placeholder='{"type": "indexeddb", "database": "officejs-chat"}' />
<input type="text" name="custom_configuration" value="{{jio_configuration}}" />
</label>
<br />
<input type="submit" value="Connect!" />
</script>
<script class="contact-form-template" type="text/x-handlebars-template">
<label>E-mail Address:</label>
<input type="text" name="default_email_coordinate_text"
value="{{default_email_coordinate_text}}" />
<label>Last Name:</label>
<input type="text" name="last_name" value="{{last_name}}" />
<label>First Name:</label>
<input type="text" name="first_name" value="{{first_name}}" />
<label>jIO Configuration:</label>
<input type="text" name="jio_configuration"
value="{{jio_configuration}}" />
<br />
<input type="submit" value="Update!" />
</script>
</head>
<body>
<h1>Choose Shared Storage</h1>
<form class="connect-form">
</form>
<h1>Edit Contact Details</h1>
<form class="contact-form">
</form>
</body>
</html>
\ No newline at end of file
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global window, rJS, jIO, btoa */
(function (window, rJS, jIO, btoa) {
/*global window, rJS, jIO, Handlebars, btoa */
(function (window, rJS, jIO, Handlebars, btoa) {
"use strict";
......@@ -8,23 +8,61 @@
// State is set in erp5_page_chat_box.
// The following functions is acquired from erp5_page_chat_box.
// The following function is acquired from erp5_page_chat_box.
.declareAcquiredMethod("joinNewRoom", "joinNewRoom")
// The following functions are all acquired from erp5_page_launcher.
.declareAcquiredMethod("jio_put", "jio_put")
.declareAcquiredMethod("jio_get", "jio_get")
.declareAcquiredMethod("jio_repair", "jio_repair")
/* Render the gadget.
* Parameters:
* - gadget.state: default_jio_type, default_erp5_url, default_dav_url
* Effects: pre-fill input fields with default input values
* - gadget.state.jio_id: the id of the contact in the jIO storage
* Effects:
* - jIO get: the contact info associated with gadget.state.jio_id
* - run the contact info through Handlebars to render the forms
* - pre-fill input fields with default input values
*/
.declareMethod("render", function () {
var gadget = this,
fields = gadget.element.querySelector(".connect-form").elements;
fields.jio_type.value = gadget.state.default_jio_type;
fields.erp5_url.value = gadget.state.default_erp5_url;
fields.dav_url.value = gadget.state.default_dav_url;
var gadget = this;
return gadget.jio_get(gadget.state.jio_id)
.push(function (result) {
var connect_form_template = Handlebars.compile(
Object.getPrototypeOf(gadget).constructor.__template_element
.querySelector(".connect-form-template").innerHTML
),
contact_form_template = Handlebars.compile(
Object.getPrototypeOf(gadget).constructor.__template_element
.querySelector(".contact-form-template").innerHTML
),
connect_form_param_dict = {
erp5_url: gadget.state.default_erp5_url,
dav_url: gadget.state.default_dav_url,
jio_configuration: result.jio_configuration
};
switch (gadget.state.default_jio_type) {
case "erp5":
connect_form_param_dict.jio_type_erp5 = true;
break;
case "dav":
connect_form_param_dict.jio_type_dav = true;
break;
case "custom":
connect_form_param_dict.jio_type_custom = true;
break;
}
gadget.element.querySelector(".connect-form").innerHTML =
connect_form_template(connect_form_param_dict);
gadget.element.querySelector(".contact-form").innerHTML =
contact_form_template(result);
});
})
......@@ -38,10 +76,10 @@
* - change to the room corresponding to the new storage
*/
.declareMethod("createJioReplicate", function () {
.declareMethod("createJioReplicate", function (event) {
var gadget = this,
remote_sub_storage,
fields = gadget.element.querySelector(".connect-form").elements;
fields = event.target.elements;
switch (fields.jio_type.value) {
case "erp5":
......@@ -82,6 +120,34 @@
})
/* Update the current contact information.
* Parameters:
* - the current values from the contact form
* Effects:
* - jIO put: the contact info from the contact form
* - jIO repair: to propagate the update to the remote storage
*/
.declareMethod("updateContact", function (event) {
var i, gadget = this,
fields = event.target.elements,
jio_document = {
portal_type: "Person",
parent_relative_url: "person_module"
};
for (i = 0; i < fields.length; i += 1) {
// exclude value of submit button, whose name is ""
if (fields[i].name) {
jio_document[fields[i].name] = fields[i].value;
}
}
return gadget.jio_put(gadget.state.jio_id, jio_document)
.push(function () {
return gadget.jio_repair();
});
})
/* Access the jIO replicate storage.
* Parameters:
* - method_name: the jIO method to call
......@@ -103,13 +169,16 @@
})
// Call createJioReplicate when the form is submitted.
// Call the appropriate function when the form is submitted.
.onEvent("submit", function (event) {
var gadget = this;
if (event.target.className === "connect-form") {
return gadget.createJioReplicate(event);
}
if (event.target.className === "contact-form") {
return gadget.updateContact(event);
}
});
}(window, rJS, jIO, btoa));
\ No newline at end of file
}(window, rJS, jIO, Handlebars, btoa));
\ No newline at end of file
......@@ -13,13 +13,9 @@
*/
// Handlebars templates
var chat_list_template,
contact_list_template,
// arbitrary limit to number of jIO text posts to return
// must include limit: [0, JIO_QUERY_MAX_LIMIT] because the default is 10
JIO_QUERY_MAX_LIMIT = 1000000000,
var JIO_QUERY_MAX_LIMIT = 1000000000,
// list of how long to wait when polling, i.e. wait for 0.5 seconds, poll,
// then wait for 1 second, poll, etc. until always waiting for 5 minutes
......@@ -186,7 +182,10 @@
rJS(window)
.setState({
// the email of the current user
name: null,
// the name of the current room
room: null,
// keep track of references to elements in the DOM
......@@ -195,7 +194,11 @@
chat_box_element: {style: {}},
room_gadget_element: {style: {}},
// true if the room is a chat box, false if the room is a contact panel
// Handlebars templates
chat_list_template: null,
contact_list_template: null,
// true if the current room is a chat box, false if it is a contact panel
is_chat: false,
// a dict of room IDs to their names, i.e. {foo_bar_com: "foo@bar.com"}
......@@ -237,6 +240,8 @@
.declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("requireSetting", "requireSetting")
.declareAcquiredMethod("getSetting", "getSetting")
.declareAcquiredMethod("jio_post", "jio_post")
.declareAcquiredMethod("jio_allDocs", "jio_allDocs")
/* Join a new room.
......@@ -329,14 +334,15 @@
// render the contact list and chat list using Handlebars
gadget.element.querySelector(".contact-list").innerHTML =
contact_list_template({list: contact_list});
gadget.state.contact_list_template({list: contact_list});
if (gadget.state.is_chat) {
chat_list = [];
for (i = 0; i < message_list.length; i += 1) {
chat_list.push(messageToChat(message_list[i]));
}
chat_list_element = gadget.element.querySelector(".chat-list");
chat_list_element.innerHTML = chat_list_template({list: chat_list});
chat_list_element.innerHTML =
gadget.state.chat_list_template({list: chat_list});
chat_list_element.scrollTop = chat_list_element.scrollHeight;
}
......@@ -364,14 +370,17 @@
* Effects:
* - update header, page_title to "OfficeJS Chat"
* - compile Handlebars templates
* - update state with default parameters
* - redirect if no jIO storage available
* - create the user contact, whose name is the user name
* - declare a new room gadget for each contact in jIO storage
* - create the user contact if not present, whose name is the user name
* - change to the user contact panel
*/
.declareService(function () {
var gadget = this,
user_email;
user_email,
has_own_contact = false;
return gadget.requireSetting(
"jio_storage_description",
"jio_configurator",
......@@ -380,11 +389,11 @@
return gadget.updateHeader({page_title: "OfficeJS Chat"});
})
.push(function () {
chat_list_template = Handlebars.compile(
gadget.state.chat_list_template = Handlebars.compile(
Object.getPrototypeOf(gadget).constructor.__template_element
.querySelector(".chat-list-template").innerHTML
);
contact_list_template = Handlebars.compile(
gadget.state.contact_list_template = Handlebars.compile(
Object.getPrototypeOf(gadget).constructor.__template_element
.querySelector(".contact-list-template").innerHTML
);
......@@ -422,29 +431,65 @@
favicon_alert: false
});
};
return gadget.createRoom(user_email);
return gadget.jio_allDocs({
query: 'portal_type: "Person"',
limit: [0, JIO_QUERY_MAX_LIMIT],
sort_on: [['modification_date', 'descending']],
select_list: [
'jio_configuration',
'default_email_coordinate_text',
'first_name',
'last_name'
]
});
})
.push(function (contact_list) {
var i, contact_email, promise_list = [];
for (i = 0; i < contact_list.data.total_rows; i += 1) {
contact_email = contact_list.data.rows[i]
.value.default_email_coordinate_text;
if (contact_email) {
if (contact_email === user_email) {
has_own_contact = true;
}
promise_list.push(gadget.declareRoomGadget(
contact_email,
contact_list.data.rows[i].id
));
}
}
return RSVP.all(promise_list);
})
.push(function () {
return gadget.changeState({room: user_email});
var i, children = gadget.element.querySelectorAll(
".chat-right-panel > div[data-gadget-url]"
);
for (i = 0; i < children.length; i += 1) {
children[i].style.display = "none";
}
if (!has_own_contact) {
return gadget.createRoom(user_email);
}
return gadget.changeState({room: user_email, is_chat: false});
})
);
})
/* Create a new room.
/* Declare a new room gadget.
* Parameters:
* - room: the name of the room
* - jio_id: the ID of the room in jIO storage
* Requirements:
* - room is not blank and not already in the contact list
* Effects:
* - declare a new gadget with scope "room-gadget-" + room
* - set its ID to a querySelector-safe translation of room
* - initialize its state and render the room gadget
* - update the chat box gadget state dicts with the new room
* - change to the room contact panel
* - update the chat box gadget state dictionaries with the new room
*/
.declareMethod("createRoom", function (room) {
.declareMethod("declareRoomGadget", function (room, jio_id) {
var gadget = this,
room_gadget;
if (!room.trim()) {
......@@ -464,6 +509,7 @@
.appendChild(room_gadget.element);
return room_gadget.changeState({
room: room,
jio_id: jio_id,
local_sub_storage: gadget.state.local_sub_storage,
default_jio_type: gadget.state.default_jio_type,
default_erp5_url: gadget.state.default_erp5_url,
......@@ -481,9 +527,6 @@
}
});
})
.push(function () {
return room_gadget.render();
})
.push(function () {
gadget.state.message_list_dict[room] = [];
gadget.state.message_count_dict[room] = 0;
......@@ -493,6 +536,33 @@
lock: false
};
gadget.state.id_to_name["chat-contact-" + nameToId(room)] = room;
return room_gadget.render();
});
})
/* Create a new room.
* Parameters:
* - room: the name of the room
* Requirements:
* - room is not blank and not already in the contact list
* Effects:
* - post a new contact to jIO storage
* - declare a new room gadget
* - change to the room contact panel
*/
.declareMethod("createRoom", function (room) {
var gadget = this;
return gadget.jio_post({
portal_type: "Person",
parent_relative_url: "person_module",
default_email_coordinate_text: room
})
.push(function (jio_id) {
return gadget.declareRoomGadget(room, jio_id);
})
.push(function () {
return gadget.changeState({room: room, is_chat: false});
});
})
......
......@@ -16,6 +16,7 @@
<br />
<input type="radio" name="default_jio_type" value="erp5">ERP5</input>
<input type="radio" name="default_jio_type" value="dav">WebDAV</input>
<input type="radio" name="default_jio_type" value="custom">Custom jIO Configuration</input>
<br />
<label>Default ERP5 URL:</label>
<input type="text" name="default_erp5_url", placeholder="https://softinst75770.host.vifib.net/erp5/web_site_module/hateoas" />
......
......@@ -22,7 +22,7 @@
</p>
<div class="configurator-grid">
<a class="grid-box configurator-button" href="#!display?n.page=sync">Synchronize</a>
<a class="grid-box configurator-button" href="#!display?n.page=contact_list">Access Document List</a>
<a class="grid-box configurator-button" href="#">Access Document List</a>
</div>
</section>
</article>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment