Sync plugins from current page

Signed-off-by: Adrian Nöthlich <git@promasu.tech>
This commit is contained in:
2019-09-11 19:08:46 +02:00
parent 85d41e4216
commit 8515ff9587
1847 changed files with 505469 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
// Options JavaScript
jQuery( document ).ready( function( $ ) {
if ( 'plugin-editor' == pagenow )
$.ajax({
type: "POST",
url: ajaxurl,
data: {
_ajax_nonce: sns_plugin_editor_options.nonce,
action: sns_plugin_editor_options.action,
file: $('input[name="file"]').val(),
plugin: $('input[name="plugin"]').val()
},
success: function( data ) {
$('#templateside > ul').html( data.ul );
if ( ! data.need_update ) return;
var warning = "<p><strong>Warning:</strong> Making changes to active plugins is not recommended. If your changes cause a fatal error, the plugin will be automatically deactivated.</p>";
if ( data.active ) {
$('p.submit').before(warning);
$('.fileedit-sub .alignleft big').html( 'Editing <strong>' + $('.fileedit-sub .alignleft big strong').html() + '</strong> (active)' );
}
$('#plugin').val( data.plugin );
console.dir( data );
}
});
});
jQuery( document ).ready( function( $ ) {
var theme = codemirror_options.theme ? codemirror_options.theme: 'default',
file = $( 'input[name="file"]' ).val(),
$new = $( '#newcontent' ),
$template = $( '#template' ),
$wpbody = $( '#wpbody-content' ),
$documentation = $( '#documentation' ),
$submit = $( 'p.submit' ).first(),
$warning = $( '#documentation + p:not(.submit)' ).first(),
$templateside = $( '#templateside' ),
templateOffset, bottomPadding, docHeight, submitHeight, resizeTimer, fileType, cmheight;
fileType = file.slice( file.lastIndexOf(".")+1 );
templateOffset = parseInt( jQuery('#template').offset().top ),
bottomPadding = parseInt( $('#wpbody-content').css('padding-bottom') );
docHeight = ( $documentation.length ) ? parseInt( $documentation.height() )
+ parseInt( $documentation.css('padding-top') )
+ parseInt( $documentation.css('padding-bottom') )
+ parseInt( $documentation.css('margin-top') )
+ parseInt( $documentation.css('margin-bottom') )
: 0;
warningHeight = ( $warning.length ) ? parseInt( $warning.height() )
+ parseInt( $warning.css('padding-top') )
+ parseInt( $warning.css('padding-bottom') )
+ parseInt( $warning.css('margin-top') )
+ parseInt( $warning.css('margin-bottom') )
: 0;
submitHeight = parseInt( $submit.height() )
+ parseInt( $submit.css('padding-top') )
+ parseInt( $submit.css('padding-bottom') )
+ parseInt( $submit.css('margin-top') )
+ parseInt( $submit.css('margin-bottom') );
templateside = parseInt( $templateside.height() );
var config = {
lineNumbers: true,
theme: theme,
//viewportMargin: Infinity
};
switch ( fileType ) {
case "md":
config.mode = "gfm";
break;
case "js":
config.mode = "javascript";
break;
case "css":
config.mode = "css";
break;
case "less":
config.mode = "less";
break;
case "coffee":
config.mode = "coffeescript";
break;
case "html":
case "htm":
config.mode = "html";
break;
case "php":
config.mode = "php";
break;
default:
config.mode = "markdown";
break;
}
CodeMirror.commands.save = function (){ jQuery('#submit').click(); };
var cmeditor = CodeMirror.fromTextArea( $new.get(0), config );
$(window).resize(function(){
clearTimeout(resizeTimer);
resizeTimer = setTimeout( cmresizer, 100 );
});
function cmresizer() {
cmheight = Math.max( 300, $(window).height() - ( templateOffset + bottomPadding + docHeight + warningHeight + submitHeight + 40 ) );
if ( cmheight > templateside )
cmeditor.setSize( null, cmheight );
else
cmeditor.setSize( null, $(window).height() - ( templateOffset + docHeight + warningHeight + submitHeight ) );
}
cmresizer();
});

View File

