| 1 |
jQuery(document).ready(function ($) { |
| 2 |
|
| 3 |
var TemplateHandler = function() { |
| 4 |
var importVars = { |
| 5 |
funnelID : $('#funnelID').val(), |
| 6 |
funnelObj : { |
| 7 |
id: 0, |
| 8 |
title: '', |
| 9 |
link: '', |
| 10 |
slug: '', |
| 11 |
featured_media: 0, |
| 12 |
featured_image: '', |
| 13 |
steps_order: '', |
| 14 |
page_builder: '', |
| 15 |
categories: '', |
| 16 |
is_pro: false, |
| 17 |
}, |
| 18 |
stepObj: { |
| 19 |
id: 0, |
| 20 |
title: '', |
| 21 |
link: '', |
| 22 |
slug: '', |
| 23 |
featured_media: 0, |
| 24 |
featured_image: '', |
| 25 |
steps_order: '', |
| 26 |
page_builder: '', |
| 27 |
categories: '', |
| 28 |
is_pro: false, |
| 29 |
type: '', |
| 30 |
} |
| 31 |
|
| 32 |
}; |
| 33 |
this.initTemplateLibrary(); |
| 34 |
$('#create-first-step, #add-new-step').on('click',{ stepTemplateHandler: this }, this.getStepTemplates); |
| 35 |
}; |
| 36 |
|
| 37 |
TemplateHandler.prototype.getStepTemplates = function(e) { |
| 38 |
e.preventDefault(); |
| 39 |
$(this).parents('.wpfnl').find('.wpfnl-modal').fadeIn(200); |
| 40 |
var payload = {'source' : 'remote'}, |
| 41 |
$_this = this, |
| 42 |
handler = e.data.stepTemplateHandler; |
| 43 |
wpAjaxHelperRequest( "wpfunnel-get-step-templates-data", payload ) |
| 44 |
.success( function( response ) { |
| 45 |
if( response.data ) { |
| 46 |
$("#wpfnl-create-step__template-wrapper").children(":not('.create-step__from-scratch, .wpfnl-loader__wrapper')").remove(); |
| 47 |
|
| 48 |
handler.initStepsTemplateView(response.data); |
| 49 |
|
| 50 |
var activeList = $('#wpfnl-create-step__filter-nav > li.active').attr('data-filter'); |
| 51 |
$('.wpfnl-create-step__template-wrapper .create-funnel__single-template.'+activeList+'').show(); |
| 52 |
} |
| 53 |
}) |
| 54 |
.error( function( response ) { |
| 55 |
console.log(response) |
| 56 |
}); |
| 57 |
}; |
| 58 |
|
| 59 |
/** |
| 60 |
* Init view for template library |
| 61 |
* |
| 62 |
* @param data |
| 63 |
* @since 1.0.0 |
| 64 |
*/ |
| 65 |
TemplateHandler.prototype.initStepsTemplateView = function (data) { |
| 66 |
// init template library model |
| 67 |
|
| 68 |
var TemplatesLibrary = Backbone.Model.extend({ |
| 69 |
defaults: { |
| 70 |
id: 0, |
| 71 |
title: '', |
| 72 |
link: '', |
| 73 |
slug: '', |
| 74 |
featured_media: 0, |
| 75 |
featured_image: '', |
| 76 |
steps_order: '', |
| 77 |
page_builder: '', |
| 78 |
categories: '', |
| 79 |
is_pro: false, |
| 80 |
type: '', |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
// init template library collection |
| 85 |
var TemplatesCollection = Backbone.Collection.extend({ |
| 86 |
model: TemplatesLibrary |
| 87 |
}); |
| 88 |
var Templates = new TemplatesCollection; |
| 89 |
|
| 90 |
if( data.steps ){ |
| 91 |
data.steps.map( m => { |
| 92 |
var template = new TemplatesLibrary(m); |
| 93 |
Templates.add(template) |
| 94 |
}); |
| 95 |
$('#wpfnl-create-funnel__filter-nav').css('display', 'flex'); |
| 96 |
}else { |
| 97 |
$('#wpfnl-create-funnel__filter-nav').hide(); |
| 98 |
} |
| 99 |
|
| 100 |
var TemplatesView = Backbone.View.extend({ |
| 101 |
el: '#wpfnl-create-step__template-wrapper', |
| 102 |
initialize: function() { |
| 103 |
this.render(); |
| 104 |
}, |
| 105 |
render: function() { |
| 106 |
Templates.each(function(model) { |
| 107 |
var template = new TemplateView({ |
| 108 |
model: model |
| 109 |
}); |
| 110 |
this.$el.prepend(template.render().el); |
| 111 |
}.bind(this)); |
| 112 |
this.$el.find('.wpfnl-loader__wrapper').fadeOut(); |
| 113 |
return this; |
| 114 |
} |
| 115 |
}); |
| 116 |
var TemplateView = Backbone.View.extend({ |
| 117 |
tagName: 'div', |
| 118 |
attributes : function () { |
| 119 |
var classes = this.model.get( 'type' ); |
| 120 |
return { |
| 121 |
class : 'create-funnel__single-template ' + classes, |
| 122 |
}; |
| 123 |
}, |
| 124 |
template: _.template($('#tmpl-wpfnl-step-templates-view').html()), |
| 125 |
render: function() { |
| 126 |
this.$el.html(this.template(this.model.attributes)); |
| 127 |
return this; |
| 128 |
}, |
| 129 |
events: { |
| 130 |
'click .wpfnl-import-step': 'importStep' |
| 131 |
}, |
| 132 |
importStep: function (e) { |
| 133 |
e.preventDefault(); |
| 134 |
var that = this, |
| 135 |
loader = $(this)[0].$el.find('.importar-loader'), |
| 136 |
funnel_id = $(this)[0].$el.find('.wpfnl-import-step').attr('data-funnel-id'); |
| 137 |
loader.fadeIn(); |
| 138 |
that.createStep( {'id' : this.model.get('id'), 'type' : this.model.get('type'), 'name' : this.model.get('type')}, funnel_id, loader); |
| 139 |
}, |
| 140 |
createStep: function (step, funnelID, loader) { |
| 141 |
var that = this, |
| 142 |
payload = { |
| 143 |
'step' : step, |
| 144 |
'funnelID' : funnelID, |
| 145 |
'source' : 'remote' |
| 146 |
}; |
| 147 |
wpAjaxHelperRequest( "wpfunnel-import-step", payload ) |
| 148 |
.success( function( response ) { |
| 149 |
loader.find('.title').text('Importing Step...'); |
| 150 |
that.afterStepCreationRedirect(response.stepID, funnelID); |
| 151 |
console.log(response) |
| 152 |
}) |
| 153 |
.error( function( response ) { |
| 154 |
console.log( "Uh, oh!" ); |
| 155 |
console.log( response.statusText ); |
| 156 |
}); |
| 157 |
}, |
| 158 |
afterStepCreationRedirect: function (stepID, funnelID) { |
| 159 |
var payload = { |
| 160 |
'stepID' : stepID, |
| 161 |
'funnelID' : funnelID, |
| 162 |
'source' : 'remote', |
| 163 |
}; |
| 164 |
wpAjaxHelperRequest( "wpfunnel-after-step-creation", payload ) |
| 165 |
.success( function( response ) { |
| 166 |
window.location = response.redirectLink |
| 167 |
}) |
| 168 |
.error( function( response ) { |
| 169 |
console.log(response) |
| 170 |
}); |
| 171 |
} |
| 172 |
}); |
| 173 |
new TemplatesView; |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* init template library for funnel showcase |
| 179 |
*/ |
| 180 |
TemplateHandler.prototype.initTemplateLibrary = function () { |
| 181 |
var payload = {'source' : 'remote'}, |
| 182 |
$_this = this; |
| 183 |
wpAjaxHelperRequest( "wpfunnel-get-templates-data", payload ) |
| 184 |
.success( function( response ) { |
| 185 |
if( response.data ) { |
| 186 |
$_this.initTemplateView(response.data); |
| 187 |
$_this.initTemplateCategoryView(response.data); |
| 188 |
} |
| 189 |
}) |
| 190 |
.error( function( response ) { |
| 191 |
console.log(response) |
| 192 |
}); |
| 193 |
}; |
| 194 |
|
| 195 |
/** |
| 196 |
* Init view for template library |
| 197 |
* |
| 198 |
* @param data |
| 199 |
* @since 1.0.0 |
| 200 |
*/ |
| 201 |
TemplateHandler.prototype.initTemplateView = function (data) { |
| 202 |
// init template library model |
| 203 |
var TemplatesLibrary = Backbone.Model.extend({ |
| 204 |
defaults: { |
| 205 |
id: 0, |
| 206 |
title: '', |
| 207 |
link: '', |
| 208 |
slug: '', |
| 209 |
featured_media: 0, |
| 210 |
featured_image: '', |
| 211 |
steps_order: '', |
| 212 |
page_builder: '', |
| 213 |
categories: '', |
| 214 |
is_pro: false, |
| 215 |
} |
| 216 |
}); |
| 217 |
|
| 218 |
// init template library collection |
| 219 |
var TemplatesCollection = Backbone.Collection.extend({ |
| 220 |
model: TemplatesLibrary |
| 221 |
}); |
| 222 |
var Templates = new TemplatesCollection; |
| 223 |
|
| 224 |
if( data.templates ){ |
| 225 |
data.templates.map( m => { |
| 226 |
var template = new TemplatesLibrary(m); |
| 227 |
Templates.add(template) |
| 228 |
}); |
| 229 |
|
| 230 |
$('#wpfnl-create-funnel__filter-nav').css('display', 'flex'); |
| 231 |
}else { |
| 232 |
$('#wpfnl-create-funnel__filter-nav').hide(); |
| 233 |
} |
| 234 |
|
| 235 |
var TemplatesView = Backbone.View.extend({ |
| 236 |
el: '#wpfnl-create-funnel__template-wrapper', |
| 237 |
initialize: function() { |
| 238 |
this.render(); |
| 239 |
}, |
| 240 |
render: function() { |
| 241 |
Templates.each(function(model) { |
| 242 |
var template = new TemplateView({ |
| 243 |
model: model |
| 244 |
}); |
| 245 |
this.$el.prepend(template.render().el); |
| 246 |
}.bind(this)); |
| 247 |
this.$el.find('.wpfnl-loader__wrapper').fadeOut(); |
| 248 |
return this; |
| 249 |
} |
| 250 |
}); |
| 251 |
var TemplateView = Backbone.View.extend({ |
| 252 |
tagName: 'div', |
| 253 |
// className : 'create-funnel__single-template', |
| 254 |
attributes : function () { |
| 255 |
var categories = this.model.get( 'categories' ), |
| 256 |
classes = []; |
| 257 |
if($.isArray(categories)) { |
| 258 |
classes = categories.map((cat) => { |
| 259 |
return cat.slug; |
| 260 |
}); |
| 261 |
} |
| 262 |
return { |
| 263 |
class : 'create-funnel__single-template ' + classes.join(" "), |
| 264 |
}; |
| 265 |
}, |
| 266 |
template: _.template($('#tmpl-wpfnl-templates-view').html()), |
| 267 |
render: function() { |
| 268 |
this.$el.html(this.template(this.model.attributes)); |
| 269 |
return this; |
| 270 |
}, |
| 271 |
events: { |
| 272 |
'click .wpfnl-import-funnel': 'importFunnel' |
| 273 |
}, |
| 274 |
importFunnel: function (e) { |
| 275 |
e.preventDefault(); |
| 276 |
var that = this, |
| 277 |
loader = $(this)[0].$el.find('.importar-loader'); |
| 278 |
var steps = this.model.get('steps_order'), |
| 279 |
payload = { |
| 280 |
'steps' : steps, |
| 281 |
'source' : 'remote', |
| 282 |
}; |
| 283 |
loader.fadeIn(); |
| 284 |
wpAjaxHelperRequest( "wpfunnel-import-funnel", payload ) |
| 285 |
.success( function( response ) { |
| 286 |
var looper = $.Deferred().resolve(), |
| 287 |
first_step_id = 0; |
| 288 |
$.when.apply($, $.map(steps, function(step, index) { |
| 289 |
looper = looper.then(function() { |
| 290 |
// trigger ajax call with item data |
| 291 |
return that.createStep(step, response.funnelID, loader, index); |
| 292 |
}); |
| 293 |
return looper; |
| 294 |
})).then(function() { |
| 295 |
// run this after all ajax calls have completed |
| 296 |
console.log('Done!'); |
| 297 |
that.afterFunnelCreationRedirect(response.funnelID); |
| 298 |
}); |
| 299 |
}) |
| 300 |
.error( function( response ) { |
| 301 |
console.log( "Uh, oh!" ); |
| 302 |
console.log( response.statusText ); |
| 303 |
}); |
| 304 |
}, |
| 305 |
createStep: function (step, funnelID, loader, index) { |
| 306 |
var deferred = $.Deferred(), |
| 307 |
payload = { |
| 308 |
'step' : step, |
| 309 |
'funnelID' : funnelID, |
| 310 |
'source' : 'remote' |
| 311 |
}; |
| 312 |
wpAjaxHelperRequest( "wpfunnel-import-step", payload ) |
| 313 |
.success( function( response ) { |
| 314 |
loader.find('.title').text('Importing Step '+ (parseInt(index)+1) ); |
| 315 |
console.log(response) |
| 316 |
deferred.resolve(response); |
| 317 |
}) |
| 318 |
.error( function( response ) { |
| 319 |
console.log( "Uh, oh!" ); |
| 320 |
console.log( response.statusText ); |
| 321 |
deferred.reject(response); |
| 322 |
}); |
| 323 |
return deferred.promise(); |
| 324 |
}, |
| 325 |
afterFunnelCreationRedirect: function (funnelId) { |
| 326 |
var payload = { |
| 327 |
'funnelID' : funnelId, |
| 328 |
'source' : 'remote' |
| 329 |
}; |
| 330 |
wpAjaxHelperRequest( "wpfunnel-after-funnel-creation", payload ) |
| 331 |
.success( function( response ) { |
| 332 |
window.location = response.redirectLink |
| 333 |
}) |
| 334 |
.error( function( response ) { |
| 335 |
console.log(response) |
| 336 |
}); |
| 337 |
} |
| 338 |
}); |
| 339 |
new TemplatesView; |
| 340 |
}; |
| 341 |
|
| 342 |
/** |
| 343 |
* Init view for template library |
| 344 |
* category filter |
| 345 |
* |
| 346 |
* @param data |
| 347 |
* @since 1.0.0 |
| 348 |
*/ |
| 349 |
TemplateHandler.prototype.initTemplateCategoryView = function (data) { |
| 350 |
var CategoriesLibrary = Backbone.Model.extend(); |
| 351 |
var CategoriesCollection = Backbone.Collection.extend({ |
| 352 |
model: CategoriesLibrary |
| 353 |
}); |
| 354 |
var Categories = new CategoriesCollection; |
| 355 |
|
| 356 |
if( data.categories ){ |
| 357 |
data.categories.map( m => { |
| 358 |
var category = new CategoriesLibrary(m); |
| 359 |
Categories.add(category) |
| 360 |
}); |
| 361 |
} |
| 362 |
var CategoriesView = Backbone.View.extend({ |
| 363 |
el: '#wpfnl-create-funnel__filter-nav', |
| 364 |
|
| 365 |
initialize: function() { |
| 366 |
this.render(); |
| 367 |
}, |
| 368 |
render: function() { |
| 369 |
Categories.each(function(model, i) { |
| 370 |
var template = new CategoryView({ |
| 371 |
model: model |
| 372 |
}); |
| 373 |
|
| 374 |
this.$el.append(template.render().el); |
| 375 |
if ( i === 0) { |
| 376 |
this.$el.find('li').eq(0).addClass('active'); |
| 377 |
} |
| 378 |
}.bind(this)); |
| 379 |
|
| 380 |
$('.wpfnl-create-funnel__filter-nav > li').on('click', function(){ |
| 381 |
$(this).addClass('active').siblings().removeClass('active'); |
| 382 |
const filterVal = $(this).attr('data-filter'); |
| 383 |
if( filterVal == 'all' ){ |
| 384 |
$('.create-funnel__single-template').show(); |
| 385 |
}else{ |
| 386 |
$('.create-funnel__single-template').not('.'+filterVal).not('.create-funnel__from-scratch').hide(); |
| 387 |
$('.create-funnel__single-template').filter('.'+filterVal).show(); |
| 388 |
} |
| 389 |
}); |
| 390 |
return this; |
| 391 |
} |
| 392 |
}); |
| 393 |
var CategoryView = Backbone.View.extend({ |
| 394 |
tagName: 'li', |
| 395 |
template: _.template($('#tmpl-wpfnl-templates-category-filter').html()), |
| 396 |
render: function() { |
| 397 |
this.$el.html(this.template(this.model.attributes)); |
| 398 |
$(this.el).attr('data-filter',this.model.get('slug')).addClass(this.class); |
| 399 |
return this; |
| 400 |
}, |
| 401 |
events: { |
| 402 |
'click .click': 'getModel' |
| 403 |
}, |
| 404 |
getModel: function () { |
| 405 |
return this.model; |
| 406 |
}, |
| 407 |
}); |
| 408 |
new CategoriesView; |
| 409 |
}; |
| 410 |
|
| 411 |
new TemplateHandler(); |
| 412 |
}); |
| 413 |
|
| 414 |
|