Skip to content

Commit 172b240

Browse files
Merge pull request #5 from alvinjohnsonso/widget-iframe-reload-height
- Added iframe resizing on height update event. - Provided platform identifier for the widget iframe.
2 parents 7373769 + 5bf26cc commit 172b240

File tree

1 file changed

+69
-53
lines changed

1 file changed

+69
-53
lines changed

views/admin.php.inc

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -159,41 +159,44 @@ function tawk_to_render_widget_iframe($base_url, $iframe_url, $widget=array(), $
159159
</div>
160160
</div>
161161
<script type="text/javascript">
162-
var currentHost = window.location.protocol + "//" + window.location.host;
163-
var url = "<?php echo $iframe_url; ?>&parentDomain=" + currentHost;
162+
var currentHost = window.location.protocol + "//" + window.location.host;
163+
var url = "<?php echo $iframe_url; ?>&pltf=drupal&pltfv=7&parentDomain=" + currentHost;
164164

165-
jQuery("#tawkIframe").attr("src", url);
165+
jQuery("#tawkIframe").attr("src", url);
166166

167-
var iframe = jQuery("#tawk_widget_customization")[0];
167+
var iframe = jQuery("#tawk_widget_customization")[0];
168168

169-
window.addEventListener("message", function(e) {
170-
if(e.origin === "<?php echo $base_url; ?>") {
169+
window.addEventListener("message", function(e) {
170+
if(e.origin === "<?php echo $base_url; ?>") {
171+
if(e.data.action === "setWidget") {
172+
setWidget(e);
173+
}
171174

172-
if(e.data.action === "setWidget") {
173-
setWidget(e);
174-
}
175+
if(e.data.action === "removeWidget") {
176+
removeWidget(e);
177+
}
175178

176-
if(e.data.action === "removeWidget") {
177-
removeWidget(e);
178-
}
179-
}
180-
});
179+
if(e.data.action === 'reloadHeight') {
180+
reloadIframeHeight(e.data.height);
181+
}
182+
}
183+
});
181184

182-
function setWidget(e) {
185+
function setWidget(e) {
183186
jQuery.post("/admin/config/tawk/setwidget", {
184-
pageId : e.data.pageId,
185-
widgetId : e.data.widgetId
186-
}, function(r) {
187-
if(r.success) {
188-
$('input[name="page_id"]').val(e.data.pageId);
189-
$('input[name="widget_id"]').val(e.data.widgetId);
190-
$('#widget_already_set').hide();
191-
e.source.postMessage({action: "setDone"}, "<?php echo $base_url; ?>");
192-
} else {
193-
e.source.postMessage({action: "setFail"}, "<?php echo $base_url; ?>");
194-
}
195-
});
196-
}
187+
pageId : e.data.pageId,
188+
widgetId : e.data.widgetId
189+
}, function(r) {
190+
if(r.success) {
191+
$('input[name="page_id"]').val(e.data.pageId);
192+
$('input[name="widget_id"]').val(e.data.widgetId);
193+
$('#widget_already_set').hide();
194+
e.source.postMessage({action: "setDone"}, "<?php echo $base_url; ?>");
195+
} else {
196+
e.source.postMessage({action: "setFail"}, "<?php echo $base_url; ?>");
197+
}
198+
});
199+
}
197200

198201
function removeWidget(e) {
199202
jQuery.post("/admin/config/tawk/removewidget", function(r) {
@@ -208,42 +211,55 @@ function tawk_to_render_widget_iframe($base_url, $iframe_url, $widget=array(), $
208211
});
209212
}
210213

211-
jQuery(document).ready(function() {
212-
if(jQuery("#always_display").prop("checked")){
213-
jQuery('.show_specific').prop('disabled', true);
214-
} else {
215-
jQuery('.hide_specific').prop('disabled', true);
214+
function reloadIframeHeight(height) {
215+
if (!height) {
216+
return;
217+
}
218+
219+
var iframe = jQuery('#tawkIframe');
220+
if (height === iframe.height()) {
221+
return;
222+
}
223+
224+
iframe.height(height);
216225
}
217226

218-
jQuery("#always_display").change(function() {
219-
if(this.checked){
220-
jQuery('.hide_specific').prop('disabled', false);
227+
jQuery(document).ready(function() {
228+
if(jQuery("#always_display").prop("checked")){
221229
jQuery('.show_specific').prop('disabled', true);
222230
} else {
223231
jQuery('.hide_specific').prop('disabled', true);
224-
jQuery('.show_specific').prop('disabled', false);
225232
}
226-
});
227233

228-
// process the form
229-
jQuery('#module_form').submit(function(event) {
230-
$path = "/admin/config/tawk/setoptions";
231-
jQuery.post($path, {
232-
action : 'set_visibility',
233-
ajax : true,
234-
page_id : jQuery('input[name="page_id"]').val(),
235-
widget_id : jQuery('input[name="widget_id"]').val(),
236-
options : jQuery(this).serialize()
237-
}, function(r) {
238-
if(r.success) {
239-
$('#optionsSuccessMessage').toggle().delay(3000).fadeOut();
234+
jQuery("#always_display").change(function() {
235+
if(this.checked){
236+
jQuery('.hide_specific').prop('disabled', false);
237+
jQuery('.show_specific').prop('disabled', true);
238+
} else {
239+
jQuery('.hide_specific').prop('disabled', true);
240+
jQuery('.show_specific').prop('disabled', false);
240241
}
241242
});
242243

243-
// stop the form from submitting the normal way and refreshing the page
244-
event.preventDefault();
244+
// process the form
245+
jQuery('#module_form').submit(function(event) {
246+
$path = "/admin/config/tawk/setoptions";
247+
jQuery.post($path, {
248+
action : 'set_visibility',
249+
ajax : true,
250+
page_id : jQuery('input[name="page_id"]').val(),
251+
widget_id : jQuery('input[name="widget_id"]').val(),
252+
options : jQuery(this).serialize()
253+
}, function(r) {
254+
if(r.success) {
255+
$('#optionsSuccessMessage').toggle().delay(3000).fadeOut();
256+
}
257+
});
258+
259+
// stop the form from submitting the normal way and refreshing the page
260+
event.preventDefault();
261+
});
245262
});
246-
});
247263
</script>
248264
<?php
249265
$markup = ob_get_contents();

0 commit comments

Comments
 (0)