| 1 |
/*jshint scripturl:true*/ |
| 2 |
/* global google */ |
| 3 |
/* global visualizer */ |
| 4 |
/* global showNotice */ |
| 5 |
(function($, wpm, visualizer) { |
| 6 |
var libraryWidth, libraryHeight, wpmv, wpmV, wpmvv, wpmvvl, wpmvvb, l10n; |
| 7 |
|
| 8 |
wpmv = wpm.view; |
| 9 |
wpmV = wpm.View; |
| 10 |
wpmvv = wpmv.visualizer = {}; |
| 11 |
l10n = wpmv.l10n.visualizer; |
| 12 |
|
| 13 |
/** |
| 14 |
* ========================================================================= |
| 15 |
* COMMON |
| 16 |
* ========================================================================= |
| 17 |
*/ |
| 18 |
|
| 19 |
if (!_.isFunction(wpmV.prototype.make)) { |
| 20 |
wpmV.prototype.make = function(tag, attrs, val) { |
| 21 |
var html, attr; |
| 22 |
|
| 23 |
html = '<' + tag; |
| 24 |
for (attr in attrs) { |
| 25 |
html += ' ' + attr + '="' + attrs[attr] + '"'; |
| 26 |
} |
| 27 |
html += '>' + val + '</' + tag + '>'; |
| 28 |
|
| 29 |
return html; |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
wpmvv.Chart = wpmV.extend({ |
| 34 |
className: 'visualizer-library-chart-canvas', |
| 35 |
|
| 36 |
constructor: function(options) { |
| 37 |
this.id = 'visualizer-chart-' + options.model.get('id'); |
| 38 |
wpmV.apply(this, arguments); |
| 39 |
}, |
| 40 |
|
| 41 |
render: function() { |
| 42 |
var self, model, chart; |
| 43 |
|
| 44 |
self = this; |
| 45 |
model = self.model; |
| 46 |
|
| 47 |
self.$el |
| 48 |
.width(self.options.width) |
| 49 |
.height(self.options.height) |
| 50 |
.css('background-image', 'none'); |
| 51 |
|
| 52 |
chart = {}; |
| 53 |
chart.type = model.get('type'); |
| 54 |
chart.series = model.get('series'); |
| 55 |
chart.data = model.get('data'); |
| 56 |
chart.library = model.get('library'); |
| 57 |
chart.settings = model.get('settings'); |
| 58 |
chart.settings.width = self.options.width; |
| 59 |
chart.settings.height = self.options.height; |
| 60 |
$('#' + self.id).append(model.get('css')); |
| 61 |
|
| 62 |
$('body').trigger('visualizer:render:specificchart:start', {id: self.id, chart: chart, v: {page_type: 'post'}} ); |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
/** |
| 67 |
* ========================================================================= |
| 68 |
* LIBRARY |
| 69 |
* ========================================================================= |
| 70 |
*/ |
| 71 |
|
| 72 |
wpmvvl = wpmvv.Library = wpmV.extend({ |
| 73 |
id: 'visualizer-library-view', |
| 74 |
className: 'visualizer-clearfix', |
| 75 |
template: wpm.template('visualizer-library-empty'), |
| 76 |
|
| 77 |
initialize: function() { |
| 78 |
var self = this; |
| 79 |
|
| 80 |
_.defaults(self.options, { |
| 81 |
filter: 'all', |
| 82 |
page: 1 |
| 83 |
}); |
| 84 |
|
| 85 |
self.controller.on('visualizer:library:filter', self.onFilterChanged, self); |
| 86 |
self.controller.on('visualizer:library:page', self.onPageChanged, self); |
| 87 |
self.collection.on('reset', self.renderCollection, self); |
| 88 |
|
| 89 |
self.resetCollection(); |
| 90 |
}, |
| 91 |
|
| 92 |
onFilterChanged: function(filter) { |
| 93 |
this.options.filter = filter; |
| 94 |
this.options.page = 1; |
| 95 |
this.resetCollection(); |
| 96 |
}, |
| 97 |
|
| 98 |
onPageChanged: function(page) { |
| 99 |
this.options.page = page; |
| 100 |
this.resetCollection(); |
| 101 |
}, |
| 102 |
|
| 103 |
render: function() {}, |
| 104 |
|
| 105 |
renderCollection: function() { |
| 106 |
var self = this; |
| 107 |
|
| 108 |
if (self.collection.length > 0) { |
| 109 |
self.$el.html(''); |
| 110 |
self.collection.each(self.addChart, self); |
| 111 |
} else { |
| 112 |
self.$el.html(self.template({})); |
| 113 |
} |
| 114 |
}, |
| 115 |
|
| 116 |
addChart: function(chart) { |
| 117 |
var self = this, |
| 118 |
view = new wpmvvl.Chart({ model: chart }); |
| 119 |
|
| 120 |
self.$el.append(view.$el); |
| 121 |
self.views.set('#visualizer-chart-' + chart.get('id'), view, { silent: true }); |
| 122 |
view.render(); |
| 123 |
}, |
| 124 |
|
| 125 |
resetCollection: function() { |
| 126 |
var self = this, |
| 127 |
controller = self.controller, |
| 128 |
content = controller.$el.find(controller.content.selector); |
| 129 |
|
| 130 |
content.lock(); |
| 131 |
self.collection.fetch({ |
| 132 |
silent: false, |
| 133 |
data: { |
| 134 |
filter: self.options.filter, |
| 135 |
page: self.options.page |
| 136 |
}, |
| 137 |
statusCode: { |
| 138 |
200: function(response) { |
| 139 |
var paginationView = controller.toolbar.get('toolbar').get('pagination'); |
| 140 |
|
| 141 |
if (self.options.page > response.total) { |
| 142 |
self.options.page = response.total; |
| 143 |
self.resetCollection(); |
| 144 |
} else { |
| 145 |
paginationView.options.page = self.options.page; |
| 146 |
paginationView.options.total = response.total || 1; |
| 147 |
paginationView.render(); |
| 148 |
} |
| 149 |
|
| 150 |
self.renderCollection(); |
| 151 |
$('.visualizer-library-chart').css('position', 'relative') |
| 152 |
.append($( |
| 153 |
// jshint ignore:start |
| 154 |
'<div class="visualizer-chart-bg"></div>' |
| 155 |
+ '<div class="visualizer-chart-insert-bg">' |
| 156 |
+ '<button class="button button-primary visualizer-library-chart-insert">' + visualizer.i10n.insert + '</button>' |
| 157 |
+ '</div>' |
| 158 |
// jshint ignore:end |
| 159 |
)) |
| 160 |
.on('mouseover', function(){ |
| 161 |
$(this).addClass('hover'); |
| 162 |
}); |
| 163 |
content.unlock(); |
| 164 |
} |
| 165 |
} |
| 166 |
}); |
| 167 |
} |
| 168 |
}); |
| 169 |
|
| 170 |
wpmvvl.Chart = wpmV.extend({ |
| 171 |
className: 'visualizer-library-chart', |
| 172 |
template: wpm.template('visualizer-library-chart'), |
| 173 |
|
| 174 |
events: { |
| 175 |
'click .visualizer-library-chart-delete': 'deleteChart', |
| 176 |
'click .visualizer-library-chart-insert': 'insertChart', |
| 177 |
'click .visualizer-library-chart-shortcode': 'selectShortcode' |
| 178 |
}, |
| 179 |
|
| 180 |
initialize: function() { |
| 181 |
var self = this; |
| 182 |
|
| 183 |
if (!libraryWidth && !libraryHeight) { |
| 184 |
libraryWidth = $('#visualizer-library-view').width() / 3 - 40; |
| 185 |
libraryHeight = libraryWidth * 3 / 4; |
| 186 |
|
| 187 |
libraryWidth = Math.floor(libraryWidth); |
| 188 |
libraryHeight = Math.floor(libraryHeight); |
| 189 |
} |
| 190 |
|
| 191 |
self._view = new wpmvv.Chart({ |
| 192 |
model: self.model, |
| 193 |
width: libraryWidth, |
| 194 |
height: libraryHeight |
| 195 |
}); |
| 196 |
|
| 197 |
self.$el.html(self.template(self.model.toJSON())).prepend(self._view.$el); |
| 198 |
self.views.set('#' + self._view.id, self._view, { silent: true }); |
| 199 |
}, |
| 200 |
|
| 201 |
render: function() { |
| 202 |
this._view.render(); |
| 203 |
}, |
| 204 |
|
| 205 |
deleteChart: function() { |
| 206 |
var self = this; |
| 207 |
|
| 208 |
if (showNotice.warn()) { |
| 209 |
self.model.destroy({ |
| 210 |
wait: true, |
| 211 |
success: function() { |
| 212 |
self.views.parent.resetCollection(); |
| 213 |
} |
| 214 |
}); |
| 215 |
} |
| 216 |
}, |
| 217 |
|
| 218 |
insertChart: function() { |
| 219 |
wpm.editor.insert('[visualizer id="' + this.model.get('id') + '"]'); |
| 220 |
}, |
| 221 |
|
| 222 |
selectShortcode: function(e) { |
| 223 |
var range, selection; |
| 224 |
|
| 225 |
if (window.getSelection && document.createRange) { |
| 226 |
selection = window.getSelection(); |
| 227 |
range = document.createRange(); |
| 228 |
range.selectNodeContents(e.target); |
| 229 |
selection.removeAllRanges(); |
| 230 |
selection.addRange(range); |
| 231 |
} else if (document.selection && document.body.createTextRange) { |
| 232 |
range = document.body.createTextRange(); |
| 233 |
range.moveToElementText(e.target); |
| 234 |
range.select(); |
| 235 |
} |
| 236 |
} |
| 237 |
}); |
| 238 |
|
| 239 |
wpmvvl.Types = wpmV.extend({ |
| 240 |
tagName: 'select', |
| 241 |
className: 'visualizer-library-filters', |
| 242 |
|
| 243 |
events: { |
| 244 |
change: 'onFilterChange' |
| 245 |
}, |
| 246 |
|
| 247 |
initialize: function() { |
| 248 |
var self = this; |
| 249 |
|
| 250 |
self.createFilters(); |
| 251 |
self.$el.html(_.chain(self.filters).map(function(filter) { |
| 252 |
return { |
| 253 |
el: self.make('option', {value: filter.key}, filter.text), |
| 254 |
priority: filter.priority || 50 |
| 255 |
}; |
| 256 |
}).sortBy('priority').pluck('el').value()); |
| 257 |
}, |
| 258 |
|
| 259 |
createFilters: function() { |
| 260 |
var self = this; |
| 261 |
|
| 262 |
self.filters = {}; |
| 263 |
_.each(l10n.library.types, function(type, i) { |
| 264 |
self.filters[type] = { |
| 265 |
text: l10n.library.filters[type], |
| 266 |
key: type, |
| 267 |
priority: (i + 1) * 10 |
| 268 |
}; |
| 269 |
}); |
| 270 |
}, |
| 271 |
|
| 272 |
onFilterChange: function() { |
| 273 |
this.controller.trigger('visualizer:library:filter', this.el.value); |
| 274 |
} |
| 275 |
}); |
| 276 |
|
| 277 |
wpmvvl.Pagination = wpmV.extend({ |
| 278 |
id: 'visualizer-library-pagination', |
| 279 |
tagName: 'ul', |
| 280 |
|
| 281 |
events: { |
| 282 |
'click a.visualizer-library-pagination-page': 'onPageChange' |
| 283 |
}, |
| 284 |
|
| 285 |
initialize: function() { |
| 286 |
_.defaults(this.options, { |
| 287 |
total: 1, |
| 288 |
page: 1 |
| 289 |
}); |
| 290 |
}, |
| 291 |
|
| 292 |
render: function() { |
| 293 |
var self, items; |
| 294 |
|
| 295 |
self = this; |
| 296 |
if (self.options.page <= 1 && self.options.total <= 1) { |
| 297 |
self.$el.html(''); |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
items = self._pagination(self.options.page, self.options.total, 7); |
| 302 |
|
| 303 |
self.$el.html(_.chain(items).map(function(item) { |
| 304 |
var content, className; |
| 305 |
|
| 306 |
content = item === '...' || item === self.options.page ? self.make('span', { class: 'visualizer-library-pagination-page' }, item) : self.make('a', { class: 'visualizer-library-pagination-page', href: 'javascript:;', 'data-page': item }, item); |
| 307 |
|
| 308 |
className = item === self.options.page ? 'visualizer-library-pagination-item visualizer-library-pagination-active' : 'visualizer-library-pagination-item'; |
| 309 |
|
| 310 |
return self.make('li', { class: className }, content); |
| 311 |
}).value()); |
| 312 |
}, |
| 313 |
|
| 314 |
_pagination: function(current, total, max) { |
| 315 |
var i, tmp, pagenation = []; |
| 316 |
|
| 317 |
if ( total <= max ) { |
| 318 |
for ( i = 1; i <= total; i++ ) { |
| 319 |
pagenation.push(i); |
| 320 |
} |
| 321 |
} else { |
| 322 |
tmp = current - Math.floor( max / 2 ); |
| 323 |
|
| 324 |
if ( max % 2 === 0 ) { |
| 325 |
tmp++; |
| 326 |
} |
| 327 |
|
| 328 |
if ( tmp < 1 ) { |
| 329 |
tmp = 1; |
| 330 |
} |
| 331 |
|
| 332 |
if ( tmp + max > total ) { |
| 333 |
tmp = total - max + 1; |
| 334 |
} |
| 335 |
|
| 336 |
for ( i = 1; i <= max; i++ ) { |
| 337 |
pagenation.push(tmp++); |
| 338 |
} |
| 339 |
|
| 340 |
if ( pagenation[0] !== 1 ) { |
| 341 |
pagenation[0] = 1; |
| 342 |
pagenation[1] = '...'; |
| 343 |
} |
| 344 |
|
| 345 |
if ( pagenation[max - 1] !== total ) { |
| 346 |
pagenation[max - 1] = total; |
| 347 |
pagenation[max - 2] = '...'; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return pagenation; |
| 352 |
}, |
| 353 |
|
| 354 |
onPageChange: function(e) { |
| 355 |
this.controller.trigger('visualizer:library:page', $(e.target).data('page')); |
| 356 |
} |
| 357 |
}); |
| 358 |
})(jQuery, wp.media, visualizer); |
| 359 |
|
| 360 |
(function($) { |
| 361 |
$.fn.lock = function() { |
| 362 |
$(this).each(function() { |
| 363 |
var locker = $('<div class="locker"></div>'), |
| 364 |
loader = $('<div class="locker-loader"></div>'), |
| 365 |
$this = $(this), |
| 366 |
position = $this.css('position'); |
| 367 |
|
| 368 |
if ($this.find('.locker').length > 0) { |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
if (!position) { |
| 373 |
position = 'static'; |
| 374 |
} |
| 375 |
|
| 376 |
$this.css('overflow', 'hidden'); |
| 377 |
switch(position) { |
| 378 |
case 'absolute': |
| 379 |
case 'relative': |
| 380 |
break; |
| 381 |
default: |
| 382 |
$this.css('position', 'relative'); |
| 383 |
break; |
| 384 |
} |
| 385 |
$this.data('position', position); |
| 386 |
|
| 387 |
locker.css('top', $this.scrollTop() + 'px').append(loader); |
| 388 |
$this.append(locker); |
| 389 |
}); |
| 390 |
|
| 391 |
return $(this); |
| 392 |
}; |
| 393 |
|
| 394 |
$.fn.unlock = function() { |
| 395 |
$(this).each(function() { |
| 396 |
var $this = $(this); |
| 397 |
|
| 398 |
$this.css({ |
| 399 |
position: $this.data('position'), |
| 400 |
overflow: 'auto' |
| 401 |
}).find('.locker').remove(); |
| 402 |
}); |
| 403 |
|
| 404 |
return $(this); |
| 405 |
}; |
| 406 |
})(jQuery); |