Example #1
0
void WContainerWidget::rootAsJavaScript(WApplication *app, std::ostream& out,
					bool all)
{
  std::vector<WWidget *> *toAdd
    = all ? children_ : (transientImpl_ ? &transientImpl_->addedChildren_ : 0);

  if (toAdd)
    for (unsigned i = 0; i < toAdd->size(); ++i) {
      DomElement *c = (*toAdd)[i]->createSDomElement(app);

      app->streamBeforeLoadJavaScript(out, false);

      c->callMethod("omousemove=function(e) {"
		    "if (!e) e = window.event;"
		    "return " + app->javaScriptClass()
		    + "._p_.dragDrag(event); }");
      c->callMethod("mouseup=function(e) {"
		    "if (!e) e = window.event;"
		    "return " + app->javaScriptClass()
		  + "._p_.dragEnd(event);}");
      c->callMethod("dragstart=function(){return false;}");
      c->asJavaScript(out);
      delete c;
    }

  if (transientImpl_)
    transientImpl_->addedChildren_.clear();

  if (!all) {
    /* Note: we ignore rendering of deletion of a bound widget... */
  }

  // FIXME
  propagateRenderOk(false);
}
Example #2
0
void WCssStyleSheet::javaScriptUpdate(WApplication *app,
				      std::ostream& js, bool all)
{
  if (!all) {
    for (unsigned i = 0; i < rulesRemoved_.size(); ++i) {
      js << WT_CLASS ".removeCssRule(";
      DomElement::jsStringLiteral(js, rulesRemoved_[i], '\'');
      js << ");";
    }
    rulesRemoved_.clear();

    for (RuleSet::const_iterator i = rulesModified_.begin();
	 i != rulesModified_.end(); ++i) {
      js << "{ var d= " WT_CLASS ".getCssRule(";
      DomElement::jsStringLiteral(js, (*i)->selector(), '\'');
      js << ");if(d){";

      DomElement *d = DomElement::updateGiven("d", DomElement_SPAN);
      if ((*i)->updateDomElement(*d, false)) {
	EscapeOStream sout(js);
	d->asJavaScript(sout, DomElement::Update);
      }

      delete d;

      js << "}}";
    }
    rulesModified_.clear();
  }

  if (!app->environment().agentIsIElt(9)
      && app->environment().agent() != WEnvironment::Konqueror) {
    RuleList& toProcess = all ? rules_ : rulesAdded_;

    for (unsigned i = 0; i < toProcess.size(); ++i) {
      WCssRule *rule = toProcess[i];
      js << WT_CLASS ".addCss('"
	 << rule->selector() << "',";
      DomElement::jsStringLiteral(js, rule->declarations(), '\'');
      js << ");" << std::endl;
    }

    rulesAdded_.clear();
    if (all)
      rulesModified_.clear();
  } else {
    std::string text = cssText(all);

    if (!text.empty()) {
      js << WT_CLASS ".addCssText(";
      DomElement::jsStringLiteral(js, text, '\'');
      js << ");" << std::endl;
    }
  }
}
Example #3
0
File: Widget.C Project: ReWeb3D/wt
std::string Widget::createExtElement(std::stringstream& alljs,
				     DomElement *inContainer)
{
  if (inContainer) {
    updateDom(*inContainer, true);
    inContainer->removeProperty(PropertyStyleDisplay);
  }

  setRendered(false);

  alljs << "var " << elVar() << ";"
	<< createJS(inContainer)
	<< elRef() << "=" << elVar() << ";";

  if (isHidden())
    alljs << elVar() << ".hide();";

  jsUpdates_.clear();

  if (!inContainer) {
    DomElement *e
      = DomElement::updateGiven(elVar() + ".getEl().dom", domElementType());
    updateDom(*e, true);

    {
      EscapeOStream out(alljs);
      e->asJavaScript(out, DomElement::Update);
    }

    delete e;
  }

  setRendered(true);

  return elVar();
}
Example #4
0
std::string DomElement::asJavaScript(EscapeOStream& out,
				     Priority priority) const
{
  switch(priority) {
  case Delete:
    if (deleted_ || removeAllChildren_) {
      out << declare();

      if (deleted_) {
	out << var_ << ".parentNode.removeChild("
	    << var_ << ");";
      } else  if (removeAllChildren_) {
	out << "while (" << var_ << ".firstChild) {"
	    << var_ << ".removeChild(" << var_ << ".firstChild);"
	    << "}";
      }
    }

    return var_;

    break;
  case Create:
    if (mode_ == ModeCreate) {
      out << declare();

      if (id_.length() != 0)
	out << var_ << ".setAttribute('id', '" << id_ << "');";
    }

    return var_;

    break;
  case Update:
  {
    /*
     * short-cut for frequent short manipulations
     */
    if (mode_ == ModeUpdate && numManipulations_ == 1) {
      if (properties_.find(Wt::PropertyStyleDisplay) != properties_.end()) {
	std::string style = properties_.find(Wt::PropertyStyleDisplay)->second;
	if (style == "none") {
	  out << "hide('" << id_ << "');";
	  return var_;
	} else if (style == "inline") {
	  out << "inline('" + id_ + "');";
	  return var_;
	} else if (style == "block") {
	  out << "block('" + id_ + "');";
	  return var_;
	}
      }
    }

    processEvents();

    if (deleted_)
      break;

    if (replaced_) {
      declare(out);

      std::string varr = replaced_->asJavaScript(out, Create);
      replaced_->asJavaScript(out, Update);

      out << varr << ".style.display = " << var_ << ".style.display;";
      out << var_ << ".parentNode.replaceChild("
	  << varr << "," << var_ << ");";

      return var_;
    } else if (insertBefore_) {
      declare(out);

      std::string varr = insertBefore_->asJavaScript(out, Create);
      insertBefore_->asJavaScript(out, Update);

      out << var_ << ".parentNode.insertBefore(" << varr << ","
	  << var_ + ");";

      return var_;
    }

    const AttributeMap& attributesToSet = attributes_;

    for (AttributeMap::const_iterator i = attributesToSet.begin();
	 i != attributesToSet.end(); ++i) {
      declare(out);

      if (i->first == "class") {
	out << var_ << ".className = ";
	jsStringLiteral(out, i->second, '\'');
	out << ";";
      } else {
	out << var_ << ".setAttribute('" << i->first << "',";
	jsStringLiteral(out, i->second, '\'');
	out << ");";
      }
    }


    for (EventHandlerMap::const_iterator i = eventHandlers_.begin();
	 i != eventHandlers_.end(); ++i) {
      if ((mode_ == ModeUpdate) || (i->second.first.length() > 0)) {
	declare(out);

	std::string fName = "f" + boost::lexical_cast<std::string>(nextId_++);

	out << "function " << fName
	    << "(event) { if (!event) event = window.event; "
	    << i->second.first << "}";

	out << var_ << ".on" << i->first << "=" << fName << ";";
      }
    }

    if ((mode_ == ModeCreate || wasEmpty_) && canWriteInnerHTML()) {
      if (!childrenToAdd_.empty() || !childrenHtml_.empty()) {
	declare(out);

	out << var_ << ".innerHTML=\'";

	EscapeOStreamScope scope = jsStringLiteral(out, '\'');

	out << childrenHtml_;

	TimeoutList timeouts;
	for (unsigned i = 0; i < childrenToAdd_.size(); ++i)
	  childrenToAdd_[i].second->asHTML(out, timeouts);

	scope.pop();

	out << "\';";

	createTimeoutJs(out, timeouts);
      }
    } else {
      for (unsigned i = 0; i < childrenToAdd_.size(); ++i) {
	declare(out);

	DomElement *child = childrenToAdd_[i].second;

	std::string cvar = child->asJavaScript(out, Create);
	child->asJavaScript(out, Update);
	
	if (childrenToAdd_[i].first.length() > 0) {
	  DomElement *beforeElement
	    = getForUpdate(childrenToAdd_[i].first, DIV);

	  out << var_ << ".insertBefore("
	      << cvar << "," << beforeElement->createReference() << ");";

	  delete beforeElement;
	} else {
	  out << var_ << ".appendChild(" << cvar << ");";
	}
      }
    }

    for (PropertyMap::const_iterator i = properties_.begin();
	 i != properties_.end(); ++i) {
      declare(out);

      std::string p;
      std::string value;

      switch(i->first) {
      case Wt::PropertyInnerHTML:
	out << var_ << ".innerHTML=";
	jsStringLiteral(out, i->second, '\'');
	out << ';';
	break;
      case Wt::PropertyValue:
	out << var_ << ".value=";
	jsStringLiteral(out, i->second, '\'');
	out << ';';
	break;
      case Wt::PropertyDisabled:
	out << var_ << ".disabled=" << i->second << ';';
	break;
      case Wt::PropertyChecked:
	out << var_ << ".checked=" << i->second << ';';
	break;
      case Wt::PropertySelected:
	out << var_ << ".selected=" << i->second << ';';
	break;
      case Wt::PropertySelectedIndex:
	out << var_ << ".selectedIndex=" << i->second << ';';
	break;
      case Wt::PropertySrc:
	out << var_ << ".src='" << i->second << "\';";
	break;
      case Wt::PropertyText:
	out << var_ << ".text=";
	jsStringLiteral(out, i->second, '\'');
	out << ';';
	break;
      default:
	if ((i->first >= Wt::PropertyStylePosition)
	    && (i->first <= Wt::PropertyStyleDisplay)) {
	  static std::string cssNames[] =
	    { "position",
	      "zIndex", "float", "clear",
	      "width", "height",
	      "minWidth", "minHeight",
	      "maxWidth", "maxHeight",
	      "left", "right", "top", "bottom",
	      "verticalAlign", "textAlign",
	      "padding",
	      "marginTop", "marginRight",
	      "marginBottom", "marginLeft",
	      "cursor", 	    
	      "borderTop", "borderRight",
	      "borderBottom", "borderLeft",
	      "color", "overflow", "overflow",
	      "fontFamily", "fontStyle", "fontVariant",
	      "fontWeight", "fontSize",
	      "backgroundColor", "backgroundImage", "backgroundRepeat",
	      "backgroundPosition",
	      "textDecoration", "tableLayout", "borderSpacing", "display" };
	  out << var_ << ".style."
	      << cssNames[i->first - Wt::PropertyStylePosition]
	      << "=\'" << i->second << "\';";
	}
      }
    }

    for (unsigned i = 0; i < methodCalls_.size(); ++i) {
      declare(out);
      out << var_ << "." << methodCalls_[i] << ';';
    }

    if (timeOut_ != -1)
      out << "addTimerEvent('" << id_ << "', " 
	  << boost::lexical_cast<std::string>(timeOut_) + ");";


    out << javaScript_;

    return var_;
  }
  }

  return var_;
}