Guest User

Untitled

a guest
Oct 5th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*\
  2. title: $:/core/modules/widgets/button.js
  3. type: application/javascript
  4. module-type: widget
  5.  
  6. Button widget
  7.  
  8. \*/
  9. (function(){
  10.  
  11. /*jslint node: true, browser: true */
  12. /*global $tw: false */
  13. "use strict";
  14.  
  15. var Widget = require("$:/core/modules/widgets/widget.js").widget;
  16.  
  17. var ButtonWidget = function(parseTreeNode,options) {
  18.     this.initialise(parseTreeNode,options);
  19. };
  20.  
  21. /*
  22. Inherit from the base widget class
  23. */
  24. ButtonWidget.prototype = new Widget();
  25.  
  26. /*
  27. Render this widget into the DOM
  28. */
  29. ButtonWidget.prototype.render = function(parent,nextSibling) {
  30.     var self = this;
  31.     // Remember parent
  32.     this.parentDomNode = parent;
  33.     // Compute attributes and execute state
  34.     this.computeAttributes();
  35.     this.execute();
  36.     // Create element
  37.     var tag = "button";
  38.     if(this.buttonTag && $tw.config.htmlUnsafeElements.indexOf(this.buttonTag) === -1) {
  39.         tag = this.buttonTag;
  40.     }
  41.     var domNode = this.document.createElement(tag);
  42.     // Assign classes
  43.     var classes = this["class"].split(" ") || [],
  44.         isPoppedUp = this.popup && this.isPoppedUp();
  45.     if(this.selectedClass) {
  46.         if(this.set && this.setTo && this.isSelected()) {
  47.             $tw.utils.pushTop(classes,this.selectedClass.split(" "));
  48.         }
  49.         if(isPoppedUp) {
  50.             $tw.utils.pushTop(classes,this.selectedClass.split(" "));
  51.         }
  52.     }
  53.     if(isPoppedUp) {
  54.         $tw.utils.pushTop(classes,"tc-popup-handle");
  55.     }
  56.     domNode.className = classes.join(" ");
  57.     // Assign other attributes
  58.     if(this.style) {
  59.         domNode.setAttribute("style",this.style);
  60.     }
  61.     if(this.tooltip) {
  62.         domNode.setAttribute("title",this.tooltip);
  63.     }
  64.     if(this["aria-label"]) {
  65.         domNode.setAttribute("aria-label",this["aria-label"]);
  66.     }
  67.         if(this["accesskey"]) {
  68.                 domNode.setAttribute("accesskey",this["accesskey"]);
  69.         }
  70.  
  71.     // Add a click event handler
  72.     domNode.addEventListener("click",function (event) {
  73.         var handled = false;
  74.         if(self.invokeActions(this,event)) {
  75.             handled = true;
  76.         }
  77.         if(self.to) {
  78.             self.navigateTo(event);
  79.             handled = true;
  80.         }
  81.         if(self.message) {
  82.             self.dispatchMessage(event);
  83.             handled = true;
  84.         }
  85.         if(self.popup) {
  86.             self.triggerPopup(event);
  87.             handled = true;
  88.         }
  89.         if(self.set) {
  90.             self.setTiddler();
  91.             handled = true;
  92.         }
  93.         if(self.actions) {
  94.             self.invokeActionString(self.actions,self,event);
  95.         }
  96.         if(handled) {
  97.             event.preventDefault();
  98.             event.stopPropagation();
  99.         }
  100.         return handled;
  101.     },false);
  102.     // Make it draggable if required
  103.     if(this.dragTiddler || this.dragFilter) {
  104.         $tw.utils.makeDraggable({
  105.             domNode: domNode,
  106.             dragTiddlerFn: function() {return self.dragTiddler;},
  107.             dragFilterFn: function() {return self.dragFilter;},
  108.             widget: this
  109.         });
  110.     }
  111.     // Insert element
  112.     parent.insertBefore(domNode,nextSibling);
  113.     this.renderChildren(domNode,null);
  114.     this.domNodes.push(domNode);
  115. };
  116.  
  117. /*
  118. We don't allow actions to propagate because we trigger actions ourselves
  119. */
  120. ButtonWidget.prototype.allowActionPropagation = function() {
  121.     return false;
  122. };
  123.  
  124. ButtonWidget.prototype.getBoundingClientRect = function() {
  125.     return this.domNodes[0].getBoundingClientRect();
  126. };
  127.  
  128. ButtonWidget.prototype.isSelected = function() {
  129.     return this.wiki.getTextReference(this.set,this.defaultSetValue,this.getVariable("currentTiddler")) === this.setTo;
  130. };
  131.  
  132. ButtonWidget.prototype.isPoppedUp = function() {
  133.     var tiddler = this.wiki.getTiddler(this.popup);
  134.     var result = tiddler && tiddler.fields.text ? $tw.popup.readPopupState(tiddler.fields.text) : false;
  135.     return result;
  136. };
  137.  
  138. ButtonWidget.prototype.navigateTo = function(event) {
  139.     var bounds = this.getBoundingClientRect();
  140.     this.dispatchEvent({
  141.         type: "tm-navigate",
  142.         navigateTo: this.to,
  143.         navigateFromTitle: this.getVariable("storyTiddler"),
  144.         navigateFromNode: this,
  145.         navigateFromClientRect: { top: bounds.top, left: bounds.left, width: bounds.width, right: bounds.right, bottom: bounds.bottom, height: bounds.height
  146.         },
  147.         navigateSuppressNavigation: event.metaKey || event.ctrlKey || (event.button === 1),
  148.         event: event
  149.     });
  150. };
  151.  
  152. ButtonWidget.prototype.dispatchMessage = function(event) {
  153.     this.dispatchEvent({type: this.message, param: this.param, tiddlerTitle: this.getVariable("currentTiddler"), event: event});
  154. };
  155.  
  156. ButtonWidget.prototype.triggerPopup = function(event) {
  157.     $tw.popup.triggerPopup({
  158.         domNode: this.domNodes[0],
  159.         title: this.popup,
  160.         wiki: this.wiki
  161.     });
  162. };
  163.  
  164. ButtonWidget.prototype.setTiddler = function() {
  165.     this.wiki.setTextReference(this.set,this.setTo,this.getVariable("currentTiddler"));
  166. };
  167.  
  168. /*
  169. Compute the internal state of the widget
  170. */
  171. ButtonWidget.prototype.execute = function() {
  172.     // Get attributes
  173.     this.actions = this.getAttribute("actions");
  174.     this.to = this.getAttribute("to");
  175.     this.message = this.getAttribute("message");
  176.     this.param = this.getAttribute("param");
  177.     this.set = this.getAttribute("set");
  178.     this.setTo = this.getAttribute("setTo");
  179.     this.popup = this.getAttribute("popup");
  180.     this.hover = this.getAttribute("hover");
  181.     this["class"] = this.getAttribute("class","");
  182.     this["aria-label"] = this.getAttribute("aria-label");
  183.     this["accesskey"] = this.getAttribute("accesskey");
  184.     this.tooltip = this.getAttribute("tooltip");
  185.     this.style = this.getAttribute("style");
  186.     this.selectedClass = this.getAttribute("selectedClass");
  187.     this.defaultSetValue = this.getAttribute("default","");
  188.     this.buttonTag = this.getAttribute("tag");
  189.     this.dragTiddler = this.getAttribute("dragTiddler");
  190.     this.dragFilter = this.getAttribute("dragFilter");
  191.     // Make child widgets
  192.     this.makeChildWidgets();
  193. };
  194.  
  195. /*
  196. Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
  197. */
  198. ButtonWidget.prototype.refresh = function(changedTiddlers) {
  199.     var changedAttributes = this.computeAttributes();
  200.     if(changedAttributes.to || changedAttributes.message || changedAttributes.param || changedAttributes.set || changedAttributes.setTo || changedAttributes.popup || changedAttributes.hover || changedAttributes["class"] || changedAttributes.selectedClass || changedAttributes.style || changedAttributes.dragFilter || changedAttributes.dragTiddler || (this.set && changedTiddlers[this.set]) || (this.popup && changedTiddlers[this.popup])) {
  201.         this.refreshSelf();
  202.         return true;
  203.     }
  204.     return this.refreshChildren(changedTiddlers);
  205. };
  206.  
  207. exports.button = ButtonWidget;
  208.  
  209. })();
Add Comment
Please, Sign In to add comment