| 1 |
/* global visualizer */ |
| 2 |
/* global alert */ |
| 3 |
|
| 4 |
(function (wpmv) { |
| 5 |
var vm, vmv; |
| 6 |
|
| 7 |
vm = visualizer.media = {}; |
| 8 |
vmv = vm.view = {}; |
| 9 |
|
| 10 |
vmv.Chart = wpmv.MediaFrame.extend({ |
| 11 |
initialize: function () { |
| 12 |
var self = this; |
| 13 |
|
| 14 |
_.defaults(self.options, { |
| 15 |
action: '', |
| 16 |
state: 'iframe:visualizer' |
| 17 |
}); |
| 18 |
|
| 19 |
wpmv.MediaFrame.prototype.initialize.apply(self, arguments); |
| 20 |
|
| 21 |
wpmv.settings.tabUrl = self.options.action; |
| 22 |
self.createIframeStates(); |
| 23 |
}, |
| 24 |
|
| 25 |
open: function () { |
| 26 |
try{ |
| 27 |
wpmv.MediaFrame.prototype.open.apply(this, arguments); |
| 28 |
}catch(error){ |
| 29 |
alert(visualizer.i10n.conflict); |
| 30 |
} |
| 31 |
this.$el.addClass('hide-menu'); |
| 32 |
} |
| 33 |
}); |
| 34 |
})(wp.media.view); |
| 35 |
|
| 36 |
(function ($, vmv, vu) { |
| 37 |
var resizeTimeout; |
| 38 |
|
| 39 |
$.fn.adjust = function () { |
| 40 |
return $(this).each(function () { |
| 41 |
var width = $('#visualizer-library').width(), |
| 42 |
margin = width * 0.02; |
| 43 |
|
| 44 |
width *= 0.305; |
| 45 |
$(this).width(width - 14).height(width * 0.75).parent().css('margin-right', margin + 'px').css('margin-bottom', margin + 'px'); |
| 46 |
}); |
| 47 |
}; |
| 48 |
|
| 49 |
$('.visualizer-chart-canvas').adjust(); |
| 50 |
|
| 51 |
$(document).ready(function () { |
| 52 |
// clears the filters in the library form and submits. |
| 53 |
$('#viz-lib-reset').on('click', function(e){ |
| 54 |
e.preventDefault(); |
| 55 |
$(this).parent('form')[0].reset(); |
| 56 |
$(this).parent('form').find('.viz-filter').each(function(index, el){ |
| 57 |
var tag = $(el).prop('tagName').toLowerCase() + (typeof $(el).attr('type') !== 'undefined' ? $(el).attr('type').toLowerCase() : ''); |
| 58 |
switch(tag){ |
| 59 |
case 'select': |
| 60 |
$(el).prop('selectedIndex', 0); |
| 61 |
break; |
| 62 |
case 'inputtext': |
| 63 |
$(el).val(''); |
| 64 |
break; |
| 65 |
} |
| 66 |
|
| 67 |
}); |
| 68 |
$(this).parent('form').submit(); |
| 69 |
}); |
| 70 |
|
| 71 |
$('.visualizer-chart-shortcode').click(function (e) { |
| 72 |
var range, selection; |
| 73 |
|
| 74 |
if (window.getSelection && document.createRange) { |
| 75 |
selection = window.getSelection(); |
| 76 |
range = document.createRange(); |
| 77 |
range.selectNodeContents(e.target); |
| 78 |
selection.removeAllRanges(); |
| 79 |
selection.addRange(range); |
| 80 |
} else if (document.selection && document.body.createTextRange) { |
| 81 |
range = document.body.createTextRange(); |
| 82 |
range.moveToElementText(e.target); |
| 83 |
range.select(); |
| 84 |
} |
| 85 |
}); |
| 86 |
|
| 87 |
$('.add-new-chart').click(function () { |
| 88 |
var wnd = window, |
| 89 |
view = new vmv.Chart({action: vu.create}); |
| 90 |
|
| 91 |
window.parent.addEventListener('message', function(event){ |
| 92 |
switch(event.data) { |
| 93 |
case 'visualizer:mediaframe:close': |
| 94 |
view.close(); |
| 95 |
break; |
| 96 |
} |
| 97 |
}, false); |
| 98 |
|
| 99 |
// remove the 'type' while refreshing the library page on creation of a new chart. |
| 100 |
// this is to avoid cases where users have filtered for chart type A and end up creating chart type B |
| 101 |
// remove 'vaction' as well so that additional actions are removed |
| 102 |
wnd.send_to_editor = function () { |
| 103 |
wnd.location.href = vu.base.replace(/type=[a-zA-Z]*/, '').replace(/vaction/, ''); |
| 104 |
}; |
| 105 |
view.open(); |
| 106 |
|
| 107 |
return false; |
| 108 |
}); |
| 109 |
|
| 110 |
$('.visualizer-chart-edit').click(function () { |
| 111 |
var wnd = window, |
| 112 |
view = new vmv.Chart({action: vu.edit + '&chart=' + $(this).attr('data-chart')}); |
| 113 |
|
| 114 |
wnd.send_to_editor = function () { |
| 115 |
wnd.location.href = wnd.location.href.replace(/vaction/, ''); |
| 116 |
}; |
| 117 |
|
| 118 |
view.open(); |
| 119 |
|
| 120 |
return false; |
| 121 |
}); |
| 122 |
|
| 123 |
$(".visualizer-chart-export").on("click", function () { |
| 124 |
$.ajax({ |
| 125 |
url: $(this).attr("data-chart"), |
| 126 |
method: "get", |
| 127 |
success: function (data, textStatus, jqXHR) { |
| 128 |
var a = document.createElement("a"); |
| 129 |
document.body.appendChild(a); |
| 130 |
a.style = "display: none"; |
| 131 |
var blob = new Blob([data.data.csv], {type: "application/csv"}), |
| 132 |
url = window.URL.createObjectURL(blob); |
| 133 |
a.href = url; |
| 134 |
a.download = data.data.name; |
| 135 |
a.click(); |
| 136 |
setTimeout(function () { |
| 137 |
window.URL.revokeObjectURL(url); |
| 138 |
}, 100); |
| 139 |
} |
| 140 |
}); |
| 141 |
return false; |
| 142 |
}); |
| 143 |
|
| 144 |
$(".visualizer-chart-image").on("click", function () { |
| 145 |
$('body').trigger('visualizer:action:specificchart', {action: 'image', id: $(this).attr("data-chart"), data: null, dataObj: {name: $(this).attr("data-chart-title")}}); |
| 146 |
return false; |
| 147 |
}); |
| 148 |
|
| 149 |
// if vaction=addnew is found as a GET request parameter, show the modal. |
| 150 |
if(location.href.indexOf('vaction=addnew') !== -1){ |
| 151 |
$('.add-new-chart').first().trigger('click'); |
| 152 |
} |
| 153 |
|
| 154 |
$(window).resize(function () { |
| 155 |
clearTimeout(resizeTimeout); |
| 156 |
resizeTimeout = setTimeout(function () { |
| 157 |
$('.visualizer-chart-canvas').adjust(); |
| 158 |
}, 100); |
| 159 |
}); |
| 160 |
|
| 161 |
$('.visualizer-error i.error').on('click', function(){ |
| 162 |
alert( $(this).attr('data-viz-error') ); |
| 163 |
}); |
| 164 |
$('.visualizer-chart:not(.visualizer-chart-display), .visualizer-library-pagination').fadeIn(500); |
| 165 |
}); |
| 166 |
})(jQuery, visualizer.media.view, visualizer.urls); |