| 1 |
var plugin_groups_canvas = false, |
| 2 |
plorg_get_config_object, |
| 3 |
plorg_record_change, |
| 4 |
plorg_canvas_reset, |
| 5 |
plorg_canvas_init, |
| 6 |
plorg_rebuild_canvas, |
| 7 |
plorg_add_node, |
| 8 |
plorg_get_default_setting, |
| 9 |
plorg_code_editor, |
| 10 |
plorg_handle_save, |
| 11 |
plorg_rebuild_magics, |
| 12 |
plorg_init_magic_tags, |
| 13 |
plorg_config_object = {}, |
| 14 |
plorg_magic_tags = []; |
| 15 |
|
| 16 |
jQuery( function($){ |
| 17 |
|
| 18 |
|
| 19 |
plorg_handle_save = function( obj ){ |
| 20 |
|
| 21 |
var notice; |
| 22 |
|
| 23 |
if( obj.data.success ){ |
| 24 |
notice = $('.updated_notice_box'); |
| 25 |
}else{ |
| 26 |
notice = $('.error_notice_box'); |
| 27 |
} |
| 28 |
|
| 29 |
notice.stop().animate({top: 32}, 200, function(){ |
| 30 |
setTimeout( function(){ |
| 31 |
notice.stop().animate({top: -175}, 200); |
| 32 |
}, 2000); |
| 33 |
}); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
plorg_init_magic_tags = function(){ |
| 39 |
//init magic tags |
| 40 |
var magicfields = jQuery('.magic-tag-enabled'); |
| 41 |
|
| 42 |
magicfields.each(function(k,v){ |
| 43 |
var input = jQuery(v); |
| 44 |
|
| 45 |
if(input.hasClass('magic-tag-init-bound')){ |
| 46 |
var currentwrapper = input.parent().find('.magic-tag-init'); |
| 47 |
if(!input.is(':visible')){ |
| 48 |
currentwrapper.hide(); |
| 49 |
}else{ |
| 50 |
currentwrapper.show(); |
| 51 |
} |
| 52 |
return; |
| 53 |
} |
| 54 |
var magictag = jQuery('<span class="dashicons dashicons-editor-code magic-tag-init"></span>'), |
| 55 |
wrapper = jQuery('<span style="position:relative;display:inline-block; width:100%;"></span>'); |
| 56 |
|
| 57 |
if(input.is('input')){ |
| 58 |
magictag.css('borderBottom', 'none'); |
| 59 |
} |
| 60 |
|
| 61 |
if(input.hasClass('plugin-groups-conditional-value-field')){ |
| 62 |
wrapper.width('auto'); |
| 63 |
} |
| 64 |
|
| 65 |
//input.wrap(wrapper); |
| 66 |
magictag.insertAfter(input); |
| 67 |
input.addClass('magic-tag-init-bound'); |
| 68 |
if(!input.is(':visible')){ |
| 69 |
magictag.hide(); |
| 70 |
}else{ |
| 71 |
magictag.show(); |
| 72 |
} |
| 73 |
}); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
// internal function declarationas |
| 78 |
plorg_get_config_object = function(el){ |
| 79 |
// new sync first |
| 80 |
$('#plugin_groups-id').trigger('change'); |
| 81 |
var clicked = $(el), |
| 82 |
config = $('#plugin-groups-live-config').val(), |
| 83 |
required = $('[required]'), |
| 84 |
clean = true; |
| 85 |
|
| 86 |
for( var input = 0; input < required.length; input++ ){ |
| 87 |
if( required[input].value.length <= 0 && $( required[input] ).is(':visible') ){ |
| 88 |
$( required[input] ).addClass('plugin-groups-input-error'); |
| 89 |
clean = false; |
| 90 |
}else{ |
| 91 |
$( required[input] ).removeClass('plugin-groups-input-error'); |
| 92 |
} |
| 93 |
} |
| 94 |
if( clean ){ |
| 95 |
plugin_groups_canvas = config; |
| 96 |
} |
| 97 |
clicked.data( 'config', config ); |
| 98 |
return clean; |
| 99 |
} |
| 100 |
|
| 101 |
plorg_record_change = function(){ |
| 102 |
// hook and rebuild the fields list |
| 103 |
jQuery(document).trigger('record_change'); |
| 104 |
jQuery('#plugin_groups-id').trigger('change'); |
| 105 |
if( plorg_config_object ){ |
| 106 |
jQuery('#plugin-groups-field-sync').trigger('refresh'); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
plorg_canvas_reset = function(el, ev){ |
| 111 |
// handy to add things before builing/rebuilding the canvas |
| 112 |
// return false to stop. |
| 113 |
|
| 114 |
// remove editors and quicktags |
| 115 |
if ( typeof tinymce !== 'undefined' ) { |
| 116 |
for ( ed in tinymce.editors ) { |
| 117 |
tinymce.editors[ed].remove(); |
| 118 |
} |
| 119 |
} |
| 120 |
if ( typeof QTags !== 'undefined' ) { |
| 121 |
QTags.buttonsInitDone = false; |
| 122 |
QTags.instances = {}; |
| 123 |
} |
| 124 |
|
| 125 |
return true |
| 126 |
} |
| 127 |
|
| 128 |
plorg_canvas_init = function(){ |
| 129 |
|
| 130 |
if( !plugin_groups_canvas ){ |
| 131 |
// bind changes |
| 132 |
jQuery('#plugin-groups-main-canvas').on('keydown keyup change','input, select, textarea', function(e) { |
| 133 |
plorg_config_object = jQuery('#plugin-groups-main-form').formJSON(); // perhaps load into memory to keep it live. |
| 134 |
jQuery('#plugin-groups-live-config').val( JSON.stringify( plorg_config_object ) ).trigger('change'); |
| 135 |
}); |
| 136 |
// bind editor |
| 137 |
plorg_init_editor(); |
| 138 |
plugin_groups_canvas = jQuery('#plugin-groups-live-config').val(); |
| 139 |
plorg_config_object = JSON.parse( plugin_groups_canvas ); // perhaps load into memory to keep it live. |
| 140 |
// wp_editor |
| 141 |
if ( typeof tinymce !== 'undefined' ) { |
| 142 |
tinymce.on('AddEditor', function(e) { |
| 143 |
|
| 144 |
e.editor.on('keyup', function (e) { |
| 145 |
this.save(); |
| 146 |
jQuery( this.targetElm ).trigger('keyup'); |
| 147 |
}); |
| 148 |
e.editor.on('change', function (e) { |
| 149 |
this.save(); |
| 150 |
jQuery( this.targetElm ).trigger('change'); |
| 151 |
}); |
| 152 |
}); |
| 153 |
} |
| 154 |
} |
| 155 |
if( $('.color-field').length ){ |
| 156 |
$('.color-field').wpColorPicker({ |
| 157 |
change: function(obj){ |
| 158 |
|
| 159 |
var trigger = $(this); |
| 160 |
if( trigger.data('target') ){ |
| 161 |
$( trigger.data('target') ).css( trigger.data('style'), trigger.val() ); |
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
}); |
| 166 |
} |
| 167 |
if( $('.plugin-groups-group-wrapper').length ){ |
| 168 |
$( ".plugin-groups-group-wrapper" ).sortable({ |
| 169 |
handle: ".sortable-item", |
| 170 |
start: function(ev, ui){ |
| 171 |
ui.item.data('moved', true); |
| 172 |
ui.placeholder.css( 'borderWidth', ui.item.css( 'borderTopWidth') ); |
| 173 |
ui.placeholder.css( 'margin', ui.item.css( 'marginTop') ); |
| 174 |
}, |
| 175 |
update: function(ev, ui){ |
| 176 |
jQuery('#plugin_groups-id').trigger('change'); |
| 177 |
} |
| 178 |
}); |
| 179 |
$( ".plugin-groups-fields-list" ).sortable({ |
| 180 |
handle: ".sortable-item", |
| 181 |
update: function(){ |
| 182 |
jQuery('#plugin_groups-id').trigger('change'); |
| 183 |
} |
| 184 |
}); |
| 185 |
} |
| 186 |
|
| 187 |
//wp_editor refresh |
| 188 |
plorg_init_wp_editors(); |
| 189 |
|
| 190 |
// live change init |
| 191 |
$('[data-init-change]').trigger('change'); |
| 192 |
$('[data-auto-focus]').focus().select(); |
| 193 |
|
| 194 |
// rebuild tags |
| 195 |
plorg_rebuild_magics(); |
| 196 |
jQuery(document).trigger('canvas_init'); |
| 197 |
} |
| 198 |
|
| 199 |
plorg_add_node = function(node, node_default){ |
| 200 |
var id = 'nd' + Math.round(Math.random() * 99866) + Math.round(Math.random() * 99866), |
| 201 |
newnode = { "_id" : id }, |
| 202 |
nodes = node.split('.'), |
| 203 |
node_point_record = nodes.join('.') + '.' + id, |
| 204 |
node_defaults = JSON.parse( '{ "_id" : "' + id + '", "_node_point" : "' + node_point_record + '" }' ); |
| 205 |
|
| 206 |
if( node_default && typeof node_default === 'object' ){ |
| 207 |
$.extend( true, node_defaults, node_default ); |
| 208 |
} |
| 209 |
var node_string = '{ "' + nodes.join( '": { "') + '" : { "' + id + '" : ' + JSON.stringify( node_defaults ); |
| 210 |
for( var cls = 0; cls <= nodes.length; cls++){ |
| 211 |
node_string += '}'; |
| 212 |
} |
| 213 |
var new_nodes = JSON.parse( node_string ); |
| 214 |
$.extend( true, plorg_config_object, new_nodes ); |
| 215 |
}; |
| 216 |
|
| 217 |
plorg_get_default_setting = function(obj){ |
| 218 |
|
| 219 |
var id = 'nd' + Math.round(Math.random() * 99866) + Math.round(Math.random() * 99866), |
| 220 |
trigger = ( obj.trigger ? obj.trigger : obj.params.trigger ), |
| 221 |
sub_id = ( trigger.data('group') ? trigger.data('group') : 'nd' + Math.round(Math.random() * 99766) + Math.round(Math.random() * 99866) ), |
| 222 |
nodes; |
| 223 |
|
| 224 |
|
| 225 |
// add simple node |
| 226 |
if( trigger.data('addNode') ){ |
| 227 |
// new node? add one |
| 228 |
plorg_add_node( trigger.data('addNode'), trigger.data('nodeDefault') ); |
| 229 |
} |
| 230 |
// remove simple node (all) |
| 231 |
if( trigger.data('removeNode') ){ |
| 232 |
// new node? add one |
| 233 |
if( plorg_config_object[trigger.data('removeNode')] ){ |
| 234 |
delete plorg_config_object[trigger.data('removeNode')]; |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
switch( trigger.data('script') ){ |
| 240 |
case "add-to-object": |
| 241 |
// add to core object |
| 242 |
//plorg_config_object.entry_name = obj.data.value; // ajax method |
| 243 |
|
| 244 |
break; |
| 245 |
case "add-field-node": |
| 246 |
// add to core object |
| 247 |
if( !plorg_config_object[trigger.data('slug')][trigger.data('group')].field ){ |
| 248 |
plorg_config_object[trigger.data('slug')][trigger.data('group')].field = {}; |
| 249 |
} |
| 250 |
plorg_config_object[trigger.data('slug')][trigger.data('group')].field[id] = { "_id": id, 'name': 'new field', 'slug': 'new_field' }; |
| 251 |
plorg_config_object.open_field = id; |
| 252 |
break; |
| 253 |
} |
| 254 |
|
| 255 |
plorg_rebuild_canvas(); |
| 256 |
|
| 257 |
}; |
| 258 |
|
| 259 |
plorg_rebuild_canvas = function(){ |
| 260 |
jQuery('#plugin-groups-live-config').val( JSON.stringify( plorg_config_object ) ); |
| 261 |
jQuery('#plugin-groups-field-sync').trigger('refresh'); |
| 262 |
}; |
| 263 |
// sutocomplete category |
| 264 |
$.widget( "custom.catcomplete", $.ui.autocomplete, { |
| 265 |
_create: function() { |
| 266 |
this._super(); |
| 267 |
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" ); |
| 268 |
}, |
| 269 |
_renderMenu: function( ul, items ) { |
| 270 |
var that = this, |
| 271 |
currentCategory = ""; |
| 272 |
$.each( items, function( index, item ) { |
| 273 |
var li; |
| 274 |
if ( item.category != currentCategory ) { |
| 275 |
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" ); |
| 276 |
currentCategory = item.category; |
| 277 |
} |
| 278 |
li = that._renderItemData( ul, item ); |
| 279 |
if ( item.category ) { |
| 280 |
li.attr( "aria-label", item.category + " : " + item.label ); |
| 281 |
} |
| 282 |
}); |
| 283 |
} |
| 284 |
}); |
| 285 |
plorg_rebuild_magics = function(){ |
| 286 |
|
| 287 |
function split( val ) { |
| 288 |
return val.split( / \s*/ ); |
| 289 |
} |
| 290 |
function extractLast( term ) { |
| 291 |
return split( term ).pop(); |
| 292 |
} |
| 293 |
$( ".magic-tag-enabled" ).bind( "keydown", function( event ) { |
| 294 |
if ( event.keyCode === $.ui.keyCode.TAB && $( this ).catcomplete( "instance" ).menu.active ) { |
| 295 |
event.preventDefault(); |
| 296 |
} |
| 297 |
}).catcomplete({ |
| 298 |
minLength: 0, |
| 299 |
source: function( request, response ) { |
| 300 |
// delegate back to autocomplete, but extract the last term |
| 301 |
plorg_magic_tags = []; |
| 302 |
var category = '', |
| 303 |
tags = $('.plugin-groups-magic-tags-definitions'); |
| 304 |
|
| 305 |
if( tags.length ){ |
| 306 |
|
| 307 |
for( var tag_set = 0; tag_set < tags.length; tag_set++ ){ |
| 308 |
|
| 309 |
var magic_tags; |
| 310 |
|
| 311 |
category = 'Magic Tags'; |
| 312 |
|
| 313 |
if( $( tags[ tag_set ] ).data('category') ){ |
| 314 |
category = $( tags[ tag_set ] ).data('category'); |
| 315 |
} |
| 316 |
// set internal tags |
| 317 |
try{ |
| 318 |
magic_tags = JSON.parse( tags[ tag_set ].value ); |
| 319 |
} catch (e) { |
| 320 |
magic_tags = [ $(tags[ tag_set ]).data('tag') ]; |
| 321 |
} |
| 322 |
|
| 323 |
var display_label; |
| 324 |
for( f = 0; f < magic_tags.length; f++ ){ |
| 325 |
display_label = magic_tags[f].split( '*' ); |
| 326 |
if( display_label[1] ){ |
| 327 |
display_label = display_label[0] + '*'; |
| 328 |
} |
| 329 |
plorg_magic_tags.push( { label: '{' + display_label + '}',value: '{' + magic_tags[f] + '}', category: category } ); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
response( $.ui.autocomplete.filter( plorg_magic_tags, extractLast( request.term ) ) ); |
| 336 |
}, |
| 337 |
focus: function() { |
| 338 |
// prevent value inserted on focus |
| 339 |
return false; |
| 340 |
}, |
| 341 |
select: function( event, ui ) { |
| 342 |
var terms = split( this.value ); |
| 343 |
// remove the current input |
| 344 |
terms.pop(); |
| 345 |
// add the selected item |
| 346 |
terms.push( ui.item.value ); |
| 347 |
// add placeholder to get the comma-and-space at the end |
| 348 |
//terms.push( "" ); |
| 349 |
this.value = terms.join( " " ); |
| 350 |
return false; |
| 351 |
} |
| 352 |
}); |
| 353 |
} |
| 354 |
|
| 355 |
plorg_init_wp_editors = function(){ |
| 356 |
|
| 357 |
if( typeof tinyMCEPreInit === 'undefined'){ |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
var ed, init, edId, qtId, firstInit, wrapper; |
| 362 |
|
| 363 |
if ( typeof tinymce !== 'undefined' ) { |
| 364 |
|
| 365 |
for ( edId in tinyMCEPreInit.mceInit ) { |
| 366 |
|
| 367 |
if ( firstInit ) { |
| 368 |
init = tinyMCEPreInit.mceInit[edId] = tinymce.extend( {}, firstInit, tinyMCEPreInit.mceInit[edId] ); |
| 369 |
} else { |
| 370 |
init = firstInit = tinyMCEPreInit.mceInit[edId]; |
| 371 |
} |
| 372 |
|
| 373 |
wrapper = tinymce.DOM.select( '#wp-' + edId + '-wrap' )[0]; |
| 374 |
|
| 375 |
if ( ( tinymce.DOM.hasClass( wrapper, 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( edId ) ) && |
| 376 |
! init.wp_skip_init ) { |
| 377 |
|
| 378 |
try { |
| 379 |
tinymce.init( init ); |
| 380 |
|
| 381 |
if ( ! window.wpActiveEditor ) { |
| 382 |
window.wpActiveEditor = edId; |
| 383 |
} |
| 384 |
} catch(e){} |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
for ( qtId in tinyMCEPreInit.qtInit ) { |
| 390 |
try { |
| 391 |
quicktags( tinyMCEPreInit.qtInit[qtId] ); |
| 392 |
|
| 393 |
if ( ! window.wpActiveEditor ) { |
| 394 |
window.wpActiveEditor = qtId; |
| 395 |
} |
| 396 |
} catch(e){}; |
| 397 |
} |
| 398 |
|
| 399 |
jQuery('.wp-editor-wrap').on( 'click.wp-editor', function() { |
| 400 |
|
| 401 |
if ( this.id ) { |
| 402 |
window.wpActiveEditor = this.id.slice( 3, -5 ); |
| 403 |
} |
| 404 |
}); |
| 405 |
|
| 406 |
|
| 407 |
} |
| 408 |
|
| 409 |
// trash |
| 410 |
$(document).on('click', '.plugin-groups-card-actions .confirm a', function(e){ |
| 411 |
e.preventDefault(); |
| 412 |
var parent = $(this).closest('.plugin-groups-card-content'); |
| 413 |
actions = parent.find('.row-actions'); |
| 414 |
|
| 415 |
actions.slideToggle(300); |
| 416 |
}); |
| 417 |
|
| 418 |
// bind slugs |
| 419 |
$(document).on('keyup change', '[data-format="slug"]', function(e){ |
| 420 |
|
| 421 |
var input = $(this); |
| 422 |
|
| 423 |
if( input.data('master') && input.prop('required') && this.value.length <= 0 && e.type === "change" ){ |
| 424 |
this.value = $(input.data('master')).val().replace(/[^a-z0-9]/gi, '_').toLowerCase(); |
| 425 |
if( this.value.length ){ |
| 426 |
input.trigger('change'); |
| 427 |
} |
| 428 |
return; |
| 429 |
} |
| 430 |
|
| 431 |
this.value = this.value.replace(/[^a-z0-9]/gi, '_').toLowerCase(); |
| 432 |
}); |
| 433 |
|
| 434 |
// bind label update |
| 435 |
$(document).on('keyup change', '[data-sync]', function(){ |
| 436 |
var input = $(this), |
| 437 |
syncs = $(input.data('sync')); |
| 438 |
|
| 439 |
syncs.each(function(){ |
| 440 |
var sync = $(this); |
| 441 |
|
| 442 |
if( sync.is('input') ){ |
| 443 |
sync.val( input.val() ).trigger('change'); |
| 444 |
}else{ |
| 445 |
sync.text(input.val()); |
| 446 |
} |
| 447 |
}); |
| 448 |
}); |
| 449 |
// bind toggles |
| 450 |
$(document).on('click', '[data-toggle]', function(){ |
| 451 |
|
| 452 |
var toggle = $(this).data('toggle'), |
| 453 |
target = $(toggle); |
| 454 |
|
| 455 |
target.each(function(){ |
| 456 |
var tog = $(this); |
| 457 |
if( tog.is(':checkbox') || tog.is(':radio') ){ |
| 458 |
if( tog.prop('checked') ){ |
| 459 |
tog.prop('checked', false); |
| 460 |
}else{ |
| 461 |
tog.prop('checked', true); |
| 462 |
} |
| 463 |
plorg_record_change(); |
| 464 |
}else{ |
| 465 |
tog.toggle(); |
| 466 |
} |
| 467 |
}); |
| 468 |
|
| 469 |
}); |
| 470 |
|
| 471 |
// bind tabs |
| 472 |
$(document).on('click', '.plugin-groups-nav-tabs a', function(e){ |
| 473 |
|
| 474 |
e.preventDefault(); |
| 475 |
var clicked = $(this), |
| 476 |
tab_id = clicked.attr('href'), |
| 477 |
required = $('[required]'), |
| 478 |
clean = true; |
| 479 |
|
| 480 |
for( var input = 0; input < required.length; input++ ){ |
| 481 |
if( required[input].value.length <= 0 && $( required[input] ).is(':visible') ){ |
| 482 |
$( required[input] ).addClass('plugin-groups-input-error'); |
| 483 |
clean = false; |
| 484 |
}else{ |
| 485 |
$( required[input] ).removeClass('plugin-groups-input-error'); |
| 486 |
} |
| 487 |
} |
| 488 |
if( !clean ){ |
| 489 |
return; |
| 490 |
} |
| 491 |
|
| 492 |
if( plorg_code_editor ){ |
| 493 |
plorg_code_editor.toTextArea(); |
| 494 |
plorg_code_editor = false; |
| 495 |
} |
| 496 |
|
| 497 |
if( $( tab_id ).find('.plugin-groups-code-editor').length ){ |
| 498 |
|
| 499 |
plorg_init_editor( $( tab_id ).find('.plugin-groups-code-editor').prop('id') ); |
| 500 |
plorg_code_editor.refresh(); |
| 501 |
plorg_code_editor.focus(); |
| 502 |
} |
| 503 |
|
| 504 |
jQuery('#plugin-groups-active-tab').val(tab_id).trigger('change'); |
| 505 |
plorg_record_change(); |
| 506 |
}); |
| 507 |
|
| 508 |
// row remover global neeto |
| 509 |
$(document).on('click', '[data-remove-parent]', function(e){ |
| 510 |
var clicked = $(this), |
| 511 |
parent = clicked.closest(clicked.data('removeParent')); |
| 512 |
if( clicked.data('confirm') ){ |
| 513 |
if( !confirm(clicked.data('confirm')) ){ |
| 514 |
return; |
| 515 |
} |
| 516 |
} |
| 517 |
parent.remove(); |
| 518 |
plorg_record_change(); |
| 519 |
}); |
| 520 |
|
| 521 |
// row remover global neeto |
| 522 |
$(document).on('click', '[data-remove-element]', function(e){ |
| 523 |
var clicked = $(this), |
| 524 |
elements = $(clicked.data('removeElement')); |
| 525 |
if( clicked.data('confirm') ){ |
| 526 |
if( !confirm(clicked.data('confirm')) ){ |
| 527 |
return; |
| 528 |
} |
| 529 |
} |
| 530 |
elements.remove(); |
| 531 |
plorg_record_change(); |
| 532 |
}); |
| 533 |
|
| 534 |
// init tags |
| 535 |
$('body').on('click', '.magic-tag-init', function(e){ |
| 536 |
var clicked = $(this), |
| 537 |
input = clicked.prev(); |
| 538 |
|
| 539 |
input.focus().trigger('init.magic'); |
| 540 |
|
| 541 |
}); |
| 542 |
|
| 543 |
// initialize live sync rebuild |
| 544 |
$(document).on('change', '[data-live-sync]', function(e){ |
| 545 |
plorg_record_change(); |
| 546 |
}); |
| 547 |
|
| 548 |
// initialise baldrick triggers |
| 549 |
$('.wp-baldrick').baldrick({ |
| 550 |
request : ajaxurl, |
| 551 |
method : 'POST', |
| 552 |
before : function(el){ |
| 553 |
|
| 554 |
var tr = $(el); |
| 555 |
|
| 556 |
if( tr.data('addNode') && !tr.data('request') ){ |
| 557 |
tr.data('request', 'plorg_get_default_setting'); |
| 558 |
} |
| 559 |
} |
| 560 |
}); |
| 561 |
|
| 562 |
|
| 563 |
window.onbeforeunload = function(e) { |
| 564 |
|
| 565 |
if( plugin_groups_canvas && plugin_groups_canvas !== jQuery('#plugin-groups-live-config').val() ){ |
| 566 |
return true; |
| 567 |
} |
| 568 |
}; |
| 569 |
|
| 570 |
|
| 571 |
}); |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
function plorg_init_editor(el){ |
| 580 |
if( !jQuery('#' + el).length ){ |
| 581 |
return; |
| 582 |
} |
| 583 |
// custom modes |
| 584 |
var mustache = function(plugin_groups_init, state) { |
| 585 |
|
| 586 |
var ch; |
| 587 |
|
| 588 |
if (plugin_groups_init.match("{{")) { |
| 589 |
while ((ch = plugin_groups_init.next()) != null){ |
| 590 |
if (ch == "}" && plugin_groups_init.next() == "}") break; |
| 591 |
} |
| 592 |
plugin_groups_init.eat("}"); |
| 593 |
return "mustache"; |
| 594 |
} |
| 595 |
/* |
| 596 |
if (plugin_groups_init.match("{")) { |
| 597 |
while ((ch = plugin_groups_init.next()) != null) |
| 598 |
if (ch == "}") break; |
| 599 |
plugin_groups_init.eat("}"); |
| 600 |
return "mustacheinternal"; |
| 601 |
}*/ |
| 602 |
if (plugin_groups_init.match("%")) { |
| 603 |
while ((ch = plugin_groups_init.next()) != null) |
| 604 |
if (ch == "%") break; |
| 605 |
plugin_groups_init.eat("%"); |
| 606 |
return "command"; |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
if (plugin_groups_init.match("[[")) { |
| 611 |
while ((ch = plugin_groups_init.next()) != null) |
| 612 |
if (ch == "]" && plugin_groups_init.next() == "]") break; |
| 613 |
plugin_groups_init.eat("]"); |
| 614 |
return "include"; |
| 615 |
}*/ |
| 616 |
while (plugin_groups_init.next() != null && |
| 617 |
//!plugin_groups_init.match("{", false) && |
| 618 |
!plugin_groups_init.match("{{", false) && |
| 619 |
!plugin_groups_init.match("%", false) ) {} |
| 620 |
return null; |
| 621 |
}; |
| 622 |
|
| 623 |
var options = { |
| 624 |
lineNumbers: true, |
| 625 |
matchBrackets: true, |
| 626 |
tabSize: 2, |
| 627 |
indentUnit: 2, |
| 628 |
indentWithTabs: true, |
| 629 |
enterMode: "keep", |
| 630 |
tabMode: "shift", |
| 631 |
lineWrapping: true, |
| 632 |
extraKeys: {"Ctrl-Space": "autocomplete"}, |
| 633 |
}; |
| 634 |
// base mode |
| 635 |
|
| 636 |
CodeMirror.defineMode("mustache", function(config, parserConfig) { |
| 637 |
var mustacheOverlay = { |
| 638 |
token: mustache |
| 639 |
}; |
| 640 |
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || 'text/html' ), mustacheOverlay); |
| 641 |
}); |
| 642 |
options.mode = jQuery('#' + el).data('mode') ? jQuery('#' + el).data('mode') : "mustache"; |
| 643 |
|
| 644 |
plorg_code_editor = CodeMirror.fromTextArea(document.getElementById(el), options); |
| 645 |
plorg_code_editor.on('keyup', tagFields); |
| 646 |
plorg_code_editor.on('blur', function(cm){ |
| 647 |
cm.save(); |
| 648 |
jQuery( cm.getInputField() ).trigger('change'); |
| 649 |
}); |
| 650 |
|
| 651 |
return plorg_code_editor; |
| 652 |
|
| 653 |
} |
| 654 |
(function() { |
| 655 |
"use strict"; |
| 656 |
|
| 657 |
if( typeof CodeMirror === 'undefined' || plugin_groups_canvas === false ){ |
| 658 |
return; |
| 659 |
} |
| 660 |
|
| 661 |
var Pos = CodeMirror.Pos; |
| 662 |
|
| 663 |
function getFields(cm, options) { |
| 664 |
|
| 665 |
var cur = cm.getCursor(), token = cm.getTokenAt(cur), |
| 666 |
result = [], |
| 667 |
fields = options.fields; |
| 668 |
|
| 669 |
if( cm.getMode().name === 'sqlmustache' ){ |
| 670 |
options.mode = 'sqlmustache'; |
| 671 |
} |
| 672 |
switch (options.mode){ |
| 673 |
case 'mustache': |
| 674 |
var wrap = {start: "{{", end: "}}"}, |
| 675 |
prefix = token.string.slice(2); |
| 676 |
break; |
| 677 |
case 'command': |
| 678 |
var wrap = {start: "%", end: "%"}, |
| 679 |
prefix = token.string.slice(1); |
| 680 |
break; |
| 681 |
default: |
| 682 |
var wrap = {start: "", end: "}}"}, |
| 683 |
prefix = token.string; |
| 684 |
break; |
| 685 |
} |
| 686 |
for( var field in fields){ |
| 687 |
if (field.indexOf(prefix) == 0 || prefix === '{' || fields[field].indexOf(prefix) == 0){ |
| 688 |
if(prefix === '{'){ |
| 689 |
wrap.start = '{'; |
| 690 |
} |
| 691 |
result.push({text: wrap.start + field + wrap.end, displayText: fields[field]}); |
| 692 |
} |
| 693 |
}; |
| 694 |
|
| 695 |
return { |
| 696 |
list: result, |
| 697 |
from: Pos(cur.line, token.start), |
| 698 |
to: Pos(cur.line, token.end) |
| 699 |
}; |
| 700 |
} |
| 701 |
CodeMirror.registerHelper("hint", "elementfield", getFields); |
| 702 |
})(); |
| 703 |
|
| 704 |
function find_if_in_wrapper( open_entry, close_entry, cm ){ |
| 705 |
in_entry = false; |
| 706 |
if( open_entry.findPrevious() ){ |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
// is entry. check if closed |
| 711 |
var open_pos = open_entry.from(); |
| 712 |
|
| 713 |
if( close_entry.findPrevious() ){ |
| 714 |
// if closed after open then not in |
| 715 |
var close_pos = close_entry.from(); |
| 716 |
if( open_pos.line > close_pos.line ){ |
| 717 |
// open is after close - on entry |
| 718 |
in_entry = open_pos |
| 719 |
}else if( open_pos.line === close_pos.line ){ |
| 720 |
// smame line - what point? |
| 721 |
if( open_pos.ch > close_pos.ch ){ |
| 722 |
//after close - in entry |
| 723 |
in_entry = open_pos; |
| 724 |
} |
| 725 |
}else{ |
| 726 |
|
| 727 |
open_entry = cm.getSearchCursor('{{#each ', open_pos); |
| 728 |
|
| 729 |
return find_if_in_wrapper( open_entry, close_entry, cm ) |
| 730 |
} |
| 731 |
|
| 732 |
}else{ |
| 733 |
|
| 734 |
in_entry = open_pos; |
| 735 |
} |
| 736 |
|
| 737 |
} |
| 738 |
|
| 739 |
// set the parent |
| 740 |
if( in_entry ){ |
| 741 |
// find what tag is open |
| 742 |
var close_tag = cm.getSearchCursor( '}}', in_entry ); |
| 743 |
if( close_tag.findNext() ){ |
| 744 |
var close_pos = close_tag.from(); |
| 745 |
start_tag = open_entry.to(); |
| 746 |
|
| 747 |
in_entry = cm.getRange( start_tag, close_pos ); |
| 748 |
|
| 749 |
} |
| 750 |
|
| 751 |
} |
| 752 |
|
| 753 |
return in_entry; |
| 754 |
} |
| 755 |
|
| 756 |
function tagFields(cm, e) { |
| 757 |
if( e.keyCode === 8 ){ |
| 758 |
return; // no backspace. |
| 759 |
} |
| 760 |
//console.log( cm ); |
| 761 |
var cur = cm.getCursor(); |
| 762 |
|
| 763 |
// test search |
| 764 |
var open_entry = cm.getSearchCursor('{{#each ', cur); |
| 765 |
var close_entry = cm.getSearchCursor('{{/each}}', cur); |
| 766 |
var open_if = cm.getSearchCursor('{{#if ', cur); |
| 767 |
var close_if = cm.getSearchCursor('{{/if', cur); |
| 768 |
|
| 769 |
var in_entry = find_if_in_wrapper( open_entry, close_entry, cm ); |
| 770 |
var in_if = false; |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
if( open_if.findPrevious() ){ |
| 777 |
// is if. check if closed |
| 778 |
var open_pos = open_if.from(); |
| 779 |
|
| 780 |
if( close_if.findPrevious() ){ |
| 781 |
// if closed after open then not in |
| 782 |
var close_pos = close_if.from(); |
| 783 |
if( open_pos.line > close_pos.line ){ |
| 784 |
// open is after close - on if |
| 785 |
in_if = true |
| 786 |
}else if( open_pos.line === close_pos.line ){ |
| 787 |
// smame line - what point? |
| 788 |
if( open_pos.ch > close_pos.ch ){ |
| 789 |
//after close - in if |
| 790 |
in_if = true; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
}else{ |
| 795 |
in_if = true; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
if( in_if ){ |
| 800 |
// find what tag is open |
| 801 |
var close_tag = cm.getSearchCursor( '}}', open_pos ); |
| 802 |
if( close_tag.findNext() ){ |
| 803 |
var close_pos = close_tag.from(); |
| 804 |
start_tag = open_entry.to(); |
| 805 |
|
| 806 |
in_if = cm.getRange( start_tag, close_pos ); |
| 807 |
|
| 808 |
} |
| 809 |
|
| 810 |
} |
| 811 |
|
| 812 |
if (!cm.state.completionActive || e.keyCode === 18){ |
| 813 |
var token = cm.getTokenAt(cur), prefix, |
| 814 |
prefix = token.string.slice(0); |
| 815 |
if(prefix){ |
| 816 |
if(token.type){ |
| 817 |
var fields = {}; |
| 818 |
//console.log( token ); |
| 819 |
if( token.type ){ |
| 820 |
// only show fields within the entry |
| 821 |
if( in_entry ){ |
| 822 |
|
| 823 |
if( !in_if ){ |
| 824 |
// dont allow closing #each if in if |
| 825 |
fields = { |
| 826 |
"/each" : "/each" |
| 827 |
}; |
| 828 |
} |
| 829 |
|
| 830 |
// ADD INDEX KEY |
| 831 |
fields['@key'] = "@key"; |
| 832 |
|
| 833 |
jQuery('.plugin-groups-autocomplete-in-entry-' + token.type).each(function(){ |
| 834 |
var field = jQuery(this); |
| 835 |
|
| 836 |
if( !field.hasClass('parent-' + in_entry) && !field.hasClass('parant-all') ){ |
| 837 |
return; |
| 838 |
} |
| 839 |
|
| 840 |
fields[field.data('slug')] = field.data('label'); |
| 841 |
//fields["#each " + field.data('slug')] = "#each " + field.data('label'); |
| 842 |
//if( !in_if ){ |
| 843 |
if( field.data('label').indexOf('#') < 0 ){ |
| 844 |
fields["#if " + field.data('slug')] = "#if " + field.data('label'); |
| 845 |
} |
| 846 |
//} |
| 847 |
//fields["#unless " + field.data('slug')] = "#unless " + field.data('label'); |
| 848 |
}); |
| 849 |
}else{ |
| 850 |
|
| 851 |
jQuery('.plugin-groups-autocomplete-out-entry-' + token.type).each(function(){ |
| 852 |
var field = jQuery(this); |
| 853 |
fields[field.data('slug')] = field.data('label'); |
| 854 |
//fields["#each " + field.data('slug')] = "#each " + field.data('label'); |
| 855 |
//if( !in_if ){ |
| 856 |
if( field.data('label').indexOf('#') < 0 ){ |
| 857 |
fields["#if " + field.data('slug')] = "#if " + field.data('label'); |
| 858 |
} |
| 859 |
//} |
| 860 |
//fields["#unless " + field.data('slug')] = "#unless " + field.data('label'); |
| 861 |
}); |
| 862 |
|
| 863 |
} |
| 864 |
|
| 865 |
if( in_if ){ |
| 866 |
fields['else'] = 'else'; |
| 867 |
fields['/if'] = '/if'; |
| 868 |
} |
| 869 |
} |
| 870 |
// sort hack |
| 871 |
var keys = []; |
| 872 |
var commands = []; |
| 873 |
var sorted_obj = {}; |
| 874 |
|
| 875 |
for(var key in fields){ |
| 876 |
if(fields.hasOwnProperty(key)){ |
| 877 |
if( key.indexOf('#') < 0 && key.indexOf('/') < 0 ){ |
| 878 |
keys.push(key); |
| 879 |
}else{ |
| 880 |
commands.push(key); |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
// sort keys |
| 886 |
keys.sort(); |
| 887 |
commands.sort(); |
| 888 |
keys = keys.concat(commands); |
| 889 |
// create new array based on Sorted Keys |
| 890 |
jQuery.each(keys, function(i, key){ |
| 891 |
sorted_obj[key] = fields[key]; |
| 892 |
}); |
| 893 |
CodeMirror.showHint(cm, CodeMirror.hint.elementfield, {fields: sorted_obj, mode: token.type}); |
| 894 |
|
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
return; |
| 899 |
} |