@@ -0,0 +1,134 @@
// Options JavaScript
jQuery( document ).ready( function( $ ) {
var compiled, source;
var theme = _SnS_options.theme ? _SnS_options.theme: 'default';
var lessMirror, lessOutput, errorLine, errorText, errors, loaded,
coffeeMirror, coffeeOutput, coffee_errorLine, coffee_errorText, coffee_errors, coffee_loaded,
lessMirrorConfig = { gutters: ["note-gutter", "CodeMirror-linenumbers"],
lineNumbers: true, mode: "text/x-less", theme: theme, indentWithTabs: true },
coffeeMirrorConfig = { lineNumbers: true, mode: "text/x-coffeescript", theme: theme };
var parser = new( less.Parser )({});
$("#enqueue_scripts").data( 'placeholder', 'Enqueue Registered Scripts...' ).width(350).chosen();
$(".chosen-container-multi .chosen-choices .search-field input").height('26px');
$(".chosen-container .chosen-results").css( 'max-height', '176px');
//CodeMirror.commands.save = saveLessMirror;
$( "textarea.js" ).not( '#coffee_compiled' ).each( function() {
CodeMirror.fromTextArea( this, { lineNumbers: true, mode: "javascript", theme: theme } );
});
$( "textarea.css" ).not( '#compiled' ).each( function() {
CodeMirror.fromTextArea( this, { lineNumbers: true, mode: "css", theme: theme } );
});
$( "textarea.html" ).each( function() {
CodeMirror.fromTextArea( this, { lineNumbers: true, mode: "text/html", theme: theme } );
});
lessOutput = CodeMirror.fromTextArea( $( '#compiled' ).get(0), { lineNumbers: true, mode: "css", theme: theme, readOnly: true } );
coffeeOutput = CodeMirror.fromTextArea( $( '#coffee_compiled' ).get(0), { lineNumbers: true, mode: "javascript", theme: theme, readOnly: true } );
$( "textarea.less" ).each( function() {
lessMirror = CodeMirror.fromTextArea( this, lessMirrorConfig );
lessMirror.on( "change", compile );
});
$( "textarea.coffee" ).each( function() {
coffeeMirror = CodeMirror.fromTextArea( this, coffeeMirrorConfig );
coffeeMirror.on( "change", coffee_compile );
});
$('#coffee').parent().append('<label><input type="checkbox" id="coffee_spacing"> Double Spaced</label>');
$('#coffee_spacing').change( coffee_compile );
compile();
coffee_compile();
loaded = true;
coffee_loaded = true;
$( "#less" ).closest('form').submit( compile );
$( "#coffee" ).closest('form').submit( coffee_compile );
//function saveLessMirror(){
// Ajax Save.
//}
function compile() {
lessMirror.save();
parser.parse( lessMirror.getValue(), function ( err, tree ) {
if ( err ){
doError( err );
} else {
try {
$( '#compiled_error' ).hide();
lessOutput.setValue( tree.toCSS() );
lessOutput.save();
$( '#compiled' ).next( '.CodeMirror' ).show();
lessOutput.refresh();
clearCompileError();
}
catch ( err ) {
doError( err );
}
}
});
}
function coffee_compile() {
coffeeMirror.save();
try {
$( '#coffee_compiled_error' ).hide();
source = $('#coffee').val();
if ( '' == source || ' ' == source ) {
coffeeOutput.setValue( '' );
} else {
compiled = CoffeeScript.compile( source );
trimmed = $('#coffee_spacing').is(':checked') ? compiled : compiled.replace(/(\n\n)/gm,"\n");
coffeeOutput.setValue( trimmed );
}
coffeeOutput.save();
$( '#coffee_compiled' ).next( '.CodeMirror' ).show();
}
catch ( err ) {
console.dir( err );
$( '#coffee_compiled' ).next( '.CodeMirror' ).hide();
if ( coffee_loaded ) {
$( '#coffee_compiled_error' ).removeClass( 'error' ).addClass( 'updated' );
$( '#coffee_compiled_error' ).show().html( "<p><strong>Warning: &nbsp; </strong>" + err.message + "</p>" );
} else {
$( '#coffee_compiled_error' ).show().html( "<p><strong>Error: &nbsp; </strong>" + err.message + "</p>" );
}
}
}
function doError( err ) {
//console.dir( err );
$( '#compiled' ).next( '.CodeMirror' ).hide();
if ( loaded ) {
$( '#compiled_error' ).removeClass( 'error' ).addClass( 'updated' );
$( '#compiled_error' ).show().html( "<p><strong>Warning: &nbsp; </strong>" + err.message + "</p>" );
} else {
$( '#compiled_error' ).show().html( "<p><strong>Error: &nbsp; </strong>" + err.message + "</p>" );
}
clearCompileError();
errorLine = lessMirror.setGutterMarker( err.line - 1, 'note-gutter', document.createTextNode("*") );
//lessMirror.setLineClass( errorLine, "cm-error");
var pos = lessMirror.posFromIndex( err.index + 1 );
var token = lessMirror.getTokenAt( pos );
var start = lessMirror.posFromIndex( err.index );
var end = lessMirror.posFromIndex( err.index + token.string.length )
errorText = lessMirror.markText( start, end, { className: "cm-error" } );
lessOutput.setValue( "" );
lessOutput.save();
}
function clearCompileError() {
if ( errorLine ) {
lessMirror.clearGutter( 'note-gutter' );
//lessMirror.setLineClass( errorLine, null );
errorLine = false;
}
if ( errorText ) errorText.clear();
errorText = false;
}
});

View File

