pad.libre-service.eu-etherpad/src/node/utils/toolbar.js

240 lines
5.8 KiB
JavaScript
Raw Normal View History

2013-03-04 00:53:17 +01:00
/**
* The Toolbar Module creates and renders the toolbars and buttons
*/
var _ = require("underscore")
2013-03-09 23:57:42 +01:00
, tagAttributes
, tag
2013-03-04 00:53:17 +01:00
, defaultButtons
, Button
, ButtonsGroup
, Separator
2013-03-09 23:57:42 +01:00
, defaultButtonAttributes;
2013-03-04 00:53:17 +01:00
defaultButtonAttributes = function (name, overrides) {
return {
command: name,
2013-03-04 00:53:17 +01:00
localizationId: "pad.toolbar." + name + ".title",
class: "buttonicon buttonicon-" + name
2013-03-04 00:53:17 +01:00
};
};
2013-03-09 23:57:42 +01:00
tag = function (name, attributes, contents) {
var aStr = tagAttributes(attributes);
if (_.isString(contents) && contents.length > 0) {
2013-03-09 23:57:42 +01:00
return '<' + name + aStr + '>' + contents + '</' + name + '>';
}
else {
return '<' + name + aStr + '></' + name + '>';
2013-03-09 23:57:42 +01:00
}
};
tagAttributes = function (attributes) {
attributes = _.reduce(attributes || {}, function (o, val, name) {
2013-03-09 23:57:42 +01:00
if (!_.isUndefined(val)) {
o[name] = val;
}
return o;
}, {});
return " " + _.map(attributes, function (val, name) {
return "" + name + '="' + _.escape(val) + '"';
}).join(" ");
2013-03-09 23:57:42 +01:00
};
2013-03-04 00:53:17 +01:00
ButtonsGroup = function () {
this.buttons = [];
};
ButtonsGroup.fromArray = function (array) {
var btnGroup = new this;
2013-03-04 00:53:17 +01:00
_.each(array, function (btnName) {
btnGroup.addButton(Button.load(btnName));
2013-03-04 00:53:17 +01:00
});
return btnGroup;
};
ButtonsGroup.prototype.addButton = function (button) {
this.buttons.push(button);
return this;
};
ButtonsGroup.prototype.render = function () {
if (this.buttons.length == 1) {
this.buttons[0].grouping = "";
}
else {
_.first(this.buttons).grouping = "grouped-left";
_.last(this.buttons).grouping = "grouped-right";
_.each(this.buttons.slice(1, -1), function (btn) {
btn.grouping = "grouped-middle"
});
}
return _.map(this.buttons, function (btn) {
return btn.render();
}).join("\n");
};
Button = function (attributes) {
this.attributes = attributes;
};
2013-03-04 00:53:17 +01:00
Button.load = function (btnName) {
2013-03-13 03:36:04 +01:00
var button = module.exports.availableButtons[btnName];
if (button.constructor === Button || button.constructor === SelectButton) {
return button;
}
else {
return new Button(button);
}
2013-03-04 00:53:17 +01:00
};
2013-03-09 23:57:42 +01:00
_.extend(Button.prototype, {
grouping: "",
render: function () {
var liAttributes = {
"data-type": "button",
"data-key": this.attributes.command,
};
return tag("li", liAttributes,
tag("a", { "class": this.grouping, "data-l10n-id": this.attributes.localizationId },
tag("span", { "class": " "+ this.attributes.class })
2013-03-09 23:57:42 +01:00
)
);
}
});
SelectButton = function (attributes) {
this.attributes = attributes;
this.options = [];
};
_.extend(SelectButton.prototype, Button.prototype, {
addOption: function (value, text, attributes) {
this.options.push({
value: value,
text: text,
attributes: attributes
});
return this;
},
select: function (attributes) {
var self = this
, options = [];
_.each(this.options, function (opt) {
var a = _.extend({
value: opt.value
}, opt.attributes);
options.push( tag("option", a, opt.text) );
});
return tag("select", attributes, options.join(""));
},
render: function () {
var attributes = {
id: this.attributes.id,
"data-key": this.attributes.command,
"data-type": "select"
};
return tag("li", attributes,
this.select({ id: this.attributes.selectId })
);
}
});
2013-03-04 00:53:17 +01:00
Separator = function () {};
Separator.prototype.render = function () {
return tag("li", { "class": "separator" });
2013-03-04 00:53:17 +01:00
};
module.exports = {
2013-03-13 03:36:04 +01:00
availableButtons: {
bold: defaultButtonAttributes("bold"),
italic: defaultButtonAttributes("italic"),
underline: defaultButtonAttributes("underline"),
strikethrough: defaultButtonAttributes("strikethrough"),
orderedlist: {
command: "insertorderedlist",
2013-03-13 03:36:04 +01:00
localizationId: "pad.toolbar.ol.title",
class: "buttonicon buttonicon-insertorderedlist"
2013-03-13 03:36:04 +01:00
},
unorderedlist: {
command: "insertunorderedlist",
2013-03-13 03:36:04 +01:00
localizationId: "pad.toolbar.ul.title",
class: "buttonicon buttonicon-insertunorderedlist"
2013-03-13 03:36:04 +01:00
},
indent: defaultButtonAttributes("indent"),
outdent: {
command: "outdent",
2013-03-13 03:36:04 +01:00
localizationId: "pad.toolbar.unindent.title",
class: "buttonicon buttonicon-outdent"
2013-03-13 03:36:04 +01:00
},
undo: defaultButtonAttributes("undo"),
redo: defaultButtonAttributes("redo"),
clearauthorship: {
command: "clearauthorship",
2013-03-13 03:36:04 +01:00
localizationId: "pad.toolbar.clearAuthorship.title",
class: "buttonicon buttonicon-clearauthorship"
2013-03-13 03:36:04 +01:00
},
importexport: {
command: "import_export",
2013-03-13 03:36:04 +01:00
localizationId: "pad.toolbar.import_export.title",
class: "buttonicon buttonicon-import_export"
2013-03-13 03:36:04 +01:00
},
timeslider: {
command: "showTimeSlider",
2013-03-13 03:36:04 +01:00
localizationId: "pad.toolbar.timeslider.title",
class: "buttonicon buttonicon-history"
2013-03-13 03:36:04 +01:00
},
savedrevision: defaultButtonAttributes("savedRevision"),
settings: defaultButtonAttributes("settings"),
embed: defaultButtonAttributes("embed"),
showusers: defaultButtonAttributes("showusers"),
timeslider_export: {
command: "import_export",
localizationId: "timeslider.toolbar.exportlink.title",
class: "buttonicon buttonicon-import_export"
},
timeslider_returnToPad: {
command: "timeslider_returnToPad",
localizationId: "timeslider.toolbar.returnbutton",
class: "buttontext"
}
2013-03-13 03:36:04 +01:00
},
registerButton: function (buttonName, buttonInfo) {
this.availableButtons[buttonName] = buttonInfo;
},
button: function (attributes) {
return new Button(attributes);
},
2013-03-09 23:57:42 +01:00
separator: function () {
return (new Separator).render();
},
selectButton: function (attributes) {
return new SelectButton(attributes);
},
2013-03-04 00:53:17 +01:00
menu: function (buttons) {
var groups = _.map(buttons, function (group) {
return ButtonsGroup.fromArray(group).render();
});
2013-03-09 23:57:42 +01:00
return groups.join(this.separator());
2013-03-04 00:53:17 +01:00
}
};