jQuery -> $ in admin editor

This commit is contained in:
ilvalle
2013-10-23 14:07:51 +02:00
parent 339de2fa41
commit ff7cb99bfe
5 changed files with 82 additions and 78 deletions
+1 -1
View File
@@ -583,7 +583,7 @@ def edit():
response.headers["web2py-component-flash"] = T('Preferences saved correctly') response.headers["web2py-component-flash"] = T('Preferences saved correctly')
else: else:
response.headers["web2py-component-flash"] = T('Preferences saved on session only') response.headers["web2py-component-flash"] = T('Preferences saved on session only')
response.headers["web2py-component-command"] = "update_editor(%s);jQuery('a[href=#editor_settings] button.close').click();" % response.json(config.read()) response.headers["web2py-component-command"] = "update_editor(%s);$('a[href=#editor_settings] button.close').click();" % response.json(config.read())
return return
else: else:
details = {'filename':'settings', 'id':'editor_settings', 'force': False} details = {'filename':'settings', 'id':'editor_settings', 'force': False}
+39 -37
View File
@@ -1,3 +1,5 @@
var template_js = '<p class="repo-name">{{{a_tag}}}</p><small>{{address}}</small>';
function prepareDataForSave(name, data) { function prepareDataForSave(name, data) {
var obj = new Object(); var obj = new Object();
obj.Name = name; obj.Name = name;
@@ -22,8 +24,8 @@ function prepareMultiPartPOST(data) {
} }
function on_error() { function on_error() {
jQuery("input[name='saved_on']").attr('style', 'background-color:red'); $("input[name='saved_on']").attr('style', 'background-color:red');
jQuery("input[name='saved_on']").val('communication error'); $("input[name='saved_on']").val('communication error');
} }
function doHighlight(highlight) { function doHighlight(highlight) {
@@ -36,24 +38,24 @@ function doHighlight(highlight) {
function doClickSave() { function doClickSave() {
var currentTabID = '#' + jQuery('#edit_placeholder div.tab-pane.active').attr('id'); var currentTabID = '#' + $('#edit_placeholder div.tab-pane.active').attr('id');
var editor = jQuery(currentTabID + ' textarea').data('editor'); var editor = $(currentTabID + ' textarea').data('editor');
var data = editor.getValue(); var data = editor.getValue();
var dataForPost = prepareMultiPartPOST(new Array( var dataForPost = prepareMultiPartPOST(new Array(
prepareDataForSave('data', data), prepareDataForSave('data', data),
prepareDataForSave('file_hash', prepareDataForSave('file_hash',
jQuery(currentTabID + " input[name='file_hash']").val()), $(currentTabID + " input[name='file_hash']").val()),
prepareDataForSave('saved_on', prepareDataForSave('saved_on',
jQuery(currentTabID + " input[name='saved_on']").val()), $(currentTabID + " input[name='saved_on']").val()),
prepareDataForSave('saved_on', prepareDataForSave('saved_on',
jQuery(currentTabID + " input[name='saved_on']").val()), $(currentTabID + " input[name='saved_on']").val()),
prepareDataForSave('from_ajax', 'true'))); prepareDataForSave('from_ajax', 'true')));
// console.info(area.textarea.value); // console.info(area.textarea.value);
jQuery(currentTabID + " input[name='saved_on']").attr('style', $(currentTabID + " input[name='saved_on']").attr('style',
'background-color:yellow'); 'background-color:yellow');
jQuery(currentTabID + " input[name='saved_on']").val('saving now...') $(currentTabID + " input[name='saved_on']").val('saving now...')
currentUrl = jQuery(currentTabID + ' form').attr('action'); currentUrl = $(currentTabID + ' form').attr('action');
jQuery.ajax({ $.ajax({
type: "POST", type: "POST",
contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"', contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"',
url: currentUrl, url: currentUrl,
@@ -67,38 +69,38 @@ function doClickSave() {
'doClickSave'); 'doClickSave');
}, },
success: function (json, text, xhr) { success: function (json, text, xhr) {
jQuery(editor).data('saved', true); // Set as saved $(editor).data('saved', true); // Set as saved
editor.on("change", store_changes_function); // Re-enable change watcher editor.on("change", store_changes_function); // Re-enable change watcher
// reenable disabled submit button // reenable disabled submit button
var t = jQuery("input[name='save']"); var t = $("input[name='save']");
t.attr('class', ''); t.attr('class', '');
t.attr('disabled', ''); t.attr('disabled', '');
var flash = xhr.getResponseHeader('web2py-component-flash'); var flash = xhr.getResponseHeader('web2py-component-flash');
if(flash) { if(flash) {
jQuery('.flash').html(decodeURIComponent(flash)) $('.flash').html(decodeURIComponent(flash))
.append('<a href="#" class="close">&times;</a>') .append('<a href="#" class="close">&times;</a>')
.slideDown(); .slideDown();
} else jQuery('.flash').hide(); } else $('.flash').hide();
try { try {
if(json.error) { if(json.error) {
window.location.href = json.redirect; window.location.href = json.redirect;
} else { } else {
// console.info( json.file_hash ); // console.info( json.file_hash );
jQuery(currentTabID + " input[name='file_hash']").val(json.file_hash); $(currentTabID + " input[name='file_hash']").val(json.file_hash);
jQuery(currentTabID + " input[name='saved_on']").val(json.saved_on); $(currentTabID + " input[name='saved_on']").val(json.saved_on);
if(json.highlight) { if(json.highlight) {
doHighlight(json.highlight); doHighlight(json.highlight);
} else { } else {
jQuery(currentTabID + " input[name='saved_on']").attr('style', 'background-color:#99FF99'); $(currentTabID + " input[name='saved_on']").attr('style', 'background-color:#99FF99');
//jQuery(".flash").delay(1000).fadeOut('slow'); //$(".flash").delay(1000).fadeOut('slow');
} }
// console.info(jQuery("input[name='file_hash']").val()); // console.info($("input[name='file_hash']").val());
var output = '<b>exposes:</b> '; var output = '<b>exposes:</b> ';
for(var i in json.functions) { for(var i in json.functions) {
output += ' <a target="_blank" href="/' + json.application + '/' + json.controller + '/' + json.functions[i] + '">' + json.functions[i] + '</a>,'; output += ' <a target="_blank" href="/' + json.application + '/' + json.controller + '/' + json.functions[i] + '">' + json.functions[i] + '</a>,';
} }
if(output != '<b>exposes:</b> ') { if(output != '<b>exposes:</b> ') {
jQuery(currentTabID + " .exposed").html(output.substring(0, output.length - 1)); $(currentTabID + " .exposed").html(output.substring(0, output.length - 1));
} }
} }
} catch(e) { } catch(e) {
@@ -113,8 +115,8 @@ function doClickSave() {
} }
function getActiveEditor() { function getActiveEditor() {
var currentTabID = '#' + jQuery('#edit_placeholder div.tab-pane.active').attr('id'); var currentTabID = '#' + $('#edit_placeholder div.tab-pane.active').attr('id');
var editor = jQuery(currentTabID + ' textarea').data('editor'); var editor = $(currentTabID + ' textarea').data('editor');
return editor; return editor;
} }
@@ -139,7 +141,7 @@ function doToggleBreakpoint(filename, url, sel) {
prepareDataForSave('sel_start', sel["start"]), prepareDataForSave('sel_start', sel["start"]),
prepareDataForSave('sel_end', sel["end"]), prepareDataForSave('sel_end', sel["end"]),
prepareDataForSave('data', sel['data']))); prepareDataForSave('data', sel['data'])));
jQuery.ajax({ $.ajax({
type: "POST", type: "POST",
contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"', contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"',
url: url, url: url,
@@ -156,10 +158,10 @@ function doToggleBreakpoint(filename, url, sel) {
// show flash message (if any) // show flash message (if any)
var flash = xhr.getResponseHeader('web2py-component-flash'); var flash = xhr.getResponseHeader('web2py-component-flash');
if(flash) { if(flash) {
jQuery('.flash').html(decodeURIComponent(flash)) $('.flash').html(decodeURIComponent(flash))
.append('<a href="#" class="close">&times;</a>') .append('<a href="#" class="close">&times;</a>')
.slideDown(); .slideDown();
} else jQuery('.flash').hide(); } else $('.flash').hide();
try { try {
if(json.error) { if(json.error) {
window.location.href = json.redirect; window.location.href = json.redirect;
@@ -189,7 +191,7 @@ function doListBreakpoints(filename, url, editor) {
var dataForPost = prepareMultiPartPOST(new Array( var dataForPost = prepareMultiPartPOST(new Array(
prepareDataForSave('filename', filename) prepareDataForSave('filename', filename)
)); ));
jQuery.ajax({ $.ajax({
type: "POST", type: "POST",
contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"', contentType: 'multipart/form-data;boundary="' + dataForPost[1] + '"',
url: url, url: url,
@@ -235,7 +237,7 @@ function makeMarker() {
function keepalive(url) { function keepalive(url) {
jQuery.ajax({ $.ajax({
type: "GET", type: "GET",
url: url, url: url,
timeout: 1000, timeout: 1000,
@@ -247,7 +249,7 @@ function keepalive(url) {
} }
function load_file(url) { function load_file(url) {
jQuery.ajax({ $.ajax({
type: "GET", type: "GET",
contentType: 'application/json', contentType: 'application/json',
cache: false, cache: false,
@@ -256,18 +258,18 @@ function load_file(url) {
timeout: 1000, timeout: 1000,
success: function (json) { success: function (json) {
if(typeof (json['plain_html']) !== undefined) { if(typeof (json['plain_html']) !== undefined) {
if(jQuery('#' + json['id']).length === 0 || json['force'] === true) { if($('#' + json['id']).length === 0 || json['force'] === true) {
// Create a tab and put the code in it // Create a tab and put the code in it
var tab_header = '<li><a href="#' + json['id'] + '" data-toggle="tab">' + json['filename'] + '<button type="button" class="close">&times;</button></a></li>'; var tab_header = '<li><a href="#' + json['id'] + '" data-toggle="tab">' + json['filename'] + '<button type="button" class="close">&times;</button></a></li>';
var tab_body = '<div id="' + json['id'] + '" class="tab-pane fade in " >' + json['plain_html'] + '</div>'; var tab_body = '<div id="' + json['id'] + '" class="tab-pane fade in " >' + json['plain_html'] + '</div>';
if(json['force'] === false) { if(json['force'] === false) {
jQuery('#filesTab').append(jQuery(tab_header)); $('#filesTab').append($(tab_header));
jQuery('#myTabContent').append(jQuery(tab_body)); $('#myTabContent').append($(tab_body));
} else { } else {
jQuery('#' + json['id']).html(jQuery(tab_body)); $('#' + json['id']).html($(tab_body));
} }
} }
jQuery("a[href='#" + json['id'] + "']").click(); $("a[href='#" + json['id'] + "']").click();
} }
}, },
error: function (x) { error: function (x) {
@@ -279,11 +281,11 @@ function load_file(url) {
function set_font(editor, incr) { function set_font(editor, incr) {
var fontSize = ''; var fontSize = '';
if(incr !== 0) { if(incr !== 0) {
fontSize = parseInt(jQuery(editor.getWrapperElement()).css('font-size')); fontSize = parseInt($(editor.getWrapperElement()).css('font-size'));
fontSize = fontSize + incr + "px"; fontSize = fontSize + incr + "px";
} }
jQuery(editor.getWrapperElement()).css('font-size', fontSize); $(editor.getWrapperElement()).css('font-size', fontSize);
editor.refresh(); editor.refresh();
} }
var template_js = '<p class="repo-name">{{{a_tag}}}</p><small>{{address}}</small>';
+40 -37
View File
@@ -15,6 +15,8 @@
return A(file, _class='editor_filelink', _href=url, _style='word-wrap: nowrap;') return A(file, _class='editor_filelink', _href=url, _style='word-wrap: nowrap;')
}} }}
{{cm=URL('static%s' % (response.static_version and '/_' + response.static_version or ''),'codemirror')}} {{cm=URL('static%s' % (response.static_version and '/_' + response.static_version or ''),'codemirror')}}
{{js_url=URL('static%s' % (response.static_version and '/_' + response.static_version or ''),'js')}}
{{css_url=URL('static%s' % (response.static_version and '/_' + response.static_version or ''),'css')}}
<link rel="stylesheet" href="{{=cm}}/lib/codemirror.css"> <link rel="stylesheet" href="{{=cm}}/lib/codemirror.css">
<link rel="stylesheet" href="{{='%s/theme/%s.css' % (cm, editor_settings['theme'])}}"> <link rel="stylesheet" href="{{='%s/theme/%s.css' % (cm, editor_settings['theme'])}}">
<script src="{{=cm}}/lib/codemirror.js"></script> <script src="{{=cm}}/lib/codemirror.js"></script>
@@ -45,9 +47,9 @@
<script src="{{=cm}}/addon/fold/xml-fold.js"></script> <script src="{{=cm}}/addon/fold/xml-fold.js"></script>
<link rel="stylesheet" href="{{=cm}}/addon/fold/foldgutter.css"> <link rel="stylesheet" href="{{=cm}}/addon/fold/foldgutter.css">
<script src="{{=cm}}/emmet.min.js"></script> <script src="{{=cm}}/emmet.min.js"></script>
<script src="{{=URL('static','js/ajax_editor.js')}}"></script> <script src="{{=js_url}}/ajax_editor.js"></script>
<link rel="stylesheet" href="{{=URL('static/css','typeahead.js-bootstrap.css')}}"> <link rel="stylesheet" href="{{=css_url}}/typeahead.js-bootstrap.css">
<link rel="stylesheet" href="{{=URL('static/css','web2py-codemirror.css')}}"> <link rel="stylesheet" href="{{=css_url}}/web2py-codemirror.css">
<script type="text/javascript"> <script type="text/javascript">
var current_theme = "{{=editor_settings['theme']}}"; //Default theme var current_theme = "{{=editor_settings['theme']}}"; //Default theme
var current_editor = "{{=editor_settings['editor']}}"; //Default editor var current_editor = "{{=editor_settings['editor']}}"; //Default editor
@@ -61,32 +63,32 @@ var current_indentwithtabs = {{=editor_settings['indentwithtabs']}};
var current_tabwidth = {{=editor_settings['tabwidth']}}; var current_tabwidth = {{=editor_settings['tabwidth']}};
var current_font_incr = 0; // Default font-size, 0 means don't set var current_font_incr = 0; // Default font-size, 0 means don't set
jQuery(document).on('shown click', 'a[data-toggle="tab"]', function (e) { $(document).on('shown click', 'a[data-toggle="tab"]', function (e) {
var tab_id = jQuery(this).attr('href'); var tab_id = $(this).attr('href');
var editor = jQuery(tab_id + " textarea").data('editor'); var editor = $(tab_id + " textarea").data('editor');
if (editor) { if (editor) {
editor.setSize(jQuery(tab_id).width(), jQuery(tab_id).height()); editor.setSize($(tab_id).width(), $(tab_id).height());
editor.refresh(); editor.refresh();
} }
//jQuery(function(){jQuery('.CodeMirror-scroll').css("height","auto").css("overflow-x","auto");}); //$(function(){$('.CodeMirror-scroll').css("height","auto").css("overflow-x","auto");});
}); });
// Close the selected tab // Close the selected tab
jQuery(document).on('click', '#filesTab button[class="close"]', function (e) { $(document).on('click', '#filesTab button[class="close"]', function (e) {
var tab_body = jQuery(jQuery(this).parent().attr("href")); // it should be a div var tab_body = $($(this).parent().attr("href")); // it should be a div
var tab_header = jQuery(this).parent().parent(); // it should be a li var tab_header = $(this).parent().parent(); // it should be a li
var saved = jQuery(tab_body.find('textarea').data('editor')).data('saved'); var saved = $(tab_body.find('textarea').data('editor')).data('saved');
var close = true; var close = true;
if (saved === false) { if (saved === false) {
close = confirm("You are closing an unsaved file") close = confirm("You are closing an unsaved file")
} }
if (close) { if (close) {
if (tab_header.hasClass('active') === true) { //Set active an other tab if (tab_header.hasClass('active') === true) { //Set active an other tab
var $prev = jQuery(tab_header).prev(); var $prev = $(tab_header).prev();
if ($prev.length) { if ($prev.length) {
$prev.children('a[data-toggle="tab"]').tab('show'); // select previous tab $prev.children('a[data-toggle="tab"]').tab('show'); // select previous tab
} else { } else {
jQuery(tab_header).next().children('a[data-toggle="tab"]').tab('show'); // select next tab $(tab_header).next().children('a[data-toggle="tab"]').tab('show'); // select next tab
} }
} }
tab_header.remove(); //remove li of tab tab_header.remove(); //remove li of tab
@@ -95,40 +97,41 @@ jQuery(document).on('click', '#filesTab button[class="close"]', function (e) {
}); });
// Revert current file // Revert current file
jQuery(document).on('click', '#revert', function (e) { $(document).on('click', '#revert', function (e) {
e.preventDefault(); e.preventDefault();
load_file(jQuery(this).attr("href")); load_file($(this).attr("href"));
}); });
// Restore current file // Restore current file
jQuery(document).on('click', '#restore', function (e) { $(document).on('click', '#restore', function (e) {
e.preventDefault(); e.preventDefault();
load_file(jQuery(this).attr("href")); load_file($(this).attr("href"));
}); });
// open the selected file // open the selected file
jQuery(document).on('click', 'a.editor_filelink, a#editor_settingslink', function (e) { $(document).on('click', 'a.editor_filelink, a#editor_settingslink', function (e) {
e.preventDefault(); e.preventDefault();
var url = jQuery(this).attr("href"); var url = $(this).attr("href");
load_file(url); load_file(url);
}); });
/* This method updates all editors already instantiated with the selected preferences*/
function update_editor(config) { function update_editor(config) {
var href = "{{="%s/theme/" % cm}}" + config.theme + ".css"; var href = "{{="%s/theme/" % cm}}" + config.theme + ".css";
var link = jQuery("<link>"); var link = $("<link>");
link.attr({ link.attr({
type: 'text/css', type: 'text/css',
rel: 'stylesheet', rel: 'stylesheet',
href: href href: href
}); });
jQuery("head").append( link ); $("head").append( link );
if ( config.editor != 'default'){ if ( config.editor != 'default'){
var src = "{{='%s/keymap/' % cm}}" + config.editor + ".js"; var src = "{{='%s/keymap/' % cm}}" + config.editor + ".js";
//var src = "/admin/static/codemirror/keymap/" + editor_name + ".js"; //var src = "/admin/static/codemirror/keymap/" + editor_name + ".js";
var script = jQuery("<script>"); var script = $("<script>");
script.attr({ script.attr({
src: src src: src
}); });
jQuery("head").append( script ); $("head").append( script );
} }
current_theme = config.theme; current_theme = config.theme;
current_editor = config.editor; current_editor = config.editor;
@@ -137,8 +140,8 @@ function update_editor(config) {
current_indentwithtabs = (config.indentwithtabs === 'true'); current_indentwithtabs = (config.indentwithtabs === 'true');
current_tabwidth = parseInt(config.tabwidth); current_tabwidth = parseInt(config.tabwidth);
jQuery('textarea[name="data"]') .each(function(id, ta) { $('textarea[name="data"]') .each(function(id, ta) {
editor = jQuery(ta).data('editor'); editor = $(ta).data('editor');
editor.setOption("theme", current_theme); editor.setOption("theme", current_theme);
editor.setOption("keyMap", current_editor); editor.setOption("keyMap", current_editor);
editor.setOption("autoCloseTags", current_closetag); editor.setOption("autoCloseTags", current_closetag);
@@ -150,17 +153,17 @@ function update_editor(config) {
} }
// incr/decr editor font-size // incr/decr editor font-size
jQuery(document).on('click', 'a.font_button', function (e) { $(document).on('click', 'a.font_button', function (e) {
e.preventDefault(); e.preventDefault();
var id = jQuery(this).attr('id'); var id = $(this).attr('id');
var new_font_incr; var new_font_incr;
switch (id) { switch (id) {
case 'incr': new_incr = 2; break; case 'incr': new_incr = 2; break;
case 'decr': new_incr = -2; break; case 'decr': new_incr = -2; break;
case 'default': new_incr = 0; case 'default': new_incr = 0;
} }
jQuery('textarea[name="data"]').each(function(id, ta) { $('textarea[name="data"]').each(function(id, ta) {
editor = jQuery(ta).data('editor'); editor = $(ta).data('editor');
set_font(editor, new_incr); set_font(editor, new_incr);
}); });
current_font_incr = (new_incr !== 0) ? current_font_incr + new_incr : 0; current_font_incr = (new_incr !== 0) ? current_font_incr + new_incr : 0;
@@ -186,7 +189,7 @@ jQuery(document).on('click', 'a.font_button', function (e) {
<div class='row-fluid'> <div class='row-fluid'>
<div class="right controls btn-toolbar pull-right"> <div class="right controls btn-toolbar pull-right">
<div class="btn-group"> <div class="btn-group">
<a class="button btn" onclick="jQuery('#files').toggle(); return false" href="#">Files toggle</a> <a class="button btn" onclick="$('#files').toggle(); return false" href="#">Files toggle</a>
</div> </div>
<div class="btn-group"> <div class="btn-group">
<a id="decr" class="button btn font_button">-</a> <a id="decr" class="button btn font_button">-</a>
@@ -242,26 +245,26 @@ jQuery(document).on('click', 'a.font_button', function (e) {
</div> </div>
</div> </div>
<script> <script>
jQuery(document).ready(function() { $(document).ready(function() {
var filesMenu = jQuery('#files'); var filesMenu = $('#files');
var ow = filesMenu.outerWidth(); var ow = filesMenu.outerWidth();
filesMenu.width(ow); filesMenu.width(ow);
setTimeout( function(){jQuery('#files').css('left', '-'+ow+'px');}, 1000); setTimeout( function(){$('#files').css('left', '-'+ow+'px');}, 1000);
}); });
</script> </script>
<!-- Typeahead scripts here so the page load faster --> <!-- Typeahead scripts here so the page load faster -->
<script src="{{=URL('static%s' % (response.static_version and '/_' + response.static_version or ''), 'js/typeahead.min.js')}}"></script> <script src="{{=URL('static%s' % (response.static_version and '/_' + response.static_version or ''), 'js/typeahead.min.js')}}"></script>
<script src="{{=URL('static%s' % (response.static_version and '/_' + response.static_version or ''), 'js/hogan-2.0.0.js')}}"></script> <script src="{{=URL('static%s' % (response.static_version and '/_' + response.static_version or ''), 'js/hogan-2.0.0.js')}}"></script>
<script> <script>
jQuery('.typeahead-tw').typeahead({ $('.typeahead-tw').typeahead({
name: 'files', name: 'files',
local:{{from gluon.serializers import json}}{{=XML(json(auto_complete_list))}}, local:{{from gluon.serializers import json}}{{=XML(json(auto_complete_list))}},
template: template_js, template: template_js,
engine: Hogan, engine: Hogan,
}); });
jQuery(document).on("typeahead:selected", '.typeahead', function(e, datum) { $(document).on("typeahead:selected", '.typeahead', function(e, datum) {
load_file(datum.link); load_file(datum.link);
jQuery(this).val(''); $(this).val('');
}); });
</script> </script>
<!-- end "edit" block --> <!-- end "edit" block -->
@@ -39,7 +39,7 @@
</form> </form>
<script> <script>
web2py_trap_form("{{=URL('default', 'edit', args=request.args, vars={'settings':True})}}", "editor_settings"); web2py_trap_form("{{=URL('default', 'edit', args=request.args, vars={'settings':True})}}", "editor_settings");
jQuery("#editor_settings_form").on('change', 'select, input', function (e) { $("#editor_settings_form").on('change', 'select, input', function (e) {
jQuery("#editor_settings_form button[type=submit]").removeClass('disabled'); $("#editor_settings_form button[type=submit]").removeClass('disabled');
}); });
</script> </script>
-1
View File
@@ -12,7 +12,6 @@
response.files.append(URL('static','css/bootstrap-responsive.min.css')) response.files.append(URL('static','css/bootstrap-responsive.min.css'))
}} }}
{{include 'web2py_ajax.html'}} {{include 'web2py_ajax.html'}}
<script>jQuery.noConflict();</script>
</head> </head>
<body class="{{=T('direction: ltr') == 'direction: rtl' and 'RTLbody' or ''}} {{block sectionclass}}home{{end}}"> <body class="{{=T('direction: ltr') == 'direction: rtl' and 'RTLbody' or ''}} {{block sectionclass}}home{{end}}">