@@ -0,0 +1,105 @@
// Options JavaScript
jQuery( document ).ready( function( $ ) { "use strict"
var collection = []
, context = "#sns-shortcodes"
, theme = _SnS_options.theme ? _SnS_options.theme: 'default'
, $form
, config;
config = {
mode: "text/html",
theme: theme,
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true,
enterMode: "keep",
matchBrackets: true
};
CodeMirror.commands.save = function() {
$form.submit();
};
// Each "IDE"
$( ".sns-less-ide", context ).each( function() {
var $text = $('.code',this);
var ide = {
data : $text.val(),
name : $text.data('sns-shortcode-key'),
$text : $text,
cm : CodeMirror.fromTextArea( $text.get(0), config )
};
if ( $text.parent().hasClass( 'sns-collapsed' ) )
ide.cm.toTextArea();
collection.push( ide );
});
// Collapsable
$( context ).on( "click", '.sns-collapsed-btn, .sns-collapsed-btn + label', function( event ){
var $this = $( this )
, collapsed
, fileName
, thisIDE;
$this.parent().toggleClass( 'sns-collapsed' );
fileName = $this.siblings( '.code' ).data( 'sns-shortcode-key' );
collapsed = $this.parent().hasClass( 'sns-collapsed' );
$(collection).each(function(index, element) {
if ( element.name == fileName )
thisIDE = element;
});
if ( collapsed ) {
thisIDE.cm.toTextArea();
} else {
thisIDE.cm = CodeMirror.fromTextArea( thisIDE.$text.get(0), config );
}
});
$( '.sns-ajax-loading' ).hide();
/*
$form = $( context ).closest( 'form' );
$form.submit( function( event ){
event.preventDefault();
$.ajax({
type: "POST",
url: window.location,
data: $(this).serialize()+'&ajaxsubmit=1',
cache: false,
success: saved
});
});
// Save
$( context ).on( "click", ".sns-ajax-save", function( event ){
event.preventDefault();
$( this ).nextAll( '.sns-ajax-loading' ).show();
$form.submit();
});*/
/*
function saved( data ) {
$(data).insertAfter( '#icon-sns + h2' ).delay(3000).fadeOut();
$( '.sns-ajax-loading' ).hide();
}
* Expects return data.
$('#sns-ajax-add-shortcode').click(function( event ){
event.preventDefault();
$(this).next().show();
$(collection).each(function (){ this.save(); });
var args = { _ajax_nonce: nonce };
args.action = 'sns_hoops';
args.subaction = 'add';
args.name = $( '#SnS_shortcodes' ).val();
args.shortcode = $( '#SnS_shortcodes_new' ).val();
$.post( ajaxurl, args, function( data ) { refreshShortcodes( data ); } );
});
$('#SnS_shortcodes').keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
$("#sns-ajax-add-shortcode").click();
}
});
*/
});

View File

