Permalink
Browse files

Merge branch 'release/v0.0.1'

  • Loading branch information...
Anatoliy Chakkaev
Anatoliy Chakkaev committed Mar 31, 2011
2 parents 0ff26e5 + dc83ef0 commit 920cba60ed864309b7e6a76615f0fdb0b956ad6d
Showing with 68 additions and 62 deletions.
  1. +68 −62 plugin.js
View
130 plugin.js
@@ -25,73 +25,57 @@ GENTICS.Aloha.ColorSelector.init = function () {
var that = this,
style = jQuery('<style>' +
+ '.GENTICS_dropdown_colorbox {padding: 4px; position: absolute; background: #FFFFFF; z-index: 11001; border: 1px solid #AAAAAA;}'+
'.GENTICS_button_text_color {background: url("/javascripts/aloha/deps/extjs/resources/images/default/editor/tb-sprite.gif") repeat scroll -160px 0 transparent !important}' +
'.GENTICS_button_text_background {background: url("/javascripts/aloha/deps/extjs/resources/images/default/editor/tb-sprite.gif") repeat scroll -176px 0 transparent !important}' +
'</style>');
jQuery.each(GENTICS.Aloha.ColorSelector.config.colors, function(index, value){
- style.append('button.GENTICS_button_'+ value +' { background: '+ value +' !important; border: 0; float: left; margin: 3px; width: 16px; height: 16px;} ');
+ style.append('button.GENTICS_button_'+ value +' { background: '+ value +' !important; border: 0; float: left; margin: 3px; width: 16px; height: 16px; border: 1px solid #888;} ');
});
style.appendTo('head');
- // // add it to the floating menu
- // GENTICS.Aloha.FloatingMenu.addButton(
- // 'GENTICS.Aloha.continuoustext',
- // button,
- // GENTICS.Aloha.i18n(GENTICS.Aloha, 'floatingmenu.tab.format'),
- // 1
- // );
-
- // jQuery.each(GENTICS.Aloha.ColorSelector.config.colors, function(index, value){
- // GENTICS.Aloha.FloatingMenu.addButton(
- // "GENTICS.Aloha.continuoustext",
- // buttons[value],
- // that.i18n("floatingmenu.tab.color"),
- // 1
- // );
- // });
var layer = new Layer();
-
- GENTICS.Aloha.FloatingMenu.addButton(
- "GENTICS.Aloha.continuoustext",
- new GENTICS.Aloha.ui.Button({
- "iconClass": "GENTICS_button_text_color",
- "size": "small",
- "onclick": function (button) {
- layer.display(button.el.dom, 'color');
- }
- }),
- that.i18n("floatingmenu.tab.format"),
- 1
- );
-
- GENTICS.Aloha.FloatingMenu.addButton(
- "GENTICS.Aloha.continuoustext",
- new GENTICS.Aloha.ui.Button({
- "iconClass": "GENTICS_button_text_background",
- "size": "small",
- "onclick": function (button) {
- layer.display(button.el.dom, 'background-color');
+ var buttons = {};
+ jQuery(['color', 'background-color']).each(function (index, style) {
+ buttons[style] = new GENTICS.Aloha.ui.Button({
+ 'iconClass': 'GENTICS_button_text_' + style.split('-')[0],
+ 'size': 'small',
+ 'toggle': true,
+ 'onclick': function (button) {
+ layer.display(button, style);
}
- }),
- that.i18n("floatingmenu.tab.format"),
- 1
- );
- function Layer () {
- }
+ });
+ GENTICS.Aloha.FloatingMenu.addButton(
+ 'GENTICS.Aloha.continuoustext',
+ buttons[style],
+ that.i18n('floatingmenu.tab.format'),
+ 1
+ );
+ });
+
+ function Layer () {}
Layer.prototype.display = function (target, style) {
- if (this.visible) return;
+ if (this.visible) {
+ if (this.visible === style) {
+ return;
+ } else {
+ this.hide();
+ }
+ }
var that = this;
- this.target = jQuery(target);
+ this.targetButton = buttons[style];
+ this.targetButton.setPressed(true);
+ this.target = jQuery(target.el.dom);
var offset = this.target.offset();
- this.el = jQuery('<div style="padding: 4px; position: absolute; background: #ddd; z-index: 11001"></div>');
+ this.el = jQuery('<div class="GENTICS_dropdown_colorbox"></div>');
this.el.css('top', offset.top + this.target.height());
this.el.css('left', offset.left);
this.populateColors(style);
this.el.width(132);
- this.el.height(60);
+ this.el.height(44);
this.el.show();
jQuery('body').append(this.el).bind('click', function(e) {
if (that.visible) {
@@ -100,7 +84,7 @@ GENTICS.Aloha.ColorSelector.init = function () {
});
this.editable = GENTICS.Aloha.activeEditable;
setTimeout(function () {
- that.visible = true;
+ that.visible = style;
}, 10);
};
@@ -112,19 +96,7 @@ GENTICS.Aloha.ColorSelector.init = function () {
if (that.editable) {
that.editable.obj[0].focus();
}
- var markup = jQuery('<span style="' + style + ':' + value + '"></span>');
- var rangeObject = GENTICS.Aloha.Selection.rangeObject;
- var foundMarkup = rangeObject.findMarkup(function() {
- return this.nodeName.toLowerCase() == markup.get(0).nodeName.toLowerCase()
- },
- that.editable.obj);
-
- if (foundMarkup) {
- jQuery(foundMarkup).css(style, value);
- } else {
- GENTICS.Utils.Dom.addMarkup(rangeObject, markup)
- }
- rangeObject.select();
+ applyFormat(style, value);
that.hide();
return false;
});
@@ -137,6 +109,40 @@ GENTICS.Aloha.ColorSelector.init = function () {
this.el.remove();
this.visible = false;
jQuery('body').unbind('click');
+ this.targetButton.setPressed(false);
};
+ function applyFormat (style, value) {
+ var markupClassName = 'GENTICS_markup_' + [style.replace('-', '_'), value].join('_');
+
+ var markup = jQuery('<span class="' + markupClassName + '" style="' + style + ':' + value + '"></span>');
+ var rangeObject = GENTICS.Aloha.Selection.rangeObject;
+
+ // check whether the markup is found in the range (at the start of the range)
+ var foundMarkup = rangeObject.findMarkup(function() {
+ return this.nodeName.toLowerCase() == markup.get(0).nodeName.toLowerCase() && this.className === markupClassName;
+ }, GENTICS.Aloha.activeEditable.obj);
+
+ if (foundMarkup) {
+ // remove the markup
+ if (rangeObject.isCollapsed()) {
+ // when the range is collapsed, we remove exactly the one DOM element
+ GENTICS.Utils.Dom.removeFromDOM(foundMarkup, rangeObject, true);
+ } else {
+ // the range is not collapsed, so we remove the markup from the range
+ GENTICS.Utils.Dom.removeMarkup(rangeObject, markup, GENTICS.Aloha.activeEditable.obj);
+ }
+ } else {
+ // when the range is collapsed, extend it to a word
+ if (rangeObject.isCollapsed()) {
+ GENTICS.Utils.Dom.extendToWord(rangeObject);
+ }
+
+ // add the markup
+ GENTICS.Utils.Dom.addMarkup(rangeObject, markup);
+ }
+ // select the modified range
+ rangeObject.select();
+ }
+
};

0 comments on commit 920cba6

Please sign in to comment.