Skip to content

Commit e0d26f7

Browse files
committed
Add first callbacks
Related to issue #17 Still sending ALL received messages, not only new ones, but also old messages, yours as sent
1 parent 5cb86dd commit e0d26f7

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ To configure the chat, just use the object passed on IASChat instantiation. *Bol
5050
- defaultSupportPic: String Default support picture (if no supporter assigned)
5151
- uploadFiles: Boolean Enable or disable the option to upload and send files (Default: true)
5252
- onlyPictures: Boolean Allow only pictures, or all file types (Default: true)
53+
- onMessage: Function Execute a function on message recieved. Params message (all object) and key are passed to the function
54+
- onSend: Function Execute a function when a message is sent. Params message (all object) and key are passed to the function
5355

5456
In IASChatProvider, there are some extra features:
5557
- container: String Container for support panel (*#identifier* or *.className*. Default: *body*)

demo/index.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,22 @@ <h1>User side support</h1>
6161
mainColor: '#ff9800',
6262
textColor: '#fff',
6363
defaultSupportName: 'John Doe',
64-
defaultSupportPic: 'http://lorempixel.com/100/100/people'
64+
defaultSupportPic: 'http://lorempixel.com/100/100/people',
65+
onMessage: myFunction,
66+
onSend: function(msg, key) {
67+
console.log('Sent message. Id stored: ' + key);
68+
console.log(msg)
69+
}
6570
});
71+
72+
function myFunction(message, key) {
73+
console.log('--------------------');
74+
console.log('- We got a message!');
75+
console.log(message);
76+
console.log('- key:');
77+
console.log(key);
78+
console.log('--------------------');
79+
}
6680
};
6781
</script>
6882

dist/chat.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function IASChat(config) {
2121
var hashSign = config.hashSign || '?';
2222
var uploadFiles = config.uploadFiles || true;
2323
var onlyPictures = config.onlyPictures || true;
24+
var onSend = config.onSend || null;
25+
var onMessage = config.onMessage || null;
2426

2527
// Prepare interface
2628
printInterface(container);
@@ -331,13 +333,20 @@ function IASChat(config) {
331333

332334
if(message.uid == uid) {
333335
printMessage(text);
336+
if(typeof onSend === 'function') {
337+
onSend(message, key);
338+
}
334339
} else {
335340
printMessage(text, true);
336341

337342
// If chat is open, set the message as read
338343
if(!isHidden()) {
339344
readLastMessage();
340345
}
346+
347+
if(typeof onMessage === 'function') {
348+
onMessage(message, key);
349+
}
341350
}
342351
}
343352

js/chat.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function IASChat(config) {
2121
var hashSign = config.hashSign || '?';
2222
var uploadFiles = config.uploadFiles || true;
2323
var onlyPictures = config.onlyPictures || true;
24+
var onSend = config.onSend || null;
25+
var onMessage = config.onMessage || null;
2426

2527
// Prepare interface
2628
printInterface(container);
@@ -331,13 +333,20 @@ function IASChat(config) {
331333

332334
if(message.uid == uid) {
333335
printMessage(text);
336+
if(typeof onSend === 'function') {
337+
onSend(message, key);
338+
}
334339
} else {
335340
printMessage(text, true);
336341

337342
// If chat is open, set the message as read
338343
if(!isHidden()) {
339344
readLastMessage();
340345
}
346+
347+
if(typeof onMessage === 'function') {
348+
onMessage(message, key);
349+
}
341350
}
342351
}
343352

0 commit comments

Comments
 (0)