@@ -0,0 +1,666 @@
jQuery( document ).ready( function( $ ) {
var context = '#SnS_meta_box',
currentCodeMirror = [], keys = [],
gutenMCE = false,
nonce = $( '#scripts_n_styles_noncename' ).val(),
theme = codemirror_options.theme ? codemirror_options.theme: 'default';
if ( window.wpEditorL10n && wpEditorL10n.tinymce && wpEditorL10n.tinymce.settings ) {
gutenMCE = wpEditorL10n.tinymce.settings;
}
// For CPTs that don't have an editor, prevent "tinyMCEPreInit is 'undefined'"
var initDatas = ( typeof tinyMCEPreInit !== 'undefined' && tinyMCEPreInit.mceInit ) ? tinyMCEPreInit.mceInit: false;
for ( var prop in initDatas ) {
keys.push( prop );
}
var mceBodyClass = getMCEBodyClasses();
$("#SnS_enqueue_scripts").data( 'placeholder', 'Enqueue Registered Scripts...' ).chosen({ width: "356px" });
$(".chosen-container-multi .chosen-choices .search-field input").height('26px');
$(".chosen-container .chosen-results").css( 'max-height', '176px');
//$('textarea', context).attr('autocomplete','off');
// Refresh when panel becomes unhidden
$( '#adv-settings' ).on( 'click', context + '-hide', refreshCodeMirrors );
$( context ).on( 'click', '.hndle, .handlediv', refreshCodeMirrors );
// add tab-switch handler
$( context ).on( 'click', '.wp-tab-bar a', onTabSwitch );
// activate first run
$( '.wp-tab-active a', context ).click();
// must run before ajax click handlers are added.
setupAjaxUI();
refreshDeleteBtns();
if ( gutenMCE && wp.data && wp.data.select ) {
var editPost = wp.data.select( 'core/edit-post' );
wp.data.subscribe( function() {
if ( editPost.isSavingMetaBoxes() ) {
$( currentCodeMirror ).each( function() {
this.save();
});
} else {
$( currentCodeMirror ).each( function() {
this.refresh();
});
}
});
}
$('#sns-ajax-update-scripts').click(function( event ){
event.preventDefault();
$(this).next().show();
$(currentCodeMirror).each(function (){ this.save(); });
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_scripts';
args.scripts = $( '#SnS_scripts' ).val();
args.scripts_in_head = $( '#SnS_scripts_in_head' ).val();
$.post( ajaxurl, args, function() { refreshMCE(); } );
});
$('#sns-ajax-update-html').click(function( event ){
event.preventDefault();
$(this).next().show();
$(currentCodeMirror).each(function (){ this.save(); });
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_html';
args.html_in_footer = $( '#SnS_html_in_footer' ).val();
args.html_in_head = $( '#SnS_html_in_head' ).val();
$.post( ajaxurl, args, function(res) { console.log('post sent'); refreshMCE(); } );
});
$('#sns-ajax-update-styles').click(function( event ){
event.preventDefault();
$(this).next().show();
$(currentCodeMirror).each(function (){ this.save(); });
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_styles';
args.styles = $( '#SnS_styles' ).val();
$.post( ajaxurl, args, function() { refreshMCE(); } );
});
/*
* Expects return data.
*/
$('#sns-ajax-update-classes').click(function( event ){
event.preventDefault();
$(this).next().show();
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_classes';
args.classes_body = $( '#SnS_classes_body' ).val();
args.classes_post = $( '#SnS_classes_post' ).val();
$.post( ajaxurl, args, function( data ) { refreshBodyClass( data ); } );
});
$('#SnS_classes_body, #SnS_classes_body').keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
$("#sns-ajax-update-classes").click();
}
});
/*
* Expects return data.
*/
$('#sns-ajax-update-dropdown').click(function( event ){
event.preventDefault();
$(this).next().show();
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_dropdown';
var format = {};
format.title = $( '#SnS_classes_mce_title' ).val();
format.classes = $( '#SnS_classes_mce_classes' ).val();
switch ( $( '#SnS_classes_mce_type' ).val() ) {
case 'inline':
format.inline = $( '#SnS_classes_mce_element' ).val();
break;
case 'block':
format.block = $( '#SnS_classes_mce_element' ).val();
if ( $( '#SnS_classes_mce_wrapper' ).prop('checked') )
format.wrapper = true;
break;
case 'selector':
format.selector = $( '#SnS_classes_mce_element' ).val();
break;
default:
return;
}
args.format = format;
$.post( ajaxurl, args, function( data ) { refreshStyleFormats( data ); } );
});
$('#SnS_classes_mce_classes, #SnS_classes_mce_element, #SnS_classes_mce_title').keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
$("#sns-ajax-update-dropdown").click();
}
});
/*
* Expects return data.
*/
$('#delete-mce-dropdown-names').on( "click", ".sns-ajax-delete", function( event ){
event.preventDefault();
$(this).next().show();
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_delete_class';
args.delete = $( this ).attr( 'id' );
$.post( ajaxurl, args, function( data ) { refreshStyleFormats( data ); } );
});
/*
* Expects return data.
*/
$('#sns-ajax-add-shortcode').click(function( event ){
event.preventDefault();
$(this).next().show();
$(currentCodeMirror).each(function (){ this.save(); });
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_shortcodes';
args.subaction = 'add';
args.name = $( '#SnS_shortcodes' ).val();
args.shortcode = $( '#SnS_shortcodes_new' ).val();
$.post( ajaxurl, args, function( data ) { refreshShortcodes( data ); } );
});
$('#SnS_shortcodes').keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
$("#sns-ajax-add-shortcode").click();
}
});
$('#sns-shortcodes').on( "click", ".sns-ajax-delete-shortcode", function( event ){
event.preventDefault();
if($(this).data('lock'))return;else $(this).data('lock',true);
$(this).next().show();
$(currentCodeMirror).each(function (){ this.save(); });
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_shortcodes';
args.subaction = 'delete';
args.name = $( this ).parent().siblings('textarea').attr( 'data-sns-shortcode-key' );
$.post( ajaxurl, args, function( data ) { refreshShortcodes( data ); } );
});
$('#sns-shortcodes').on( "click", ".sns-ajax-update-shortcode", function( event ){
event.preventDefault();
$(this).next().show();
$(currentCodeMirror).each(function (){ this.save(); });
var args = { _ajax_nonce: nonce, post_id: $( '#post_ID' ).val(), };
args.action = 'sns_shortcodes';
args.subaction = 'update';
args.name = $( this ).parent().siblings('textarea').attr( 'data-sns-shortcode-key' );
args.shortcode = $( this ).parent().siblings('textarea').val();
$.post( ajaxurl, args, function( data ) { refreshShortcodes( data ); } );
});
/*
* Returns the body_class of TinyMCE minus the Scripts n Styles values.
*/
function getMCEBodyClasses() {
var t = [],
a = [],
b = [],
c = [];
if ( gutenMCE.body_class ) {
b = gutenMCE.body_class.trim().split( ' ' );
}
$( keys ).each( function( index, element ) {
var data = initDatas[element];
if ( data.body_class ) {
t = data.body_class.split( ' ' );
}
var bc = $( '#SnS_classes_body' ).val().split( ' ' ),
pc = $( '#SnS_classes_post' ).val().split( ' ' ),
p;
for ( var i = 0; i < t.length; i++ ) {
p = $.inArray( bc[i], t );
if ( -1 != p ) {
t.splice( p, 1 );
}
}
for ( var i = 0; i < t.length; i++ ) {
p = $.inArray( pc[i], t );
if ( -1 != p ) {
t.splice( p, 1 );
}
}
t = t.join( ' ' );
a[element] = t;
});
c = a.concat( b );
return c;
}
/*
* Builds and Adds the DOM for AJAX functionality.
*/
function setupAjaxUI() {
// set up ajax ui. (need to come up with a better ID naming scheme.)
$('#SnS_scripts-tab').append(
'<div class="sns-ajax-wrap">'
+ '<a id="sns-ajax-update-scripts" href="#" class="button">Update Scripts</a>'
+ ' '
+ '<span class="sns-ajax-loading"><span class="spinner" style="display: inline-block;"></span></span>'
+ '</div>'
);
$('#SnS_html-tab').append(
'<div class="sns-ajax-wrap">'
+ '<a id="sns-ajax-update-html" href="#" class="button">Update HTML</a>'
+ ' '
+ '<span class="sns-ajax-loading"><span class="spinner" style="display: inline-block;"></span></span>'
+ '</div>'
);
$('#SnS_styles-tab').append(
'<div class="sns-ajax-wrap">'
+ '<a id="sns-ajax-update-styles" href="#" class="button">Update Styles</a>'
+ ' '
+ '<span class="sns-ajax-loading"><span class="spinner" style="display: inline-block;"></span></span>'
+ '</div>'
);
$('#sns-classes').append(
'<div class="sns-ajax-wrap">'
+ '<a id="sns-ajax-update-classes" href="#" class="button">Update Classes</a>'
+ ' '
+ '<span class="sns-ajax-loading"><span class="spinner" style="display: inline-block;"></span></span>'
+ '</div>'
);
$('#add-mce-dropdown-names').append(
'<div class="sns-ajax-wrap">'
+ '<a id="sns-ajax-update-dropdown" href="#" class="button">Add Class</a>'
+ ' '
+ '<span class="sns-ajax-loading"><span class="spinner" style="display: inline-block;"></span></span>'
+ '</div>'
);
$('#SnS_shortcodes').after(
' &nbsp; '
+ '<a id="sns-ajax-add-shortcode" href="#" class="button">Add New</a>'
+ ' '
+ '<span class="sns-ajax-loading"><span class="spinner" style="display: inline-block;"></span></span>'
);
$('#sns-shortcodes .sns-shortcode .inside').append(
'<div class="sns-ajax-wrap">'
+ '<a class="sns-ajax-delete-shortcode button" href="#">Delete</a>'
+ ' &nbsp; '
+ '<a class="sns-ajax-update-shortcode button" href="#">Update</a>'
+ ' '
+ '<span class="sns-ajax-loading"><span class="spinner" style="display: inline-block;"></span></span>'
+ '</div>'
);
$( '.sns-ajax-loading' ).hide();
if ( $( '#SnS_classes_mce_type').val() == 'block' ) {
$('#add-mce-dropdown-names .sns-mce-wrapper').show();
} else {
$('#add-mce-dropdown-names .sns-mce-wrapper').hide();
}
$( '#SnS_classes_mce_type' ).change(function() {
if ( $(this).val() == 'block' ) {
$('#add-mce-dropdown-names .sns-mce-wrapper').show();
} else {
$('#add-mce-dropdown-names .sns-mce-wrapper').hide();
}
});
$( '.wp-tab-bar li', context ).show();
}
/*
* Main Tab Switch Handler.
*/
function onTabSwitch( event ) {
event.preventDefault();
clearCodeMirrors();
/*
* There is a weird bug where if clearCodeMirrors() is called right before
* loadCodeMirrors(), loading the page with the Styles tab active, and
* then switching to the Script tab, you can lose data from the second
* CodeMirror if leaving and returning to that tab. I've no idea what's
* going on there. Leaving code inbetween them is a fraggle, but working,
* workaround. Maybe has to do with execution time? No idea.
*/
// switch active classes
$( '.wp-tab-active', context ).removeClass( 'wp-tab-active' );
$( this ).parent( 'li' ).addClass( 'wp-tab-active' );
$( '.wp-tabs-panel-active', context ).hide().removeClass( 'wp-tabs-panel-active' );
$( $( this ).attr( 'href' ) ).show().addClass( 'wp-tabs-panel-active' );
loadCodeMirrors();
$.post( ajaxurl, {
action: 'sns_update_tab',
_ajax_nonce: nonce,
active_tab: $( '.wp-tab-bar li', context ).index( $( this ).parent( 'li' ).get(0) )
}
);
}
/*
* CodeMirror Utilities.
*/
function clearCodeMirrors() {
$(currentCodeMirror).each(function (){
this.toTextArea();
});
currentCodeMirror = [];
}
function refreshCodeMirrors() {
$(currentCodeMirror).each( function(){
this.refresh();
});
}
function loadCodeMirrors() {
// collect codemirrors
var settings;
// loop codemirrors
$( '.wp-tabs-panel-active textarea.codemirror', context ).each(function (){
if ( $(this).hasClass( 'js' ) )
settings = {
mode: "text/javascript",
theme: theme,
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true
};
else if ( $(this).hasClass( 'html' ) )
settings = {
mode: "text/html",
theme: theme,
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true
};
else if ( $(this).hasClass( 'css' ) )
settings = {
mode: "text/css",
theme: theme,
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true
};
else if ( $(this).hasClass( 'less' ) )
settings = {
mode: "text/x-less",
theme: theme,
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true,
};
else if ( $(this).hasClass( 'htmlmixed' ) )
settings = {
mode: "text/html",
theme: theme,
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true,
enterMode: "keep",
matchBrackets: true
};
/*else if ( $(this).hasClass( 'php' ) )
settings = {
mode: "application/x-httpd-php",
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true,
enterMode: "keep",
matchBrackets: true
};*/
else
return;
// initialize and store active codemirrors
currentCodeMirror.push( CodeMirror.fromTextArea( this, settings ) );
});
}
/*
* Refresh after AJAX.
*/
function refreshDeleteBtns() {
// responsible for clearing out Delete Buttons, and Adding new ones.
// initData should always contain the latest settings.
var formats = [];
$(keys).each(function(index, key) {
var initData = initDatas[key]
if ( initData.style_formats && initData.style_formats.length ) {
formats = initData.style_formats;
}
});
if ( gutenMCE.style_formats && gutenMCE.style_formats.length ) {
formats = gutenMCE.style_formats;
}
if ( ! formats.length ) {
$( '#delete-mce-dropdown-names', context ).hide();
return;
}
$( '#delete-mce-dropdown-names .sns-ajax-delete-p' ).remove();
$( '#delete-mce-dropdown-names', context ).show();
for ( var i = 0; i < formats.length; i++ ) {
var deleteBtn = {};
if ( formats[i].inline ) {
deleteBtn.element = formats[i].inline;
deleteBtn.wrapper = '';
} else if ( formats[i].block ) {
deleteBtn.element = formats[i].block;
if ( formats[i].wrapper )
deleteBtn.wrapper = ' (wrapper)';
else
deleteBtn.wrapper = '';
} else if ( formats[i].selector ) {
deleteBtn.element = formats[i].selector;
deleteBtn.wrapper = '';
} else {
console.log( 'ERROR!' );
}
deleteBtn.title = formats[i].title;
deleteBtn.classes = formats[i].classes;
$( '#instructions-mce-dropdown-names', context ).after(
'<p class="sns-ajax-delete-p"><a title="delete" class="sns-ajax-delete" id="'
+ deleteBtn.title + '">X</a> "'
+ deleteBtn.title + '" <code>&lt;'
+ deleteBtn.element + ' class="'
+ deleteBtn.classes + '"&gt;</code>'
+ deleteBtn.wrapper + '</p>'
);
}
}
function refreshBodyClass( data ) {
$(keys).each(function(index, key) {
initDatas[key].body_class = mceBodyClass[key] + ' ' + data.classes_body + ' ' + data.classes_post;
});
refreshMCE();
}
function refreshStyleFormats( data ) {
var initData = false;
$(keys).each(function(index, key) {
initData = initDatas[key];
});
if ( ! initData && gutenMCE ) {
initData = gutenMCE;
}
// error check
//console.log(data.classes_mce);
if ( typeof data.classes_mce === 'undefined' ) {
console.log( data );
/*$( '.sns-ajax-loading' ).hide();
return;*/ // Don't block
} else if ( data.classes_mce.length && data.classes_mce != 'Empty' ) {
var style_formats = [];
for ( var i = 0; i < data.classes_mce.length; i++ ) { // loop returned classes_mce
var format = {};
format.title = data.classes_mce[i].title;
if ( data.classes_mce[i].inline )
format.inline = data.classes_mce[i].inline;
else if ( data.classes_mce[i].block ) {
format.block = data.classes_mce[i].block;
if (data.classes_mce[i].wrapper)
format.wrapper = true;
} else if ( data.classes_mce[i].selector )
format.selector = data.classes_mce[i].selector;
else
console.log('dropdown format has bad type.');
format.classes = data.classes_mce[i].classes;
style_formats.push( format );
}
if ( initData ) {
initData.style_formats = style_formats;
if ( initData.toolbar2.indexOf( "styleselect" ) == -1 ) {
var tempString = "styleselect,";
initData.toolbar2 = tempString.concat(initData.toolbar2);
}
}
$( '#delete-mce-dropdown-names', context ).show();
} else {
if ( initData ) {
delete initData.style_formats;
initData.toolbar2 = initData.toolbar2.replace("styleselect,", "");
}
$( '#delete-mce-dropdown-names', context ).hide();
}
refreshDeleteBtns();
refreshMCE();
}
if ( 0 == $( '.sns-shortcode', '#sns-shortcodes' ).length )
$( 'h4', '#sns-shortcodes' ).hide();
function refreshShortcodes( data ) {
if ( data.code ) {
switch ( data.code ) {
case 2:
console.log( data.message );
break;
case 3:
$( 'textarea[data-sns-shortcode-key=' + data.message + ']', '#sns-shortcodes' ).closest('.sns-shortcode').slideUp(function(){
$(this).remove();
if ( 0 == $( '.sns-shortcode', '#sns-shortcodes' ).length )
$( 'h4', '#sns-shortcodes' ).slideUp();
});
break;
}
} else {
if ( 0 == data.indexOf( "<" ) ) {
$('#sns-shortcodes-wrap').prepend( data ).find( '.widget' ).hide().slideDown();
$( '.codemirror-new' ).parent().prepend( '<span class="sns-collapsed-shortcode-btn"></span>' );
var codemirrorNew = $( '.codemirror-new' ).removeClass('codemirror-new').addClass('codemirror').get(0);
currentCodeMirror.push( CodeMirror.fromTextArea( codemirrorNew, {
mode: "text/html",
theme: theme,
lineNumbers: true,
tabMode: "shift",
indentUnit: 4,
indentWithTabs: true,
enterMode: "keep",
matchBrackets: true
} ) );
if ( 0 == $( 'h4', '#sns-shortcodes' ).length )
$( '#sns-shortcodes' ).prepend('<h4>Existing Codes: </h4>');
if ( ! $( 'h4', '#sns-shortcodes' ).is( ":visible" ) )
$( 'h4', '#sns-shortcodes' ).slideDown();
clearCodeMirrors();
$('#SnS_shortcodes').val('');
$('#SnS_shortcodes_new').val('');
loadCodeMirrors();
} else if ( 0 == data.indexOf( "empty value." ) ) {
console.log('empty value');
} else if ( 0 == data.indexOf( "Use delete instead." ) ) {
console.log('Use delete instead');
} else {
console.log( 'Scripts n Styles: ' + '\n\n' + 'Sorry, there was an AJAX error: (' + data + ')' + '\n\n' + 'Please use the post update button instead.' );
}
}
$( '.sns-ajax-loading' ).hide();
}
addShortcodeBtns();
function addShortcodeBtns() {
$( '.sns-shortcode > .inside > p' ).before('<span class="sns-collapsed-shortcode-btn"></span>');
$('#sns-shortcodes-wrap').on("click",'.sns-collapsed-shortcode-btn', function(event){
$(this).parent().toggleClass('sns-collapsed-shortcode');
});
$('.sns-collapsed-shortcode-btn').click();
}
function refreshMCE() {
$( tinyMCE.editors ).each( function( index, ed ){
// If Visual has been activated.
if ( ed ) {
if ( ed.isHidden() ) {
refreshMCEhelper( ed );
} else {
$('#'+ed.id+'-html').click(); // 3.3
refreshMCEhelper( ed );
$('#'+ed.id+'-tmce').click(); // 3.3
}
}
});
$( '.sns-ajax-loading' ).hide();
}
function refreshMCEhelper( ed ) {
if ( gutenMCE ) {
return;
}
ed.save();
ed.destroy();
ed.remove();
if ( initDatas[ed.id] && initDatas[ed.id].wpautop )
$('#'+ed.id).val( switchEditors.wpautop( $('#'+ed.id).val() ) );
ed = new tinymce.Editor( ed.id, initDatas[ed.id], tinymce.EditorManager );
ed.render();
ed.hide();
}
});

