From 00a934b85d81f7f164440e4037f7de0aa2d615e1 Mon Sep 17 00:00:00 2001 From: Eston Bond Date: Fri, 12 Nov 2010 13:37:59 -0800 Subject: [PATCH] Abstracting Dialog out of editor --- .../javascript/gollum-editor/gollum.editor.js | 179 +---------------- .../public/javascript/gollum.dialog.js | 181 ++++++++++++++++++ 2 files changed, 182 insertions(+), 178 deletions(-) create mode 100644 lib/gollum/frontend/public/javascript/gollum.dialog.js diff --git a/lib/gollum/frontend/public/javascript/gollum-editor/gollum.editor.js b/lib/gollum/frontend/public/javascript/gollum-editor/gollum.editor.js index f34e9fa0..29d57204 100644 --- a/lib/gollum/frontend/public/javascript/gollum-editor/gollum.editor.js +++ b/lib/gollum/frontend/public/javascript/gollum-editor/gollum.editor.js @@ -446,189 +446,12 @@ }; - - /** - * Dialog - * Used by FunctionBar & internally to display editor-specific messages, - * inputs and more. - * - */ - var Dialog = { - - markupCreated: false, - - attachEvents: function( evtOK ) { - $('#gollum-editor-action-ok').click(function( e ) { - Dialog.eventOK( e, evtOK ); - }); - $('#gollum-editor-action-cancel').click( Dialog.eventCancel ); - }, - - createFieldMarkup: function( fieldArray ) { - var fieldMarkup = '
'; - for ( var i=0; i < fieldArray.length; i++ ) { - if ( typeof fieldArray[i] == 'object' ) { - fieldMarkup += '
'; - switch ( fieldArray[i].type ) { - - // only text is supported for now - case 'text': - case 'code': - default: - fieldMarkup += Dialog.createFieldText( fieldArray[i] ); - break; - - } - fieldMarkup += '
'; - } - - } - fieldMarkup += '
'; - return fieldMarkup; - }, - - createFieldText: function( fieldAttributes ) { - var html = ''; - - if ( fieldAttributes.name ) { - html += ''; - } - - html += ''; - } - - return html; - }, - - createMarkup: function( title, body ) { - Dialog.markupCreated = true; - return '
' + - '
' + - '
' + - '

' + - title +'

' + - '
' + body + '
' + - '
' + - 'Cancel' + - 'OK' + - '
' + - '
' + - '
'; - }, - - eventCancel: function( e ) { - e.preventDefault(); - debug('Cancelled dialog.'); - Dialog.hide(); - }, - - eventOK: function( e, evtOK ) { - e.preventDefault(); - - var results = []; - // get the results from each field and build them into the object - $('#gollum-editor-dialog-body input').each(function() { - results[$(this).attr('name')] = $(this).val(); - }); - - // pass them to evtOK if it exists (which it should) - if ( evtOK && - typeof evtOK == 'function' ) { - evtOK( results ); - } - Dialog.hide(); - }, - - hide: function() { - $('#gollum-editor-dialog').animate({ opacity: 0 }, { - duration: 200, - complete: function() { - $('#gollum-editor-dialog').removeClass('active'); - } - }); - }, - - init: function( argObject ) { - var title = ''; - var body = ''; - - // bail out if necessary - if ( !argObject || - typeof argObject != 'object' ) { - debug('Editor Dialog: Cannot init; invalid init object'); - return; - } - - // alright, build out fields - if ( argObject.fields && typeof argObject.fields == 'object' ) { - body = Dialog.createFieldMarkup( argObject.fields ); - } - - if ( argObject.title && typeof argObject.title == 'string' ) { - title = argObject.title; - } - - if ( Dialog.markupCreated ) { - $('#gollum-editor-dialog').remove(); - } - var $dialog = $( Dialog.createMarkup( title, body ) ); - $('body').append( $dialog ); - if ( argObject.OK && - typeof argObject.OK == 'function' ) { - Dialog.attachEvents( argObject.OK ); - } - Dialog.show(); - }, - - show: function() { - if ( !Dialog.markupCreated ) { - debug('Dialog: No markup to show. Please use init first.') - } else { - debug('Showing dialog'); - $('#gollum-editor-dialog').animate({ opacity: 0 }, { - duration: 1, - complete: function() { - $('#gollum-editor-dialog').addClass('active'); - Dialog.position(); // position this thing - $('#gollum-editor-dialog').animate({ opacity: 1 }, { - duration: 500 - }); - } - }); - } - }, - - position: function() { - var dialogHeight = $('#gollum-editor-dialog-inner').height(); - debug(dialogHeight); - $('#gollum-editor-dialog-inner') - .css('height', dialogHeight + 'px') - .css('margin-top', -1 * parseInt( dialogHeight / 2 )); - } - - }; - - /** * $.GollumEditor.Dialog * Used in exec() to display dialogs with dynamic fields. * */ - $.GollumEditor.Dialog = Dialog; + $.GollumEditor.Dialog = $.GollumDialog; $.GollumEditor.replaceSelection = function( repText ) { FunctionBar.replaceFieldSelection( $('#gollum-editor-body'), repText ); } diff --git a/lib/gollum/frontend/public/javascript/gollum.dialog.js b/lib/gollum/frontend/public/javascript/gollum.dialog.js new file mode 100644 index 00000000..c6252a02 --- /dev/null +++ b/lib/gollum/frontend/public/javascript/gollum.dialog.js @@ -0,0 +1,181 @@ +/** + * Dialog + * Used by FunctionBar & internally to display editor-specific messages, + * inputs and more. + * + */ + +(function($) { + + var Dialog = { + + markupCreated: false, + + attachEvents: function( evtOK ) { + $('#gollum-editor-action-ok').click(function( e ) { + Dialog.eventOK( e, evtOK ); + }); + $('#gollum-editor-action-cancel').click( Dialog.eventCancel ); + }, + + createFieldMarkup: function( fieldArray ) { + var fieldMarkup = '
'; + for ( var i=0; i < fieldArray.length; i++ ) { + if ( typeof fieldArray[i] == 'object' ) { + fieldMarkup += '
'; + switch ( fieldArray[i].type ) { + + // only text is supported for now + case 'text': + case 'code': + default: + fieldMarkup += Dialog.createFieldText( fieldArray[i] ); + break; + + } + fieldMarkup += '
'; + } + + } + fieldMarkup += '
'; + return fieldMarkup; + }, + + createFieldText: function( fieldAttributes ) { + var html = ''; + + if ( fieldAttributes.name ) { + html += ''; + } + + html += ''; + } + + return html; + }, + + createMarkup: function( title, body ) { + Dialog.markupCreated = true; + return '
' + + '
' + + '
' + + '

' + + title +'

' + + '
' + body + '
' + + '
' + + 'Cancel' + + 'OK' + + '
' + + '
' + + '
'; + }, + + eventCancel: function( e ) { + e.preventDefault(); + debug('Cancelled dialog.'); + Dialog.hide(); + }, + + eventOK: function( e, evtOK ) { + e.preventDefault(); + + var results = []; + // get the results from each field and build them into the object + $('#gollum-editor-dialog-body input').each(function() { + results[$(this).attr('name')] = $(this).val(); + }); + + // pass them to evtOK if it exists (which it should) + if ( evtOK && + typeof evtOK == 'function' ) { + evtOK( results ); + } + Dialog.hide(); + }, + + hide: function() { + $('#gollum-editor-dialog').animate({ opacity: 0 }, { + duration: 200, + complete: function() { + $('#gollum-editor-dialog').removeClass('active'); + } + }); + }, + + init: function( argObject ) { + var title = ''; + var body = ''; + + // bail out if necessary + if ( !argObject || + typeof argObject != 'object' ) { + debug('Editor Dialog: Cannot init; invalid init object'); + return; + } + + // alright, build out fields + if ( argObject.fields && typeof argObject.fields == 'object' ) { + body = Dialog.createFieldMarkup( argObject.fields ); + } + + if ( argObject.title && typeof argObject.title == 'string' ) { + title = argObject.title; + } + + if ( Dialog.markupCreated ) { + $('#gollum-editor-dialog').remove(); + } + var $dialog = $( Dialog.createMarkup( title, body ) ); + $('body').append( $dialog ); + if ( argObject.OK && + typeof argObject.OK == 'function' ) { + Dialog.attachEvents( argObject.OK ); + } + Dialog.show(); + }, + + show: function() { + if ( !Dialog.markupCreated ) { + debug('Dialog: No markup to show. Please use init first.') + } else { + debug('Showing dialog'); + $('#gollum-editor-dialog').animate({ opacity: 0 }, { + duration: 1, + complete: function() { + $('#gollum-editor-dialog').addClass('active'); + Dialog.position(); // position this thing + $('#gollum-editor-dialog').animate({ opacity: 1 }, { + duration: 500 + }); + } + }); + } + }, + + position: function() { + var dialogHeight = $('#gollum-editor-dialog-inner').height(); + debug(dialogHeight); + $('#gollum-editor-dialog-inner') + .css('height', dialogHeight + 'px') + .css('margin-top', -1 * parseInt( dialogHeight / 2 )); + } + + }; + + $.GollumDialog = Dialog; + +})(jQuery); \ No newline at end of file