1 /* ==========================================================
2 * jquery-live-preview.js v1.1.0
3 * https://github.com/alanphoon/jquery-live-preview
4 * ==========================================================
5 * Copyright 2015 Alan Phoon, www.ampedupdesigns.com
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * ========================================================== */
29 livePreview: function(options) {
41 var options = $.extend(defaults, options);
42 //calculate appropriate scaling based on width.
43 var scale_w = (options.viewWidth / options.targetWidth);
44 var scale_h = (options.viewHeight / options.targetHeight);
46 var preview_id = 'livepreview_dialog';
48 if(typeof options.scale != 'undefined')
49 scale_f = options.scale;
58 var showPreview = function(event) {
59 var triggerType = event.data.triggerType;
60 var obj = event.data.target;
61 var href = event.data.href;
62 var s = event.data.scale;
64 if( (triggerType == 'click') && ($('#' + preview_id).length == 0) ) {
65 event.preventDefault();
68 var currentPos = options.position;
69 if(obj.attr("data-position"))
70 currentPos = obj.attr("data-position");
72 var currentOffset = options.positionOffset;
73 if(obj.attr("data-positionOffset"))
74 currentOffset = obj.attr("data-positionOffset");
76 if(obj.attr("data-scale"))
77 s = obj.attr("data-scale");
79 var pos = $(this).offset();
80 var width = $(this).width();
81 var height = $(this).height();
82 var toppos = pos.top - (options.viewHeight/2);
83 var leftpos = pos.left + width + currentOffset;
85 if(currentPos == 'left') {
86 leftpos = pos.left - options.viewWidth - currentOffset;
89 if(currentPos == 'top') {
90 leftpos = pos.left + (width/2) - (options.viewWidth/2);
91 toppos = pos.top - options.viewHeight - currentOffset;
94 if(currentPos == 'bottom') {
95 leftpos = pos.left + (width/2) - (options.viewWidth/2);
96 toppos = pos.top + (height/2) + currentOffset;
100 $('body').append('<div id="livepreview_dialog" class="' + currentPos + '" style="display:none; padding:0px; left: ' + leftpos + 'px; top:' + toppos + 'px; width: ' + options.viewWidth + 'px; height: ' + options.viewHeight + 'px"><div class="livepreview-container" style="overflow:hidden; width: ' + options.viewWidth + 'px; height: ' + options.viewHeight + 'px"><iframe id="livepreview_iframe" src="' + href + '" style="height:' + options.targetHeight + 'px; width:' + options.targetWidth + 'px;-moz-transform: scale('+ s + ');-moz-transform-origin: 0 0;-o-transform: scale('+ s + ');-o-transform-origin: 0 0;-webkit-transform: scale('+ s + ');-webkit-transform-origin: 0 0;"></iframe></div></div>');
101 $('#' + preview_id).fadeIn(100);
104 return this.each(function() {
108 var href = obj.attr("data-preview-url") || obj.attr("href");
109 var triggerType = options.trigger;
111 if(obj.attr("data-trigger")) {
112 triggerType = obj.attr("data-trigger");
115 if(triggerType != 'click') {
116 triggerType = 'mouseenter';
117 obj.on('click', function() {
118 $('#' + preview_id).remove();
122 obj.on(triggerType, null, { triggerType: triggerType, target: obj, href: href, scale: s }, showPreview);
123 obj.on('mouseleave', function() {
124 $('#' + preview_id).remove();