View File

@@ -0,0 +1,18 @@
// Options JavaScript
jQuery( document ).ready( function( $ ) {
var theme = codemirror_options.theme ? codemirror_options.theme: 'default';
var editor = CodeMirror.fromTextArea(document.getElementById("codemirror_demo"), {
lineNumbers: true,
matchBrackets: true,
mode: "application/x-httpd-php",
indentUnit: 4,
indentWithTabs: true,
enterMode: "keep",
tabMode: "shift",
theme: theme
});
$('input[name="SnS_options[cm_theme]"]').change( function(){
editor.setOption("theme", $(this).val());
});
});

View File

@@ -0,0 +1,276 @@
// Options JavaScript
jQuery( document ).ready( function( $ ) { "use strict"
var collection = []
, context = "#less_area"
, theme = _SnS_options.theme ? _SnS_options.theme: 'default'
, timeout = _SnS_options.timeout || 1000
, loaded = false
, preview = false
, compiled
, $error, $status, $form, $css
, onChange
, errorMarker, errorText, errorMirror
, config;
// Prevent keystoke compile buildup
onChange = function onChange( cm ){
$status.show();
cm.save();
if ( timeout ) {
clearTimeout( _SnS_options.theme_compiler_timer );
_SnS_options.theme_compiler_timer = setTimeout( _SnS_options.theme_compiler, timeout );
} else {
compile();
}
}
config = {
gutters: ["note-gutter", "CodeMirror-linenumbers"],
lineNumbers: true,
mode: "text/x-less",
theme: theme,
indentWithTabs: true,
tabSize: 4,
indentUnit: 4
};
CodeMirror.commands.save = function() {
$form.submit();
};
// Each "IDE"
$( ".sns-less-ide", context ).each( function() {
var $text = $('.code',this);
var ide = {
name : $text.data('file-name'),
raw : $text.data('raw'),
data : $text.val(),
$text : $text,
lines : 0,
startLine : 0,
endLine : 0,
startChars : 0,
endChars : 0,
errorLine : null,
errorText : null,
cm : CodeMirror.fromTextArea( $text.get(0), config )
};
ide.cm.on( "change", onChange );
if ( $text.parent().hasClass( 'sns-collapsed' ) )
ide.cm.toTextArea();
collection.push( ide );
});
// Collapsable
$( context ).on( "click", '.sns-collapsed-btn, .sns-collapsed-btn + label', function( event ){
var $this = $( this )
, collapsed
, fileName
, thisIDE;
$this.parent().toggleClass( 'sns-collapsed' );
fileName = $this.siblings( '.code' ).data( 'file-name' );
collapsed = $this.parent().hasClass( 'sns-collapsed' );
$(collection).each(function(index, element) {
if ( element.name == fileName )
thisIDE = element;
});
if ( collapsed ) {
thisIDE.cm.toTextArea();
} else {
thisIDE.cm = CodeMirror.fromTextArea( thisIDE.$text.get(0), config );
thisIDE.cm.on( "change", onChange );
}
$.post( ajaxurl,
{ action: 'sns_open_theme_panels'
, _ajax_nonce: $( '#_wpnonce' ).val()
, 'file-name': fileName
, 'collapsed': collapsed ? 'yes' : 'no'
}
);
});
$( '#css_area' ).on( "click", '.sns-collapsed-btn, .sns-collapsed-btn + label', function( event ){
var $this = $( this ).parent();
$this.toggleClass( 'sns-collapsed' );
preview = ! $this.hasClass( 'sns-collapsed' );
if ( preview )
compiled = createCSSEditor();
else
compiled.toTextArea();
});
$( '.single-status' ).hide();
$( '.sns-ajax-loading' ).hide();
// Load
$( context ).on( "click", ".sns-ajax-load", function( event ){
event.preventDefault();
$( this ).nextAll( '.sns-ajax-loading' ).show();
var name = $( this ).parent().prevAll( '.code' ).data( 'file-name' );
$( collection ).each( function( index, element ){
if ( element.name == name ) {
element.cm.setValue( element.raw );
return;
}
});
compile();
$( '.sns-ajax-loading' ).hide();
$( this ).nextAll( '.single-status' )
.show().delay(3000).fadeOut()
.children('.settings-error').text( 'Original Source File Loaded.' );
});
// Save
$( context ).on( "click", ".sns-ajax-save", function( event ){
event.preventDefault();
$( this ).nextAll( '.sns-ajax-loading' ).show();
$form.submit();
});
function saved( data ) {
$(data).insertAfter( '#icon-sns + h2' ).delay(3000).fadeOut();
$( '.sns-ajax-loading' ).hide();
}
// The CSS output side.
$css = $( '.css', "#css_area" );
if ( preview ) {
compiled = createCSSEditor();
}
$error = $( "#compiled_error" );
$status = $( "#compile_status" );
// Start.
compile();
loaded = true;
$form = $( "#less_area" ).closest( 'form' );
$form.submit( function( event ){
event.preventDefault();
compile();
$.ajax({
type: "POST",
url: window.location,
data: $(this).serialize()+'&ajaxsubmit=1',
cache: false,
success: saved
});
});
function createCSSEditor() {
return CodeMirror.fromTextArea(
$css.get(0),
{ lineNumbers: true, mode: "css", theme: theme, indentWithTabs: true, tabSize: 4, indentUnit: 4 }
);
}
function compile() {
var lessValue = '';
var totalLines = 0;
var totalChars = 0;
var compiledValue;
$( collection ).each(function(){
//this.cm.save();
lessValue += "\n" + this.$text.val();
this.lines = this.cm.lineCount();
this.startLine = totalLines;
totalLines += this.lines;
this.endLine = totalLines;
this.chars = this.$text.val().length + 1;
this.startChars = totalChars;
totalChars += this.chars;
this.endChars = totalChars;
});
var parser = new( less.Parser )({});
parser.parse( lessValue, function ( err, tree ) {
if ( err ){
doError( err );
} else {
try {
$error.hide();
if ( preview ) {
$( compiled.getWrapperElement() ).show();
compiledValue = tree.toCSS();
compiled.setValue( compiledValue );
compiled.save();
compiled.refresh();
} else {
compiledValue = tree.toCSS({ compress: true });
$css.val( compiledValue );
}
clearCompileError();
}
catch ( err ) {
doError( err );
}
}
});
clearTimeout( _SnS_options.theme_compiler_timer );
$status.hide();
}
function doError( err ) {
var pos, token, start, end, errLine, fileName, errMessage, errIndex;
errLine = err.line-1;
errorMirror = null;
$( collection ).each(function( i ){
if ( this.startLine <= errLine && errLine < this.endLine ) {
errorMirror = this.cm;
errLine = errLine - this.startLine -1;
fileName = this.name;
errIndex = err.index - this.startChars;
return;
}
});
if ( preview )
$( compiled.getWrapperElement()).hide();
var errMessage = '';
errMessage = " &nbsp; <em>LESS " + err.type +" Error</em> on line " + ( errLine + 1 ) + " of " + fileName + ". <br />" + err.message + "</p>";
if ( loaded ) {
$error
.removeClass( 'error' )
.addClass( 'updated' )
.show()
.html( "<p><strong>Warning:</strong>" + errMessage + "</p>" );
} else {
$error
.show()
.html( "<p><strong>Error: &nbsp; </strong>" + errMessage + "</p>" );
}
clearCompileError();
if (!errorMirror) return;
errorMarker = errorMirror.setGutterMarker( errLine, 'note-gutter', $('<span></span>').addClass('cm-error').css('marginLeft','4px').text('✖').get(0) );
//errorMirror.addLineClass( errLine, "wrap", cm-error" );
pos = errorMirror.posFromIndex( errIndex );
token = errorMirror.getTokenAt( pos );
start = errorMirror.posFromIndex( errIndex - 1 );
end = errorMirror.posFromIndex( errIndex + token.string.length - 1 );
errorText = errorMirror.markText( start, end, { className: "cm-error" } );
if ( preview ) {
//compiled.setValue( "" );
//compiled.save();
//compiled.refresh();
}
}
function clearCompileError() {
if ( errorMarker ) {
$( collection ).each(function( i ){
this.cm.clearGutter( 'note-gutter' );
});
//errorMirror.removeLineClass( errLine, "wrap", "cm-error" );
errorMarker = false;
}
if ( errorText ) {
errorText.clear();
errorText = false;
}
}
_SnS_options.theme_compiler = compile;
});