ribbon.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. ArozOS Office - the ribbon (common/ribbon.js)
  3. =============================================
  4. OfficeRibbon turns the app's #toolbar into an Office-style ribbon: tabs
  5. in the title bar (built by office.js), and under them one panel per tab
  6. made of groups of controls - two rows of small buttons, a big button
  7. with its label underneath, a stack of labelled buttons, or a gallery.
  8. var R = OfficeRibbon;
  9. var home = R.tab("home", "Home");
  10. var g = home.group({ id: "font", label: "Font", icon: "font" });
  11. g.row([$fontSelect, R.button({ icon: "bold", title: "Bold", key: "Ctrl+B",
  12. cmd: "bold", onClick: fn })]);
  13. g.row([...]);
  14. home.group({ id: "clip", label: "Clipboard", icon: "paste" })
  15. .add(R.big({ icon: "paste", label: "Paste", onClick: fn, menu: fn }))
  16. .stack([R.button({ icon: "cut", label: "Cut", showLabel: true, onClick: fn }), ...]);
  17. Three widths, one DOM:
  18. - wide: every group shows its controls.
  19. - narrower than the panel: galleries give up tiles first, then whole
  20. groups fold into a button of their own (lowest priority first, then
  21. from the right) that opens the group's controls in a popup below it -
  22. the controls are moved there, not copied, so ids and handlers stay.
  23. - phone (body.of-narrow): the panel is one row that scrolls sideways,
  24. big buttons shrink to icons, galleries and anything marked
  25. mobile:false are left out, and the tab strip becomes one picker.
  26. State: a control may carry active(), enabled() and visible() functions
  27. and a tab may carry when(); refresh() re-reads them all (office.js calls
  28. it from OfficeApp.updateMenus(), and apps call it on selection change).
  29. Apps can still toggle .active on their own ids as before.
  30. Menus opened from the ribbon are ordinary OfficeApp.showContextMenu
  31. item lists (see CONTRACT.md for the item shape).
  32. */
  33. var OfficeRibbon = (function () {
  34. "use strict";
  35. var tabs = []; // [{id,title,when,contextual,$btn,$panel,groups}]
  36. var activeId = null;
  37. var $host = null; // #toolbar
  38. var $strip = null; // tab buttons, in the title bar
  39. var $picker = null; // the phone-width tab picker button
  40. var tracked = []; // controls with active/enabled/visible fns
  41. var galleries = [];
  42. var popup = null; // { group, $pop } while a folded group is open
  43. var narrow = false;
  44. var NARROW_PX = 600;
  45. var fitTimer = null;
  46. function esc(t) {
  47. if (t === undefined || t === null) return "";
  48. return String(t).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
  49. .replace(/"/g, "&quot;");
  50. }
  51. function call(fn, dflt) {
  52. if (typeof fn !== "function") return dflt;
  53. try { return fn(); } catch (e) { return dflt; }
  54. }
  55. function evalItems(m) { return (typeof m === "function") ? m() : (m || []); }
  56. function keepFocus(e) { e.preventDefault(); }
  57. function rectOf(el) { return el.getBoundingClientRect(); }
  58. /* ================= host ================= */
  59. function host() {
  60. if (!$host) {
  61. $host = $("#toolbar");
  62. if (!$host.length) $host = $('<div class="of-toolbar of-noprint" id="toolbar"></div>').prependTo("body");
  63. $host.addClass("of-ribbon").empty();
  64. watchWidth();
  65. }
  66. return $host;
  67. }
  68. function hasTabs() { return tabs.length > 0; }
  69. /* ================= tabs ================= */
  70. function findTab(id) {
  71. for (var i = 0; i < tabs.length; i++) if (tabs[i].id === id) return tabs[i];
  72. return null;
  73. }
  74. function tab(id, title, opts) {
  75. var t = findTab(id);
  76. if (t) return t.api;
  77. opts = opts || {};
  78. t = {
  79. id: id, title: title, when: opts.when || null,
  80. contextual: !!opts.contextual, groups: [], $btn: null,
  81. $panel: $('<div class="of-rb-panel"></div>').attr("data-tab", id)
  82. };
  83. host().append(t.$panel);
  84. t.api = {
  85. id: id,
  86. group: function (gopts) { return group(t, gopts || {}); },
  87. select: function () { select(id); }
  88. };
  89. tabs.push(t);
  90. if ($strip) addTabButton(t);
  91. if (!activeId && !t.when) select(id);
  92. else t.$panel.hide();
  93. return t.api;
  94. }
  95. function tabVisible(t) { return !t.when || !!call(t.when, false); }
  96. function addTabButton(t) {
  97. var $b = $('<button type="button" class="of-rb-tab"></button>').text(t.title)
  98. .attr("data-tab", t.id);
  99. if (t.contextual) $b.addClass("of-rb-ctx");
  100. $b.on("mousedown", keepFocus);
  101. $b.on("click", function () {
  102. if (window.OfficeApp) OfficeApp.closeAllMenus();
  103. if ($("body").hasClass("of-rb-min")) {
  104. // a minimized ribbon shows the tab over the document until
  105. // the next click elsewhere
  106. select(t.id);
  107. peek(true);
  108. return;
  109. }
  110. select(t.id);
  111. });
  112. $b.on("dblclick", function () { setMinimized(!$("body").hasClass("of-rb-min")); });
  113. if (t.when && !tabVisible(t)) $b.hide();
  114. if (t.id === activeId) $b.addClass("active");
  115. t.$btn = $b;
  116. if ($picker) $b.insertBefore($picker); else $strip.append($b);
  117. // one more tab: the title bar may have to close up
  118. scheduleFit();
  119. }
  120. /* office.js hands over the title bar's tab container once it exists */
  121. function attachStrip($el) {
  122. $strip = $el;
  123. $picker = $('<button type="button" class="of-rb-tabpick" title="Ribbon tab"></button>');
  124. $picker.on("mousedown", keepFocus);
  125. $picker.on("click", function () {
  126. if (window.OfficeApp) OfficeApp.closeAllMenus();
  127. var r = rectOf($picker[0]);
  128. OfficeApp.showContextMenu(r.left, r.bottom + 2, tabs.filter(tabVisible).map(function (t) {
  129. return {
  130. label: t.title,
  131. checked: t.id === activeId,
  132. action: function () { select(t.id); }
  133. };
  134. }));
  135. });
  136. $strip.append($picker);
  137. tabs.forEach(addTabButton);
  138. updatePicker();
  139. }
  140. function updatePicker() {
  141. var t = findTab(activeId);
  142. if ($picker) $picker.html(esc(t ? t.title : "Menu") + ' <i class="caret down icon"></i>');
  143. }
  144. function select(id) {
  145. var t = findTab(id);
  146. if (!t) return;
  147. closePopup();
  148. activeId = id;
  149. tabs.forEach(function (x) {
  150. x.$panel.toggle(x.id === id);
  151. if (x.$btn) x.$btn.toggleClass("active", x.id === id);
  152. });
  153. updatePicker();
  154. refresh();
  155. fit();
  156. }
  157. function activeTab() { return activeId; }
  158. /* ================= minimized ribbon ================= */
  159. function setMinimized(on) {
  160. peek(false);
  161. $("body").toggleClass("of-rb-min", !!on);
  162. try { OfficeApp.setSetting("ribbonMin", !!on); } catch (e) { }
  163. $(".of-rb-mintoggle i.icon").attr("class", (on ? "chevron down" : "chevron up") + " icon");
  164. $(".of-rb-mintoggle").attr("title", on ? "Show the ribbon" : "Collapse the ribbon");
  165. // the editing area just changed height: let the app lay out again
  166. try { window.dispatchEvent(new Event("resize")); } catch (e) { }
  167. if (!on) fit();
  168. }
  169. function isMinimized() { return $("body").hasClass("of-rb-min"); }
  170. function peek(on) {
  171. $("body").toggleClass("of-rb-peek", !!on);
  172. $(document).off("mousedown.ofrbpeek");
  173. if (!on) return;
  174. fit();
  175. setTimeout(function () {
  176. $(document).on("mousedown.ofrbpeek", function (e) {
  177. var t = e.target;
  178. if ($(t).closest(".of-ribbon, .of-rb-tab, .of-context-menu, .of-cp-panel, .of-rb-popup, .of-dialog-overlay").length) return;
  179. peek(false);
  180. });
  181. }, 0);
  182. }
  183. /* ================= groups ================= */
  184. function group(t, o) {
  185. var g = {
  186. id: o.id || ("g" + t.groups.length), label: o.label || "", icon: o.icon || "",
  187. priority: o.priority || 0, order: t.groups.length,
  188. collapsible: o.collapse !== false, $col: null
  189. };
  190. g.$el = $('<div class="of-rb-group"></div>').attr("data-group", g.id);
  191. if (o.mobile === false) g.$el.attr("data-mobile", "hide");
  192. g.$body = $('<div class="of-rb-gbody"></div>');
  193. var gicon = (o.svg && window.OfficeIcons && OfficeIcons.has(o.svg)) ? OfficeIcons.get(o.svg) :
  194. (g.icon ? '<i class="' + esc(g.icon) + ' icon"></i>' : "");
  195. g.$gbtn = $('<button type="button" class="of-rb-gbtn"></button>')
  196. .attr("title", g.label)
  197. .append(gicon)
  198. .append('<span class="of-rb-gbtnlbl">' + esc(g.label) + ' <i class="caret down icon"></i></span>');
  199. g.$gbtn.on("mousedown", keepFocus);
  200. g.$gbtn.on("click", function () {
  201. if (popup && popup.group === g) closePopup(); else openPopup(g);
  202. });
  203. g.$el.append(g.$body).append(g.$gbtn);
  204. t.$panel.append(g.$el);
  205. t.groups.push(g);
  206. var api = {
  207. $el: g.$el,
  208. // one line of small controls; consecutive rows stack in a column
  209. row: function (items) {
  210. if (!g.$col) {
  211. g.$col = $('<div class="of-rb-rows"></div>');
  212. g.$body.append(g.$col);
  213. }
  214. var $r = $('<div class="of-rb-row"></div>');
  215. appendItems($r, items);
  216. g.$col.append($r);
  217. return api;
  218. },
  219. // up to three labelled small buttons, one under the other
  220. stack: function (items) {
  221. g.$col = null;
  222. var $s = $('<div class="of-rb-stack"></div>');
  223. appendItems($s, items);
  224. g.$body.append($s);
  225. return api;
  226. },
  227. // anything else, as a column of its own (a big button, a gallery)
  228. add: function (el) {
  229. g.$col = null;
  230. appendItems(g.$body, [el]);
  231. return api;
  232. },
  233. // the dialog launcher in the group's bottom-right corner
  234. // (o: { title, onClick }); it travels with the controls into a
  235. // folded group's popup
  236. launcher: function (lo) {
  237. var $l = $('<button type="button" class="of-rb-launch"></button>')
  238. .attr("title", lo.title || g.label)
  239. .append(window.OfficeIcons ? OfficeIcons.get("launcher") : "");
  240. $l.on("mousedown", keepFocus);
  241. $l.on("click", function (e) {
  242. closePopup();
  243. if (lo.onClick) lo.onClick.call(this, e);
  244. });
  245. g.$el.addClass("of-rb-haslaunch");
  246. g.$body.append($l);
  247. return api;
  248. }
  249. };
  250. return api;
  251. }
  252. function appendItems($to, items) {
  253. (items || []).forEach(function (it) {
  254. if (!it) return;
  255. if (it === "|") { $to.append('<span class="of-rb-vsep"></span>'); return; }
  256. $to.append(it);
  257. });
  258. }
  259. /* ---------- folded groups ---------- */
  260. function openPopup(g) {
  261. closePopup();
  262. var $pop = $('<div class="of-rb-popup of-noprint"></div>');
  263. $pop.append(g.$body);
  264. $("body").append($pop);
  265. var r = rectOf(g.$gbtn[0]);
  266. var w = $pop.outerWidth(), h = $pop.outerHeight();
  267. var x = Math.max(4, Math.min(r.left, window.innerWidth - w - 4));
  268. var y = r.bottom + 2;
  269. if (y + h > window.innerHeight - 4) y = Math.max(4, window.innerHeight - h - 4);
  270. $pop.css({ left: x + "px", top: y + "px" });
  271. g.$gbtn.addClass("open");
  272. popup = { group: g, $pop: $pop };
  273. refresh();
  274. setTimeout(function () {
  275. $(document).on("mousedown.ofrbpop", function (e) {
  276. if (!popup) return;
  277. if (popup.$pop[0].contains(e.target) || popup.group.$gbtn[0].contains(e.target)) return;
  278. // a menu, colour picker or dialog opened from inside the popup
  279. // belongs to it
  280. if ($(e.target).closest(".of-context-menu, .of-cp-panel, .of-dialog-overlay").length) return;
  281. closePopup();
  282. });
  283. }, 0);
  284. }
  285. function closePopup() {
  286. $(document).off("mousedown.ofrbpop");
  287. if (!popup) return;
  288. var g = popup.group;
  289. g.$el.prepend(g.$body);
  290. g.$gbtn.removeClass("open");
  291. popup.$pop.remove();
  292. popup = null;
  293. }
  294. // a command inside a folded group's popup closes it, as in Office
  295. function afterCommand(el, keep) {
  296. if (keep || !popup) return;
  297. if (popup.$pop[0].contains(el)) closePopup();
  298. }
  299. /* ================= fitting ================= */
  300. function watchWidth() {
  301. var apply = function () {
  302. var n = window.innerWidth <= NARROW_PX;
  303. if (n !== narrow) {
  304. narrow = n;
  305. $("body").toggleClass("of-narrow", narrow);
  306. closePopup();
  307. }
  308. scheduleFit();
  309. };
  310. $(window).on("resize.ofribbon", apply);
  311. narrow = window.innerWidth <= NARROW_PX;
  312. $("body").toggleClass("of-narrow", narrow);
  313. }
  314. function isNarrow() { return narrow; }
  315. function scheduleFit() {
  316. clearTimeout(fitTimer);
  317. fitTimer = setTimeout(fit, 30);
  318. }
  319. /* The title bar must show every tab: when they do not fit beside the
  320. quick access buttons, the tabs first close up and the AutoSave label
  321. goes, then the strip turns into the one-button tab picker a phone
  322. uses. Overflow is measured on the last item of the bar, which is
  323. pushed past its right edge (the bar cannot clip: the File menu drops
  324. out of it). */
  325. function fitStrip() {
  326. if (!$strip) return;
  327. var $body = $("body");
  328. $body.removeClass("of-tb-tight of-tb-pick");
  329. if (narrow) return;
  330. var bar = $strip.closest(".of-titlebar")[0];
  331. if (!bar || !bar.offsetParent) return;
  332. var over = function () {
  333. var kids = $(bar).children().filter(function () { return this.offsetWidth > 0; });
  334. if (!kids.length) return false;
  335. var right = bar.getBoundingClientRect().right - 2;
  336. return kids[kids.length - 1].getBoundingClientRect().right > right;
  337. };
  338. if (!over()) return;
  339. $body.addClass("of-tb-tight");
  340. if (!over()) return;
  341. $body.addClass("of-tb-pick");
  342. }
  343. /* Make the active panel fit: galleries first give up tiles, then whole
  344. groups fold into buttons - lowest priority first, the rightmost of
  345. equals first. A phone-width panel scrolls instead. */
  346. function fit() {
  347. fitStrip();
  348. var t = findTab(activeId);
  349. if (!t || !$host) return;
  350. closePopup();
  351. var panel = t.$panel[0];
  352. t.groups.forEach(function (g) { g.$el.removeClass("of-rb-collapsed"); });
  353. galleries.forEach(function (gl) {
  354. if (t.$panel[0].contains(gl.$el[0])) setGalleryCount(gl, gl.max);
  355. });
  356. if (narrow || !panel.offsetParent) return;
  357. var avail = function () { return $host[0].clientWidth - 2; };
  358. var over = function () { return panel.scrollWidth > avail(); };
  359. if (!over()) return;
  360. var gls = galleries.filter(function (gl) { return panel.contains(gl.$el[0]); });
  361. var shrunk = true;
  362. while (over() && shrunk) {
  363. shrunk = false;
  364. for (var i = 0; i < gls.length && over(); i++) {
  365. if (gls[i].count > gls[i].min) {
  366. setGalleryCount(gls[i], gls[i].count - 1);
  367. shrunk = true;
  368. }
  369. }
  370. }
  371. if (!over()) return;
  372. var order = t.groups.filter(function (g) {
  373. return g.collapsible && g.$el.is(":visible");
  374. }).sort(function (a, b) {
  375. if (a.priority !== b.priority) return a.priority - b.priority;
  376. return b.order - a.order;
  377. });
  378. for (var k = 0; k < order.length && over(); k++) {
  379. order[k].$el.addClass("of-rb-collapsed");
  380. }
  381. }
  382. /* ================= state ================= */
  383. function track($el, o) {
  384. if (o.active || o.enabled || o.visible) tracked.push({ $el: $el, o: o });
  385. }
  386. function refresh() {
  387. var relayout = false;
  388. tabs.forEach(function (t) {
  389. if (!t.when || !t.$btn) return;
  390. var on = tabVisible(t);
  391. if (on !== (t.$btn.css("display") !== "none")) {
  392. t.$btn.toggle(on);
  393. relayout = true;
  394. }
  395. if (!on && t.id === activeId) {
  396. var first = null;
  397. tabs.forEach(function (x) { if (!first && tabVisible(x)) first = x; });
  398. if (first) select(first.id);
  399. }
  400. });
  401. tracked.forEach(function (c) {
  402. var o = c.o;
  403. if (o.visible) {
  404. var v = !!call(o.visible, true);
  405. if (v !== (c.$el.css("display") !== "none")) { c.$el.toggle(v); relayout = true; }
  406. }
  407. if (o.active) c.$el.toggleClass("active", !!call(o.active, false));
  408. if (o.enabled) {
  409. var en = !!call(o.enabled, true);
  410. var $btns = c.$el.is("button") ? c.$el : c.$el.find("button");
  411. $btns.prop("disabled", !en);
  412. c.$el.toggleClass("of-rb-disabled", !en);
  413. }
  414. });
  415. galleries.forEach(function (gl) {
  416. gl.tiles.forEach(function (tile) {
  417. if (tile.it.active) tile.$el.toggleClass("active", !!call(tile.it.active, false));
  418. });
  419. });
  420. if (relayout) scheduleFit();
  421. }
  422. /* ================= controls ================= */
  423. function iconHtml(o) {
  424. if (o.iconHtml) return o.iconHtml;
  425. // o.svg: one of the drawn icons in common/icons.js
  426. if (o.svg && window.OfficeIcons && OfficeIcons.has(o.svg)) return OfficeIcons.get(o.svg);
  427. if (o.icon) return '<i class="' + esc(o.icon) + ' icon"></i>';
  428. if (o.text) return '<span class="of-rb-txt">' + esc(o.text) + "</span>";
  429. return "";
  430. }
  431. function tip(o) {
  432. var t = o.title || o.label || "";
  433. if (o.key) t += " (" + o.key + ")";
  434. return t;
  435. }
  436. function common($b, o) {
  437. if (o.id) $b.attr("id", o.id);
  438. if (o.cmd) $b.attr("data-cmd", o.cmd);
  439. if (o.cls) $b.addClass(o.cls);
  440. if (o.mobile === false) $b.attr("data-mobile", "hide");
  441. if (o.desktop === false) $b.attr("data-desktop", "hide");
  442. $b.attr("title", tip(o));
  443. track($b, o);
  444. }
  445. /* A small button: an icon, or an icon and its label (showLabel). */
  446. function button(o) {
  447. var $b = $('<button type="button" class="of-tbtn of-rb-btn"></button>');
  448. $b.append(iconHtml(o));
  449. if (o.label && o.showLabel) {
  450. $b.addClass("of-rb-withlabel").append('<span class="of-rb-lbl">' + esc(o.label) + "</span>");
  451. }
  452. common($b, o);
  453. if (o.keepFocus !== false) $b.on("mousedown", keepFocus);
  454. $b.on("click", function (e) {
  455. if (o.onClick) o.onClick.call(this, e);
  456. afterCommand(this, o.keepOpen);
  457. });
  458. return $b;
  459. }
  460. function openMenuAt(el, o) {
  461. var r = rectOf(el);
  462. if (o.onOpen) { o.onOpen(el, r); return; }
  463. OfficeApp.showContextMenu(r.left, r.bottom + 2, evalItems(o.menu));
  464. }
  465. /* A small button that opens a menu (or o.onOpen(anchor, rect)). */
  466. function dropdown(o) {
  467. var $b = $('<button type="button" class="of-tbtn of-rb-btn of-rb-dd"></button>');
  468. $b.append(iconHtml(o));
  469. if (o.label && o.showLabel) {
  470. $b.addClass("of-rb-withlabel").append('<span class="of-rb-lbl">' + esc(o.label) + "</span>");
  471. }
  472. $b.append('<i class="caret down icon of-rb-caret"></i>');
  473. common($b, o);
  474. if (o.keepFocus !== false) $b.on("mousedown", keepFocus);
  475. $b.on("click", function () { openMenuAt(this, o); });
  476. return $b;
  477. }
  478. /* A small split button: the face runs onClick, the caret opens a menu. */
  479. function split(o) {
  480. var $w = $('<span class="of-rb-split"></span>');
  481. var face = {};
  482. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) face[k] = o[k];
  483. delete face.menu;
  484. var $main = button(face);
  485. var $car = $('<button type="button" class="of-tbtn of-rb-btn of-rb-caretbtn"><i class="caret down icon"></i></button>')
  486. .attr("title", o.menuTitle || tip(o));
  487. $car.on("mousedown", keepFocus);
  488. $car.on("click", function () { openMenuAt($w[0], o); });
  489. if (o.mobile === false) $w.attr("data-mobile", "hide");
  490. $w.append($main).append($car);
  491. return $w;
  492. }
  493. /* A big button: icon over label. With a menu (or onOpen) and no
  494. onClick the whole button opens the menu. With both it is two
  495. buttons, as in Office: the icon runs the command, the label under it
  496. (with the caret) opens the menu - two targets, never one button
  497. whose halves behave differently. */
  498. function big(o) {
  499. if (o.onClick && (o.menu || o.onOpen)) return bigSplit(o);
  500. var $b = $('<button type="button" class="of-rb-big"></button>');
  501. $b.append('<span class="of-rb-bigicon">' + iconHtml(o) + "</span>");
  502. var $lbl = $('<span class="of-rb-biglbl"></span>').text(o.label || "");
  503. if (o.menu || o.onOpen) $lbl.append(' <i class="caret down icon"></i>');
  504. $b.append($lbl);
  505. common($b, o);
  506. if (o.keepFocus !== false) $b.on("mousedown", keepFocus);
  507. $b.on("click", function (e) {
  508. var hasMenu = !!(o.menu || o.onOpen);
  509. if (hasMenu && (!o.onClick || $(e.target).closest(".of-rb-biglbl").length)) {
  510. openMenuAt(this, o);
  511. return;
  512. }
  513. if (o.onClick) o.onClick.call(this, e);
  514. afterCommand(this, o.keepOpen);
  515. });
  516. return $b;
  517. }
  518. function bigSplit(o) {
  519. var $w = $('<span class="of-rb-bigsplit"></span>');
  520. var $top = $('<button type="button" class="of-rb-bigtop"></button>')
  521. .attr("title", tip(o))
  522. .append('<span class="of-rb-bigicon">' + iconHtml(o) + "</span>");
  523. var $bot = $('<button type="button" class="of-rb-bigbot"></button>')
  524. .attr("title", o.menuTitle || ((o.label || "") + " - more options"))
  525. .append($('<span class="of-rb-biglbl"></span>').text(o.label || ""))
  526. .append('<i class="caret down icon"></i>');
  527. // the caret on a line of its own under the label (Office's Paste)
  528. if (o.caretBelow) $w.addClass("of-rb-caretbelow");
  529. if (o.id) $top.attr("id", o.id);
  530. if (o.cmd) $top.attr("data-cmd", o.cmd);
  531. if (o.cls) $w.addClass(o.cls);
  532. if (o.mobile === false) $w.attr("data-mobile", "hide");
  533. if (o.desktop === false) $w.attr("data-desktop", "hide");
  534. track($w, o);
  535. if (o.keepFocus !== false) {
  536. $top.on("mousedown", keepFocus);
  537. $bot.on("mousedown", keepFocus);
  538. }
  539. $top.on("click", function (e) {
  540. o.onClick.call(this, e);
  541. afterCommand(this, o.keepOpen);
  542. });
  543. $bot.on("click", function () { openMenuAt($w[0], o); });
  544. $w.append($top).append($bot);
  545. return $w;
  546. }
  547. /* An on/off switch with its label (AutoSave). */
  548. function toggle(o) {
  549. var $b = $('<button type="button" class="of-rb-switch" role="switch"></button>');
  550. $b.append('<span class="of-rb-sw"><span class="of-rb-knob"></span></span>');
  551. if (o.label) $b.append('<span class="of-rb-swlbl">' + esc(o.label) + "</span>");
  552. var sync = function () {
  553. var on = !!call(o.get, false);
  554. $b.toggleClass("on", on).attr("aria-checked", on ? "true" : "false");
  555. };
  556. common($b, { id: o.id, title: o.title || o.label, cls: o.cls, mobile: o.mobile });
  557. $b.on("mousedown", keepFocus);
  558. $b.on("click", function () { o.set(!call(o.get, false)); sync(); });
  559. sync();
  560. $b.data("sync", sync);
  561. tracked.push({ $el: $b, o: { active: o.get } });
  562. return $b;
  563. }
  564. /* A colour control as Office has it: the face applies the current
  565. colour (the bar under the icon), the caret picks another one. */
  566. /* big: true draws it as a big split button (icon and colour bar over a
  567. label with the caret), for a colour that stands in a group of big
  568. buttons rather than in a row of small ones */
  569. function colorButton(o) {
  570. var $w, $main, $car;
  571. var cur = o.value || "";
  572. if (o.big) {
  573. $w = $('<span class="of-rb-bigsplit of-rb-color of-rb-bigcolor"></span>');
  574. $main = $('<button type="button" class="of-rb-bigtop of-rb-cface"></button>')
  575. .attr("title", tip(o))
  576. .append('<span class="of-rb-bigicon">' + iconHtml(o) + "</span>")
  577. .append('<span class="of-rb-cbar"></span>');
  578. $car = $('<button type="button" class="of-rb-bigbot"></button>')
  579. .attr("title", (o.title || "Colour") + " - more colours")
  580. .append($('<span class="of-rb-biglbl"></span>').text(o.label || o.title || ""))
  581. .append('<i class="caret down icon"></i>');
  582. } else {
  583. $w = $('<span class="of-rb-split of-rb-color"></span>');
  584. $main = $('<button type="button" class="of-tbtn of-rb-btn of-rb-cface"></button>')
  585. .attr("title", tip(o))
  586. .append(iconHtml(o))
  587. .append('<span class="of-rb-cbar"></span>');
  588. $car = $('<button type="button" class="of-tbtn of-rb-btn of-rb-caretbtn"><i class="caret down icon"></i></button>')
  589. .attr("title", (o.title || "Colour") + " - more colours");
  590. }
  591. if (o.id) $w.attr("id", o.id);
  592. var paint = function () {
  593. $main.find(".of-rb-cbar").css("background", cur || "transparent")
  594. .toggleClass("none", !cur);
  595. };
  596. var pick = function () {
  597. OfficeColorPicker.open({
  598. anchor: $w[0], value: cur || o.value || "#000000",
  599. allowNone: !!o.allowNone, noneLabel: o.noneLabel,
  600. onPick: function (hex) {
  601. cur = hex || "";
  602. paint();
  603. if (o.onPick) o.onPick(cur);
  604. }
  605. });
  606. };
  607. $main.on("mousedown", keepFocus);
  608. $car.on("mousedown", keepFocus);
  609. $main.on("click", function () {
  610. // nothing chosen yet (or "none" for a colour that must be one):
  611. // the first press picks
  612. if (!cur && !o.allowNone) { pick(); return; }
  613. if (o.onPick) o.onPick(cur);
  614. afterCommand(this, false);
  615. });
  616. $car.on("click", pick);
  617. if (o.mobile === false) $w.attr("data-mobile", "hide");
  618. $w.append($main).append($car);
  619. $w.data("ofColor", {
  620. get: function () { return cur; },
  621. set: function (hex) { cur = hex || ""; paint(); }
  622. });
  623. paint();
  624. return $w;
  625. }
  626. function colorOf(sel) { var c = $(sel).data("ofColor"); return c ? c.get() : ""; }
  627. function setColor(sel, hex) { var c = $(sel).data("ofColor"); if (c) c.set(hex); }
  628. /* A gallery: a row of preview tiles (styles, themes, transitions) that
  629. gives up tiles when room is short, plus a button listing them all.
  630. items: [{ key, label, html | build($tile), active(), onClick() }] */
  631. function gallery(o) {
  632. var gl = {
  633. $el: $('<div class="of-rb-gallery"></div>'), tiles: [],
  634. max: 0, min: o.minVisible || 2, count: 0, tileW: o.tileW || 76
  635. };
  636. if (o.id) gl.$el.attr("id", o.id);
  637. if (o.cls) gl.$el.addClass(o.cls);
  638. gl.$el.attr("data-mobile", o.mobile === true ? "show" : "hide");
  639. var $items = $('<div class="of-rb-gitems"></div>');
  640. var makeTile = function (it) {
  641. var $t = $('<button type="button" class="of-rb-gtile"></button>').attr("title", it.title || it.label || "");
  642. if (it.build) it.build($t); else $t.html(it.html || esc(it.label));
  643. $t.on("mousedown", keepFocus);
  644. $t.on("click", function () {
  645. if (it.onClick) it.onClick();
  646. closeGalleryPop();
  647. afterCommand(this, false);
  648. refresh();
  649. });
  650. return $t;
  651. };
  652. var items = evalItems(o.items);
  653. items.forEach(function (it) {
  654. var $t = makeTile(it).css("width", gl.tileW + "px");
  655. gl.tiles.push({ it: it, $el: $t });
  656. $items.append($t);
  657. });
  658. gl.max = Math.min(items.length, o.visible || items.length);
  659. var $more = $('<button type="button" class="of-rb-gmore" title="' + esc(o.moreTitle || "More") + '"><i class="caret down icon"></i></button>');
  660. $more.on("mousedown", keepFocus);
  661. $more.on("click", function () {
  662. if ($galleryPop) { closeGalleryPop(); return; }
  663. var $pop = $('<div class="of-rb-gpop of-noprint"></div>');
  664. if (o.cls) $pop.addClass(o.cls);
  665. items.forEach(function (it) {
  666. var $t = makeTile(it).css("width", gl.tileW + "px");
  667. if (it.active && call(it.active, false)) $t.addClass("active");
  668. $pop.append($t);
  669. });
  670. if (o.extra) {
  671. evalItems(o.extra).forEach(function (x) {
  672. var $x = $('<button type="button" class="of-rb-gextra"></button>')
  673. .html((x.icon ? '<i class="' + esc(x.icon) + ' icon"></i>' : "") + esc(x.label));
  674. $x.on("click", function () { closeGalleryPop(); x.action(); });
  675. $pop.append($x);
  676. });
  677. }
  678. $("body").append($pop);
  679. var r = rectOf(gl.$el[0]);
  680. var w = $pop.outerWidth(), h = $pop.outerHeight();
  681. $pop.css({
  682. left: Math.max(4, Math.min(r.left, window.innerWidth - w - 4)) + "px",
  683. top: Math.max(4, Math.min(r.top, window.innerHeight - h - 4)) + "px"
  684. });
  685. $galleryPop = $pop;
  686. setTimeout(function () {
  687. $(document).on("mousedown.ofrbgal", function (e) {
  688. if ($galleryPop && !$galleryPop[0].contains(e.target)) closeGalleryPop();
  689. });
  690. }, 0);
  691. });
  692. gl.$el.append($items).append($more);
  693. galleries.push(gl);
  694. setGalleryCount(gl, gl.max);
  695. return gl.$el;
  696. }
  697. var $galleryPop = null;
  698. function closeGalleryPop() {
  699. $(document).off("mousedown.ofrbgal");
  700. if ($galleryPop) { $galleryPop.remove(); $galleryPop = null; }
  701. }
  702. function setGalleryCount(gl, n) {
  703. gl.count = Math.max(gl.min, Math.min(gl.max, n));
  704. gl.tiles.forEach(function (t, i) { t.$el.toggle(i < gl.count); });
  705. }
  706. /* ================= the Clipboard group =================
  707. Every Home tab starts with it, laid out as Office has it: a big Paste
  708. (the icon pastes, the label under it opens the paste options), then
  709. Cut, Copy (its caret lists other ways to copy) and Format Painter one
  710. under the other, and the dialog launcher in the corner opening the
  711. Clipboard pane (OfficeApp.showClipboardPane).
  712. R.clipboardGroup(home, {
  713. pasteMenu: fn -> items, // the paste options (Paste first)
  714. copyMenu: fn -> items, // the ways to copy (Copy first)
  715. painter: { id, title, active(), start(sticky) }
  716. });
  717. Cut, Copy and Paste run the app's Edit menu commands
  718. (OfficeApp.clipboard), so the ribbon and the menu always agree. */
  719. function clipboardGroup(t, o) {
  720. o = o || {};
  721. var run = function (op) { return function () { OfficeApp.clipboard(op); }; };
  722. var pane = function () { OfficeApp.showClipboardPane(); };
  723. var withPane = function (m) {
  724. return function () {
  725. var items = evalItems(m).slice();
  726. items.push({ sep: true });
  727. items.push({ label: "Clipboard...", icon: "clipboard outline", action: pane });
  728. return items;
  729. };
  730. };
  731. var g = t.group({ id: "clipboard", label: "Clipboard", svg: "pasteBig", priority: 5 });
  732. g.add(big({
  733. svg: "pasteBig", label: "Paste", title: "Paste", key: "Ctrl+V", cls: "of-rb-paste",
  734. caretBelow: true, menuTitle: "Paste options",
  735. onClick: run("paste"),
  736. menu: withPane(o.pasteMenu || [])
  737. }));
  738. var p = o.painter || {};
  739. var $painter = button({
  740. svg: "formatPainter", id: p.id, active: p.active,
  741. title: p.title || "Format Painter (double-click to keep it on)",
  742. onClick: function () { if (p.start) p.start(false); }
  743. }).on("dblclick", function () { if (p.start) p.start(true); });
  744. var copyBtn = o.copyMenu ? split({
  745. svg: "copy", title: "Copy", key: "Ctrl+C", menuTitle: "Copy options",
  746. onClick: run("copy"), menu: o.copyMenu
  747. }) : button({ svg: "copy", title: "Copy", key: "Ctrl+C", onClick: run("copy") });
  748. g.stack([
  749. button({ svg: "cut", title: "Cut", key: "Ctrl+X", onClick: run("cut") }),
  750. copyBtn,
  751. $painter
  752. ]);
  753. g.$el.addClass("of-rb-clipboard");
  754. g.launcher({ title: "Clipboard - everything copied in Office this session", onClick: pane });
  755. return g;
  756. }
  757. /* ---------- closing everything (Escape, a menu opening) ---------- */
  758. function closePopups() {
  759. closePopup();
  760. closeGalleryPop();
  761. }
  762. return {
  763. tab: tab,
  764. hasTabs: hasTabs,
  765. select: select,
  766. activeTab: activeTab,
  767. attachStrip: attachStrip,
  768. refresh: refresh,
  769. fit: fit,
  770. scheduleFit: scheduleFit,
  771. isNarrow: isNarrow,
  772. setMinimized: setMinimized,
  773. isMinimized: isMinimized,
  774. closePopups: closePopups,
  775. // controls
  776. button: button,
  777. dropdown: dropdown,
  778. split: split,
  779. big: big,
  780. toggle: toggle,
  781. colorButton: colorButton,
  782. colorOf: colorOf,
  783. setColor: setColor,
  784. gallery: gallery,
  785. clipboardGroup: clipboardGroup,
  786. host: host
  787. };
  788. })();