PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/ui/panes/options.js

https://bitbucket.org/qwebirc/qwebirc/
JavaScript | 503 lines | 435 code | 64 blank | 4 comment | 59 complexity | d45087577498af69f15c5bafe0fb72ec MD5 | raw file
Possible License(s): BSD-3-Clause
  1. qwebirc.ui.supportsFocus = function() {
  2. var ua = navigator.userAgent;
  3. if(!$defined(ua))
  4. return [true];
  5. if(Browser.Engine.ipod || ua.indexOf("Konqueror") != -1)
  6. return [false, false];
  7. return [true];
  8. }
  9. /**
  10. * Note that options are settable by the uioptions url arg by default unless you specifiy
  11. * settableByURL...
  12. */
  13. qwebirc.config.DEFAULT_OPTIONS = [
  14. [1, "BEEP_ON_MENTION", "Beep on activity", true, {
  15. applyChanges: function(value, ui) {
  16. if(ui.setBeepOnMention)
  17. ui.setBeepOnMention(value);
  18. }
  19. }],
  20. [16, "NOTIFICATIONS", "Emit HTML5 notifications on activity", false, {
  21. enabled: function() {
  22. if(!("Notification" in window))
  23. return [false, false]; /* [disabled, default_value] */
  24. return [true];
  25. },
  26. applyChanges: function(value, ui) {
  27. if(ui.setNotifications)
  28. ui.setNotifications(value);
  29. }
  30. }],
  31. [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
  32. enabled: qwebirc.ui.supportsFocus
  33. }],
  34. [2, "DEDICATED_MSG_WINDOW", "Send privmsgs to dedicated messages window", false],
  35. [4, "DEDICATED_NOTICE_WINDOW", "Send notices to dedicated message window", false],
  36. [3, "NICK_OV_STATUS", "Show status (@/+) before nicknames in channel lines", true],
  37. [5, "ACCEPT_SERVICE_INVITES", "Automatically join channels when invited by Q", true, {
  38. settableByURL: false
  39. }],
  40. [6, "USE_HIDDENHOST", "Hide your hostmask when authed to Q (+x)", true, {
  41. settableByURL: false
  42. }],
  43. [8, "LASTPOS_LINE", "Show a last position indicator for each window", true, {
  44. enabled: qwebirc.ui.supportsFocus
  45. }],
  46. [9, "NICK_COLOURS", "Automatically colour nicknames", false],
  47. [10, "HIDE_JOINPARTS", "Hide JOINS/PARTS/QUITS", false],
  48. [11, "STYLE_HUE", "Adjust user interface hue", function(ui) {
  49. return {class_: qwebirc.config.HueOption, default_: ui.__styleValues.hue};
  50. }, {
  51. applyChanges: function(value, ui) {
  52. ui.setModifiableStylesheetValues({hue: value});
  53. }
  54. }],
  55. [12, "QUERY_ON_NICK_CLICK", "Query on nickname click in channel", false],
  56. [13, "SHOW_NICKLIST", "Show nickname list in channels", qwebirc.util.deviceHasKeyboard()],
  57. [14, "SHOW_TIMESTAMPS", "Show timestamps", true], /* we rely on the hue update */
  58. [15, "SIDE_TABS", "Show tabs on the side", false, {
  59. enabled: function() {
  60. if(Browser.Engine.trident && Browser.Engine.version < 8)
  61. return [false, false]; /* [disabled, default_value] */
  62. return [true];
  63. },
  64. applyChanges: function(value, ui) {
  65. ui.setSideTabs(value);
  66. }
  67. }]
  68. ];
  69. qwebirc.config.DefaultOptions = null;
  70. qwebirc.config.Input = new Class({
  71. initialize: function(parent, option, position, parentObject) {
  72. this.option = option;
  73. this.value = option.value;
  74. this.enabled = this.option.enabled;
  75. this.position = position;
  76. this.parentElement = parent;
  77. this.parentObject = parentObject;
  78. this.render();
  79. },
  80. createInput: function(type, parent, name, selected, id) {
  81. if(!$defined(parent))
  82. parent = this.parentElement;
  83. return qwebirc.util.createInput(type, parent, name, selected, this.option.id);
  84. },
  85. FE: function(element, parent) {
  86. var n = new Element(element);
  87. if(!$defined(parent))
  88. parent = this.parentElement;
  89. parent.appendChild(n);
  90. return n;
  91. },
  92. focus: function() {
  93. this.mainElement.focus();
  94. },
  95. render: function() {
  96. this.event("render", this.mainElement);
  97. },
  98. applyChanges: function() {
  99. this.event("applyChanges", [this.get(), this.parentObject.optionObject.ui]);
  100. },
  101. event: function(name, x) {
  102. if(!$defined(this.option.extras))
  103. return;
  104. var t = this.option.extras[name];
  105. if(!$defined(t))
  106. return;
  107. t.pass(x, this)();
  108. },
  109. cancel: function() {
  110. }
  111. });
  112. qwebirc.config.TextInput = new Class({
  113. Extends: qwebirc.config.Input,
  114. render: function() {
  115. var i = this.createInput("text");
  116. this.mainElement = i;
  117. i.value = this.value;
  118. i.disabled = !this.enabled;
  119. this.parent();
  120. },
  121. get: function() {
  122. return this.mainElement.value;
  123. }
  124. });
  125. qwebirc.config.HueInput = new Class({
  126. Extends: qwebirc.config.Input,
  127. render: function() {
  128. var i = new Element("div");
  129. i.addClass("qwebirc-optionspane");
  130. i.addClass("hue-slider");
  131. this.parentElement.appendChild(i);
  132. var k = new Element("div");
  133. k.addClass("knob");
  134. if(Browser.Engine.trident) {
  135. k.setStyle("top", "0px");
  136. k.setStyle("background-color", "black");
  137. }
  138. i.appendChild(k);
  139. var slider = new Slider(i, k, {steps: 36, range: [0, 369], wheel: true});
  140. slider.set(this.value);
  141. this.startValue = this.value;
  142. slider.addEvent("change", function(step) {
  143. this.value = step;
  144. this.applyChanges();
  145. }.bind(this));
  146. this.mainElement = i;
  147. if(!this.enabled)
  148. slider.detach();
  149. this.parent();
  150. },
  151. get: function() {
  152. return this.value;
  153. },
  154. cancel: function() {
  155. this.value = this.startValue;
  156. this.applyChanges();
  157. }
  158. });
  159. qwebirc.config.CheckInput = new Class({
  160. Extends: qwebirc.config.Input,
  161. render: function() {
  162. var i = this.createInput("checkbox", null, null, null, this.id);
  163. this.mainElement = i;
  164. i.checked = this.value;
  165. i.disabled = !this.enabled;
  166. this.parent();
  167. },
  168. get: function() {
  169. return this.mainElement.checked;
  170. }
  171. });
  172. qwebirc.config.RadioInput = new Class({
  173. Extends: qwebirc.config.Input,
  174. render: function() {
  175. var value = this.option.options;
  176. this.elements = [];
  177. for(var i=0;i<value.length;i++) {
  178. var d = this.FE("div", this.parentObject);
  179. var e = this.createInput("radio", d, "options_radio" + this.position, i == this.option.position);
  180. this.elements.push(e);
  181. e.disabled = !this.enabled;
  182. if(i == 0)
  183. this.mainElement = e;
  184. d.appendChild(document.createTextNode(value[i][0]));
  185. };
  186. this.parent();
  187. },
  188. get: function() {
  189. for(var i=0;i<this.elements.length;i++) {
  190. var x = this.elements[i];
  191. if(x.checked) {
  192. this.option.position = i;
  193. return this.option.options[i][1];
  194. }
  195. }
  196. }
  197. });
  198. qwebirc.config.Option = new Class({
  199. initialize: function(optionId, prefix, label, default_, extras) {
  200. this.prefix = prefix;
  201. this.label = label;
  202. this.default_ = default_;
  203. this.optionId = optionId;
  204. this.extras = extras;
  205. if($defined(extras) && $defined(extras.enabled)) {
  206. var enabledResult = extras.enabled();
  207. this.enabled = enabledResult[0];
  208. if(!enabledResult[0] && enabledResult.length > 1)
  209. this.default_ = enabledResult[1];
  210. } else {
  211. this.enabled = true;
  212. }
  213. if($defined(extras) && $defined(extras.settableByURL)) {
  214. this.settableByURL = extras.settableByURL;
  215. } else {
  216. this.settableByURL = true;
  217. }
  218. },
  219. setSavedValue: function(x) {
  220. if(this.enabled)
  221. this.value = x;
  222. }
  223. });
  224. qwebirc.config.RadioOption = new Class({
  225. Extends: qwebirc.config.Option,
  226. Element: qwebirc.config.RadioInput,
  227. initialize: function(optionId, prefix, label, default_, extras, options) {
  228. this.options = options.map(function(x) {
  229. if(typeof(x) == "string")
  230. return [x, x];
  231. return x;
  232. });
  233. this.defaultposition = default_;
  234. this.parent(optionId, prefix, label, this.options[default_][1], extras);
  235. },
  236. setSavedValue: function(x) {
  237. for(var i=0;i<this.options.length;i++) {
  238. var y = this.options[i][1];
  239. if(x == y) {
  240. this.position = i;
  241. this.value = x;
  242. return;
  243. }
  244. }
  245. this.position = this.defaultposition;
  246. this.value = this.default_;
  247. }
  248. });
  249. qwebirc.config.TextOption = new Class({
  250. Extends: qwebirc.config.Option,
  251. Element: qwebirc.config.TextInput
  252. });
  253. qwebirc.config.CheckOption = new Class({
  254. Extends: qwebirc.config.Option,
  255. Element: qwebirc.config.CheckInput
  256. });
  257. qwebirc.config.HueOption = new Class({
  258. Extends: qwebirc.config.Option,
  259. Element: qwebirc.config.HueInput
  260. });
  261. qwebirc.ui.Options = new Class({
  262. initialize: function(ui) {
  263. this.ui = ui;
  264. if(!$defined(qwebirc.config.DefaultOptions))
  265. this.__configureDefaults();
  266. this.optionList = qwebirc.config.DefaultOptions.slice();
  267. this.optionHash = {};
  268. this._setup();
  269. this.optionList.forEach(function(x) {
  270. x.setSavedValue(this._get(x));
  271. this.optionHash[x.prefix] = x;
  272. this[x.prefix] = x.value;
  273. }.bind(this));
  274. },
  275. __configureDefaults: function() {
  276. qwebirc.config.DefaultOptions = qwebirc.config.DEFAULT_OPTIONS.map(function(x) {
  277. var optionId = x[0];
  278. var prefix = x[1];
  279. var label = x[2];
  280. var default_ = x[3];
  281. var moreextras = x[4];
  282. var extras = x[5];
  283. var stype = typeof(default_);
  284. if(stype == "number") {
  285. return new qwebirc.config.RadioOption(optionId, prefix, label, default_, moreextras, extra);
  286. } else {
  287. var type;
  288. if(stype == "boolean") {
  289. type = qwebirc.config.CheckOption;
  290. } else if(stype == "function") {
  291. var options = default_.call(this, this.ui);
  292. type = options.class_;
  293. default_ = options.default_;
  294. } else {
  295. type = qwebirc.config.TextOption;
  296. }
  297. return new type(optionId, prefix, label, default_, moreextras);
  298. }
  299. }, this);
  300. },
  301. setValue: function(option, value) {
  302. this.optionHash[option.prefix].value = value;
  303. this[option.prefix] = value;
  304. },
  305. setValueByPrefix: function(prefix, value) {
  306. this.optionHash[prefix].value = value;
  307. this[prefix] = value;
  308. },
  309. getOptionList: function() {
  310. return this.optionList;
  311. },
  312. _get: function(x) {
  313. return x.default_;
  314. },
  315. _setup: function() {
  316. },
  317. flush: function() {
  318. }
  319. });
  320. qwebirc.ui.OptionsPane = new Class({
  321. Implements: [Events],
  322. initialize: function(parentElement, optionObject) {
  323. this.parentElement = parentElement;
  324. this.optionObject = optionObject;
  325. this.createElements();
  326. },
  327. createElements: function() {
  328. var FE = function(element, parent) {
  329. var n = new Element(element);
  330. parent.appendChild(n);
  331. return n;
  332. };
  333. var t = FE("table", this.parentElement);
  334. var tb = FE("tbody", t);
  335. this.boxList = [];
  336. var optList = this.optionObject.getOptionList();
  337. for(var i=0;i<optList.length;i++) {
  338. var x = optList[i];
  339. var row = FE("tr", tb);
  340. var cella = FE("td", row);
  341. x.id = qwebirc.util.generateID();
  342. var label = new Element("label", {"for": x.id});
  343. cella.appendChild(label);
  344. label.set("text", x.label + ":");
  345. var cellb = FE("td", row);
  346. this.boxList.push([x, new x.Element(cellb, x, i, this)]);
  347. }
  348. var r = FE("tr", tb);
  349. var cella = FE("td", r);
  350. var cellb = FE("td", r);
  351. var save = qwebirc.util.createInput("submit", cellb);
  352. save.value = "Save";
  353. save.addEvent("click", function() {
  354. this.save();
  355. this.fireEvent("close");
  356. }.bind(this));
  357. var cancel = qwebirc.util.createInput("submit", cellb);
  358. cancel.value = "Cancel";
  359. cancel.addEvent("click", function() {
  360. this.cancel();
  361. this.fireEvent("close");
  362. }.bind(this));
  363. },
  364. save: function() {
  365. this.boxList.forEach(function(x) {
  366. var option = x[0];
  367. var box = x[1];
  368. this.optionObject.setValue(option, box.get());
  369. }.bind(this));
  370. this.boxList.forEach(function(x) {
  371. x[1].applyChanges();
  372. }.bind(this));
  373. this.optionObject.flush();
  374. },
  375. cancel: function() {
  376. this.boxList.forEach(function(x) {
  377. x[1].cancel();
  378. }.bind(this));
  379. }
  380. });
  381. qwebirc.ui.CookieOptions = new Class({
  382. Extends: qwebirc.ui.Options,
  383. _setup: function() {
  384. this.__cookie = new Hash.Cookie("opt1", {duration: 3650, autoSave: false});
  385. },
  386. _get: function(x) {
  387. var v = this.__cookie.get(x.optionId);
  388. if(!$defined(v))
  389. return x.default_;
  390. return v;
  391. },
  392. flush: function() {
  393. this.__cookie.erase();
  394. this._setup();
  395. this.getOptionList().forEach(function(x) {
  396. this.__cookie.set(x.optionId, x.value);
  397. }.bind(this));
  398. this.__cookie.save();
  399. }
  400. });
  401. qwebirc.ui.SuppliedArgOptions = new Class({
  402. Extends: qwebirc.ui.CookieOptions,
  403. initialize: function(ui, arg) {
  404. var p = new QHash();
  405. if($defined(arg) && arg != "" && arg.length > 2) {
  406. var checksum = arg.substr(arg.length - 2, 2);
  407. var decoded = qwebirc.util.b64Decode(arg.substr(0, arg.length - 2));
  408. if(decoded && (new qwebirc.util.crypto.MD5().digest(decoded).slice(0, 2) == checksum)) {
  409. var p2 = qwebirc.util.parseURI("?" + decoded);
  410. p2.each(function(k, v) {
  411. p.put(k, JSON.decode(v, true));
  412. });
  413. }
  414. }
  415. this.parsedOptions = p;
  416. this.parent(ui);
  417. },
  418. _get: function(x) {
  419. if(x.settableByURL !== true)
  420. return this.parent(x);
  421. var opt = this.parsedOptions.get(String(x.optionId));
  422. if(!$defined(opt))
  423. return this.parent(x);
  424. return opt;
  425. },
  426. serialise: function() {
  427. var result = [];
  428. this.getOptionList().forEach(function(x) {
  429. if(x.settableByURL && x.default_ != x.value)
  430. result.push(x.optionId + "=" + JSON.encode(x.value));
  431. }.bind(this));
  432. var raw = result.join("&");
  433. var checksum = new qwebirc.util.crypto.MD5().digest(raw).slice(0, 2);
  434. return (qwebirc.util.b64Encode(raw)).replaceAll("=", "") + checksum;
  435. }
  436. });
  437. qwebirc.ui.DefaultOptionsClass = new Class({
  438. Extends: qwebirc.ui.SuppliedArgOptions
  439. });