field.js 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // All Rights Reserved.
  15. /**
  16. * @fileoverview Class to encapsulate an editable field. Always uses an
  17. * iframe to contain the editable area, never inherits the style of the
  18. * surrounding page, and is always a fixed height.
  19. *
  20. * @author nicksantos@google.com (Nick Santos)
  21. * @see ../demos/editor/editor.html
  22. * @see ../demos/editor/field_basic.html
  23. */
  24. goog.provide('goog.editor.Field');
  25. goog.provide('goog.editor.Field.EventType');
  26. goog.require('goog.a11y.aria');
  27. goog.require('goog.a11y.aria.Role');
  28. goog.require('goog.array');
  29. goog.require('goog.asserts');
  30. goog.require('goog.async.Delay');
  31. goog.require('goog.dom');
  32. goog.require('goog.dom.Range');
  33. goog.require('goog.dom.TagName');
  34. goog.require('goog.dom.classlist');
  35. goog.require('goog.dom.safe');
  36. goog.require('goog.editor.BrowserFeature');
  37. goog.require('goog.editor.Command');
  38. goog.require('goog.editor.Plugin');
  39. goog.require('goog.editor.icontent');
  40. goog.require('goog.editor.icontent.FieldFormatInfo');
  41. goog.require('goog.editor.icontent.FieldStyleInfo');
  42. goog.require('goog.editor.node');
  43. goog.require('goog.editor.range');
  44. goog.require('goog.events');
  45. goog.require('goog.events.EventHandler');
  46. goog.require('goog.events.EventTarget');
  47. goog.require('goog.events.EventType');
  48. goog.require('goog.events.KeyCodes');
  49. goog.require('goog.functions');
  50. goog.require('goog.html.SafeHtml');
  51. goog.require('goog.html.legacyconversions');
  52. goog.require('goog.log');
  53. goog.require('goog.log.Level');
  54. goog.require('goog.string');
  55. goog.require('goog.string.Unicode');
  56. goog.require('goog.style');
  57. goog.require('goog.userAgent');
  58. goog.require('goog.userAgent.product');
  59. /**
  60. * This class encapsulates an editable field.
  61. *
  62. * event: load Fires when the field is loaded
  63. * event: unload Fires when the field is unloaded (made not editable)
  64. *
  65. * event: beforechange Fires before the content of the field might change
  66. *
  67. * event: delayedchange Fires a short time after field has changed. If multiple
  68. * change events happen really close to each other only
  69. * the last one will trigger the delayedchange event.
  70. *
  71. * event: beforefocus Fires before the field becomes active
  72. * event: focus Fires when the field becomes active. Fires after the blur event
  73. * event: blur Fires when the field becomes inactive
  74. *
  75. * TODO: figure out if blur or beforefocus fires first in IE and make FF match
  76. *
  77. * @param {string} id An identifer for the field. This is used to find the
  78. * field and the element associated with this field.
  79. * @param {Document=} opt_doc The document that the element with the given
  80. * id can be found in. If not provided, the default document is used.
  81. * @constructor
  82. * @extends {goog.events.EventTarget}
  83. */
  84. goog.editor.Field = function(id, opt_doc) {
  85. goog.events.EventTarget.call(this);
  86. /**
  87. * The id for this editable field, which must match the id of the element
  88. * associated with this field.
  89. * @type {string}
  90. */
  91. this.id = id;
  92. /**
  93. * The hash code for this field. Should be equal to the id.
  94. * @type {string}
  95. * @private
  96. */
  97. this.hashCode_ = id;
  98. /**
  99. * Dom helper for the editable node.
  100. * @type {goog.dom.DomHelper}
  101. * @protected
  102. */
  103. this.editableDomHelper = null;
  104. /**
  105. * Map of class id to registered plugin.
  106. * @type {Object}
  107. * @private
  108. */
  109. this.plugins_ = {};
  110. /**
  111. * Plugins registered on this field, indexed by the goog.editor.Plugin.Op
  112. * that they support.
  113. * @type {Object<Array<goog.editor.Plugin>>}
  114. * @private
  115. */
  116. this.indexedPlugins_ = {};
  117. for (var op in goog.editor.Plugin.OPCODE) {
  118. this.indexedPlugins_[op] = [];
  119. }
  120. /**
  121. * Additional styles to install for the editable field.
  122. * @type {string}
  123. * @protected
  124. */
  125. this.cssStyles = '';
  126. // The field will not listen to change events until it has finished loading
  127. /** @private */
  128. this.stoppedEvents_ = {};
  129. this.stopEvent(goog.editor.Field.EventType.CHANGE);
  130. this.stopEvent(goog.editor.Field.EventType.DELAYEDCHANGE);
  131. /** @private */
  132. this.isModified_ = false;
  133. /** @private */
  134. this.isEverModified_ = false;
  135. /** @private */
  136. this.delayedChangeTimer_ = new goog.async.Delay(
  137. this.dispatchDelayedChange_, goog.editor.Field.DELAYED_CHANGE_FREQUENCY,
  138. this);
  139. /** @private */
  140. this.debouncedEvents_ = {};
  141. for (var key in goog.editor.Field.EventType) {
  142. this.debouncedEvents_[goog.editor.Field.EventType[key]] = 0;
  143. }
  144. if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
  145. /** @private */
  146. this.changeTimerGecko_ = new goog.async.Delay(
  147. this.handleChange, goog.editor.Field.CHANGE_FREQUENCY, this);
  148. }
  149. /**
  150. * @type {goog.events.EventHandler<!goog.editor.Field>}
  151. * @protected
  152. */
  153. this.eventRegister = new goog.events.EventHandler(this);
  154. // Wrappers around this field, to be disposed when the field is disposed.
  155. /** @private */
  156. this.wrappers_ = [];
  157. /** @private */
  158. this.loadState_ = goog.editor.Field.LoadState_.UNEDITABLE;
  159. var doc = opt_doc || document;
  160. /**
  161. * The dom helper for the node to be made editable.
  162. * @type {goog.dom.DomHelper}
  163. * @protected
  164. */
  165. this.originalDomHelper = goog.dom.getDomHelper(doc);
  166. /**
  167. * The original node that is being made editable, or null if it has
  168. * not yet been found.
  169. * @type {Element}
  170. * @protected
  171. */
  172. this.originalElement = this.originalDomHelper.getElement(this.id);
  173. /**
  174. * @private {boolean}
  175. */
  176. this.followLinkInNewWindow_ =
  177. goog.editor.BrowserFeature.FOLLOWS_EDITABLE_LINKS;
  178. // Default to the same window as the field is in.
  179. /** @private */
  180. this.appWindow_ = this.originalDomHelper.getWindow();
  181. };
  182. goog.inherits(goog.editor.Field, goog.events.EventTarget);
  183. /**
  184. * The editable dom node.
  185. * @type {Element}
  186. * TODO(user): Make this private!
  187. */
  188. goog.editor.Field.prototype.field = null;
  189. /**
  190. * Logging object.
  191. * @type {goog.log.Logger}
  192. * @protected
  193. */
  194. goog.editor.Field.prototype.logger = goog.log.getLogger('goog.editor.Field');
  195. /**
  196. * Event types that can be stopped/started.
  197. * @enum {string}
  198. */
  199. goog.editor.Field.EventType = {
  200. /**
  201. * Dispatched when the command state of the selection may have changed. This
  202. * event should be listened to for updating toolbar state.
  203. */
  204. COMMAND_VALUE_CHANGE: 'cvc',
  205. /**
  206. * Dispatched when the field is loaded and ready to use.
  207. */
  208. LOAD: 'load',
  209. /**
  210. * Dispatched when the field is fully unloaded and uneditable.
  211. */
  212. UNLOAD: 'unload',
  213. /**
  214. * Dispatched before the field contents are changed.
  215. */
  216. BEFORECHANGE: 'beforechange',
  217. /**
  218. * Dispatched when the field contents change, in FF only.
  219. * Used for internal resizing, please do not use.
  220. */
  221. CHANGE: 'change',
  222. /**
  223. * Dispatched on a slight delay after changes are made.
  224. * Use for autosave, or other times your app needs to know
  225. * that the field contents changed.
  226. */
  227. DELAYEDCHANGE: 'delayedchange',
  228. /**
  229. * Dispatched before focus in moved into the field.
  230. */
  231. BEFOREFOCUS: 'beforefocus',
  232. /**
  233. * Dispatched when focus is moved into the field.
  234. */
  235. FOCUS: 'focus',
  236. /**
  237. * Dispatched when the field is blurred.
  238. */
  239. BLUR: 'blur',
  240. /**
  241. * Dispatched before tab is handled by the field. This is a legacy way
  242. * of controlling tab behavior. Use trog.plugins.AbstractTabHandler now.
  243. */
  244. BEFORETAB: 'beforetab',
  245. /**
  246. * Dispatched after the iframe containing the field is resized, so that UI
  247. * components which contain it can respond.
  248. */
  249. IFRAME_RESIZED: 'ifrsz',
  250. /**
  251. * Dispatched after a user action that will eventually fire a SELECTIONCHANGE
  252. * event. For mouseups, this is fired immediately before SELECTIONCHANGE,
  253. * since {@link #handleMouseUp_} fires SELECTIONCHANGE immediately. May be
  254. * fired up to {@link #SELECTION_CHANGE_FREQUENCY_} ms before SELECTIONCHANGE
  255. * is fired in the case of keyup events, since they use
  256. * {@link #selectionChangeTimer_}.
  257. */
  258. BEFORESELECTIONCHANGE: 'beforeselectionchange',
  259. /**
  260. * Dispatched when the selection changes.
  261. * Use handleSelectionChange from plugin API instead of listening
  262. * directly to this event.
  263. */
  264. SELECTIONCHANGE: 'selectionchange'
  265. };
  266. /**
  267. * The load state of the field.
  268. * @enum {number}
  269. * @private
  270. */
  271. goog.editor.Field.LoadState_ = {
  272. UNEDITABLE: 0,
  273. LOADING: 1,
  274. EDITABLE: 2
  275. };
  276. /**
  277. * The amount of time that a debounce blocks an event.
  278. * TODO(nicksantos): As of 9/30/07, this is only used for blocking
  279. * a keyup event after a keydown. We might need to tweak this for other
  280. * types of events. Maybe have a per-event debounce time?
  281. * @type {number}
  282. * @private
  283. */
  284. goog.editor.Field.DEBOUNCE_TIME_MS_ = 500;
  285. /**
  286. * There is at most one "active" field at a time. By "active" field, we mean
  287. * a field that has focus and is being used.
  288. * @type {?string}
  289. * @private
  290. */
  291. goog.editor.Field.activeFieldId_ = null;
  292. /**
  293. * Whether this field is in "modal interaction" mode. This usually
  294. * means that it's being edited by a dialog.
  295. * @type {boolean}
  296. * @private
  297. */
  298. goog.editor.Field.prototype.inModalMode_ = false;
  299. /**
  300. * The window where dialogs and bubbles should be rendered.
  301. * @type {!Window}
  302. * @private
  303. */
  304. goog.editor.Field.prototype.appWindow_;
  305. /**
  306. * Target node to be used when dispatching SELECTIONCHANGE asynchronously on
  307. * mouseup (to avoid IE quirk). Should be set just before starting the timer and
  308. * nulled right after consuming.
  309. * @type {Node}
  310. * @private
  311. */
  312. goog.editor.Field.prototype.selectionChangeTarget_;
  313. /**
  314. * Flag controlling wether to capture mouse up events on the window or not.
  315. * @type {boolean}
  316. * @private
  317. */
  318. goog.editor.Field.prototype.useWindowMouseUp_ = false;
  319. /**
  320. * FLag indicating the handling of a mouse event sequence.
  321. * @type {boolean}
  322. * @private
  323. */
  324. goog.editor.Field.prototype.waitingForMouseUp_ = false;
  325. /**
  326. * Sets the active field id.
  327. * @param {?string} fieldId The active field id.
  328. */
  329. goog.editor.Field.setActiveFieldId = function(fieldId) {
  330. goog.editor.Field.activeFieldId_ = fieldId;
  331. };
  332. /**
  333. * @return {?string} The id of the active field.
  334. */
  335. goog.editor.Field.getActiveFieldId = function() {
  336. return goog.editor.Field.activeFieldId_;
  337. };
  338. /**
  339. * Sets flag to control whether to use window mouse up after seeing
  340. * a mouse down operation on the field.
  341. * @param {boolean} flag True to track window mouse up.
  342. */
  343. goog.editor.Field.prototype.setUseWindowMouseUp = function(flag) {
  344. goog.asserts.assert(
  345. !flag || !this.usesIframe(),
  346. 'procssing window mouse up should only be enabled when not using iframe');
  347. this.useWindowMouseUp_ = flag;
  348. };
  349. /**
  350. * @return {boolean} Whether we're in modal interaction mode. When this
  351. * returns true, another plugin is interacting with the field contents
  352. * in a synchronous way, and expects you not to make changes to
  353. * the field's DOM structure or selection.
  354. */
  355. goog.editor.Field.prototype.inModalMode = function() {
  356. return this.inModalMode_;
  357. };
  358. /**
  359. * @param {boolean} inModalMode Sets whether we're in modal interaction mode.
  360. */
  361. goog.editor.Field.prototype.setModalMode = function(inModalMode) {
  362. this.inModalMode_ = inModalMode;
  363. };
  364. /**
  365. * Returns a string usable as a hash code for this field. For field's
  366. * that were created with an id, the hash code is guaranteed to be the id.
  367. * TODO(user): I think we can get rid of this. Seems only used from editor.
  368. * @return {string} The hash code for this editable field.
  369. */
  370. goog.editor.Field.prototype.getHashCode = function() {
  371. return this.hashCode_;
  372. };
  373. /**
  374. * Returns the editable DOM element or null if this field
  375. * is not editable.
  376. * <p>On IE or Safari this is the element with contentEditable=true
  377. * (in whitebox mode, the iFrame body).
  378. * <p>On Gecko this is the iFrame body
  379. * TODO(user): How do we word this for subclass version?
  380. * @return {Element} The editable DOM element, defined as above.
  381. */
  382. goog.editor.Field.prototype.getElement = function() {
  383. return this.field;
  384. };
  385. /**
  386. * Returns original DOM element that is being made editable by Trogedit or
  387. * null if that element has not yet been found in the appropriate document.
  388. * @return {Element} The original element.
  389. */
  390. goog.editor.Field.prototype.getOriginalElement = function() {
  391. return this.originalElement;
  392. };
  393. /**
  394. * Registers a keyboard event listener on the field. This is necessary for
  395. * Gecko since the fields are contained in an iFrame and there is no way to
  396. * auto-propagate key events up to the main window.
  397. * @param {string|Array<string>} type Event type to listen for or array of
  398. * event types, for example goog.events.EventType.KEYDOWN.
  399. * @param {Function} listener Function to be used as the listener.
  400. * @param {boolean=} opt_capture Whether to use capture phase (optional,
  401. * defaults to false).
  402. * @param {Object=} opt_handler Object in whose scope to call the listener.
  403. */
  404. goog.editor.Field.prototype.addListener = function(
  405. type, listener, opt_capture, opt_handler) {
  406. var elem = this.getElement();
  407. // On Gecko, keyboard events only reliably fire on the document element when
  408. // using an iframe.
  409. if (goog.editor.BrowserFeature.USE_DOCUMENT_FOR_KEY_EVENTS && elem &&
  410. this.usesIframe()) {
  411. elem = elem.ownerDocument;
  412. }
  413. if (opt_handler) {
  414. this.eventRegister.listenWithScope(
  415. elem, type, listener, opt_capture, opt_handler);
  416. } else {
  417. this.eventRegister.listen(elem, type, listener, opt_capture);
  418. }
  419. };
  420. /**
  421. * Returns the registered plugin with the given classId.
  422. * @param {string} classId classId of the plugin.
  423. * @return {goog.editor.Plugin} Registered plugin with the given classId.
  424. */
  425. goog.editor.Field.prototype.getPluginByClassId = function(classId) {
  426. return this.plugins_[classId];
  427. };
  428. /**
  429. * Registers the plugin with the editable field.
  430. * @param {goog.editor.Plugin} plugin The plugin to register.
  431. */
  432. goog.editor.Field.prototype.registerPlugin = function(plugin) {
  433. var classId = plugin.getTrogClassId();
  434. if (this.plugins_[classId]) {
  435. goog.log.error(
  436. this.logger, 'Cannot register the same class of plugin twice.');
  437. }
  438. this.plugins_[classId] = plugin;
  439. // Only key events and execute should have these has* functions with a custom
  440. // handler array since they need to be very careful about performance.
  441. // The rest of the plugin hooks should be event-based.
  442. for (var op in goog.editor.Plugin.OPCODE) {
  443. var opcode = goog.editor.Plugin.OPCODE[op];
  444. if (plugin[opcode]) {
  445. this.indexedPlugins_[op].push(plugin);
  446. }
  447. }
  448. plugin.registerFieldObject(this);
  449. // By default we enable all plugins for fields that are currently loaded.
  450. if (this.isLoaded()) {
  451. plugin.enable(this);
  452. }
  453. };
  454. /**
  455. * Unregisters the plugin with this field.
  456. * @param {goog.editor.Plugin} plugin The plugin to unregister.
  457. */
  458. goog.editor.Field.prototype.unregisterPlugin = function(plugin) {
  459. var classId = plugin.getTrogClassId();
  460. if (!this.plugins_[classId]) {
  461. goog.log.error(
  462. this.logger, 'Cannot unregister a plugin that isn\'t registered.');
  463. }
  464. delete this.plugins_[classId];
  465. for (var op in goog.editor.Plugin.OPCODE) {
  466. var opcode = goog.editor.Plugin.OPCODE[op];
  467. if (plugin[opcode]) {
  468. goog.array.remove(this.indexedPlugins_[op], plugin);
  469. }
  470. }
  471. plugin.unregisterFieldObject(this);
  472. };
  473. /**
  474. * Sets the value that will replace the style attribute of this field's
  475. * element when the field is made non-editable. This method is called with the
  476. * current value of the style attribute when the field is made editable.
  477. * @param {string} cssText The value of the style attribute.
  478. */
  479. goog.editor.Field.prototype.setInitialStyle = function(cssText) {
  480. this.cssText = cssText;
  481. };
  482. /**
  483. * Reset the properties on the original field element to how it was before
  484. * it was made editable.
  485. */
  486. goog.editor.Field.prototype.resetOriginalElemProperties = function() {
  487. var field = this.getOriginalElement();
  488. field.removeAttribute('contentEditable');
  489. field.removeAttribute('g_editable');
  490. field.removeAttribute('role');
  491. if (!this.id) {
  492. field.removeAttribute('id');
  493. } else {
  494. field.id = this.id;
  495. }
  496. field.className = this.savedClassName_ || '';
  497. var cssText = this.cssText;
  498. if (!cssText) {
  499. field.removeAttribute('style');
  500. } else {
  501. goog.dom.setProperties(field, {'style': cssText});
  502. }
  503. if (goog.isString(this.originalFieldLineHeight_)) {
  504. goog.style.setStyle(field, 'lineHeight', this.originalFieldLineHeight_);
  505. this.originalFieldLineHeight_ = null;
  506. }
  507. };
  508. /**
  509. * Checks the modified state of the field.
  510. * Note: Changes that take place while the goog.editor.Field.EventType.CHANGE
  511. * event is stopped do not effect the modified state.
  512. * @param {boolean=} opt_useIsEverModified Set to true to check if the field
  513. * has ever been modified since it was created, otherwise checks if the field
  514. * has been modified since the last goog.editor.Field.EventType.DELAYEDCHANGE
  515. * event was dispatched.
  516. * @return {boolean} Whether the field has been modified.
  517. */
  518. goog.editor.Field.prototype.isModified = function(opt_useIsEverModified) {
  519. return opt_useIsEverModified ? this.isEverModified_ : this.isModified_;
  520. };
  521. /**
  522. * Number of milliseconds after a change when the change event should be fired.
  523. * @type {number}
  524. */
  525. goog.editor.Field.CHANGE_FREQUENCY = 15;
  526. /**
  527. * Number of milliseconds between delayed change events.
  528. * @type {number}
  529. */
  530. goog.editor.Field.DELAYED_CHANGE_FREQUENCY = 250;
  531. /**
  532. * @return {boolean} Whether the field is implemented as an iframe.
  533. */
  534. goog.editor.Field.prototype.usesIframe = goog.functions.TRUE;
  535. /**
  536. * @return {boolean} Whether the field should be rendered with a fixed
  537. * height, or should expand to fit its contents.
  538. */
  539. goog.editor.Field.prototype.isFixedHeight = goog.functions.TRUE;
  540. /**
  541. * @return {boolean} Whether the field should be refocused on input.
  542. * This is a workaround for the iOS bug that text input doesn't work
  543. * when the main window listens touch events.
  544. */
  545. goog.editor.Field.prototype.shouldRefocusOnInputMobileSafari =
  546. goog.functions.FALSE;
  547. /**
  548. * Map of keyCodes (not charCodes) that cause changes in the field contents.
  549. * @type {Object}
  550. * @private
  551. */
  552. goog.editor.Field.KEYS_CAUSING_CHANGES_ = {
  553. 46: true, // DEL
  554. 8: true // BACKSPACE
  555. };
  556. if (!goog.userAgent.IE) {
  557. // Only IE doesn't change the field by default upon tab.
  558. // TODO(user): This really isn't right now that we have tab plugins.
  559. goog.editor.Field.KEYS_CAUSING_CHANGES_[9] = true; // TAB
  560. }
  561. /**
  562. * Map of keyCodes (not charCodes) that when used in conjunction with the
  563. * Ctrl key cause changes in the field contents. These are the keys that are
  564. * not handled by basic formatting trogedit plugins.
  565. * @type {Object}
  566. * @private
  567. */
  568. goog.editor.Field.CTRL_KEYS_CAUSING_CHANGES_ = {
  569. 86: true, // V
  570. 88: true // X
  571. };
  572. if (goog.userAgent.WINDOWS && !goog.userAgent.GECKO) {
  573. // In IE and Webkit, input from IME (Input Method Editor) does not generate a
  574. // keypress event so we have to rely on the keydown event. This way we have
  575. // false positives while the user is using keyboard to select the
  576. // character to input, but it is still better than the false negatives
  577. // that ignores user's final input at all.
  578. goog.editor.Field.KEYS_CAUSING_CHANGES_[229] = true; // from IME;
  579. }
  580. /**
  581. * Returns true if the keypress generates a change in contents.
  582. * @param {goog.events.BrowserEvent} e The event.
  583. * @param {boolean} testAllKeys True to test for all types of generating keys.
  584. * False to test for only the keys found in
  585. * goog.editor.Field.KEYS_CAUSING_CHANGES_.
  586. * @return {boolean} Whether the keypress generates a change in contents.
  587. * @private
  588. */
  589. goog.editor.Field.isGeneratingKey_ = function(e, testAllKeys) {
  590. if (goog.editor.Field.isSpecialGeneratingKey_(e)) {
  591. return true;
  592. }
  593. return !!(
  594. testAllKeys && !(e.ctrlKey || e.metaKey) &&
  595. (!goog.userAgent.GECKO || e.charCode));
  596. };
  597. /**
  598. * Returns true if the keypress generates a change in the contents.
  599. * due to a special key listed in goog.editor.Field.KEYS_CAUSING_CHANGES_
  600. * @param {goog.events.BrowserEvent} e The event.
  601. * @return {boolean} Whether the keypress generated a change in the contents.
  602. * @private
  603. */
  604. goog.editor.Field.isSpecialGeneratingKey_ = function(e) {
  605. var testCtrlKeys = (e.ctrlKey || e.metaKey) &&
  606. e.keyCode in goog.editor.Field.CTRL_KEYS_CAUSING_CHANGES_;
  607. var testRegularKeys = !(e.ctrlKey || e.metaKey) &&
  608. e.keyCode in goog.editor.Field.KEYS_CAUSING_CHANGES_;
  609. return testCtrlKeys || testRegularKeys;
  610. };
  611. /**
  612. * Sets the application window.
  613. * @param {!Window} appWindow The window where dialogs and bubbles should be
  614. * rendered.
  615. */
  616. goog.editor.Field.prototype.setAppWindow = function(appWindow) {
  617. this.appWindow_ = appWindow;
  618. };
  619. /**
  620. * Returns the "application" window, where dialogs and bubbles
  621. * should be rendered.
  622. * @return {!Window} The window.
  623. */
  624. goog.editor.Field.prototype.getAppWindow = function() {
  625. return this.appWindow_;
  626. };
  627. /**
  628. * Sets the zIndex that the field should be based off of.
  629. * TODO(user): Get rid of this completely. Here for Sites.
  630. * Should this be set directly on UI plugins?
  631. *
  632. * @param {number} zindex The base zIndex of the editor.
  633. */
  634. goog.editor.Field.prototype.setBaseZindex = function(zindex) {
  635. this.baseZindex_ = zindex;
  636. };
  637. /**
  638. * Returns the zindex of the base level of the field.
  639. *
  640. * @return {number} The base zindex of the editor.
  641. */
  642. goog.editor.Field.prototype.getBaseZindex = function() {
  643. return this.baseZindex_ || 0;
  644. };
  645. /**
  646. * Sets up the field object and window util of this field, and enables this
  647. * editable field with all registered plugins.
  648. * This is essential to the initialization of the field.
  649. * It must be called when the field becomes fully loaded and editable.
  650. * @param {Element} field The field property.
  651. * @protected
  652. */
  653. goog.editor.Field.prototype.setupFieldObject = function(field) {
  654. this.loadState_ = goog.editor.Field.LoadState_.EDITABLE;
  655. this.field = field;
  656. this.editableDomHelper = goog.dom.getDomHelper(field);
  657. this.isModified_ = false;
  658. this.isEverModified_ = false;
  659. field.setAttribute('g_editable', 'true');
  660. goog.a11y.aria.setRole(field, goog.a11y.aria.Role.TEXTBOX);
  661. };
  662. /**
  663. * Help make the field not editable by setting internal data structures to null,
  664. * and disabling this field with all registered plugins.
  665. * @private
  666. */
  667. goog.editor.Field.prototype.tearDownFieldObject_ = function() {
  668. this.loadState_ = goog.editor.Field.LoadState_.UNEDITABLE;
  669. for (var classId in this.plugins_) {
  670. var plugin = this.plugins_[classId];
  671. if (!plugin.activeOnUneditableFields()) {
  672. plugin.disable(this);
  673. }
  674. }
  675. this.field = null;
  676. this.editableDomHelper = null;
  677. };
  678. /**
  679. * Initialize listeners on the field.
  680. * @private
  681. */
  682. goog.editor.Field.prototype.setupChangeListeners_ = function() {
  683. if ((goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) &&
  684. this.usesIframe() && this.shouldRefocusOnInputMobileSafari()) {
  685. // This is a workaround for the iOS bug that text input doesn't work
  686. // when the main window listens touch events.
  687. var editWindow = this.getEditableDomHelper().getWindow();
  688. this.boundRefocusListenerMobileSafari_ =
  689. goog.bind(editWindow.focus, editWindow);
  690. editWindow.addEventListener(
  691. goog.events.EventType.KEYDOWN, this.boundRefocusListenerMobileSafari_,
  692. false);
  693. editWindow.addEventListener(
  694. goog.events.EventType.TOUCHEND, this.boundRefocusListenerMobileSafari_,
  695. false);
  696. }
  697. if (goog.userAgent.OPERA && this.usesIframe()) {
  698. // We can't use addListener here because we need to listen on the window,
  699. // and removing listeners on window objects from the event register throws
  700. // an exception if the window is closed.
  701. this.boundFocusListenerOpera_ =
  702. goog.bind(this.dispatchFocusAndBeforeFocus_, this);
  703. this.boundBlurListenerOpera_ = goog.bind(this.dispatchBlur, this);
  704. var editWindow = this.getEditableDomHelper().getWindow();
  705. editWindow.addEventListener(
  706. goog.events.EventType.FOCUS, this.boundFocusListenerOpera_, false);
  707. editWindow.addEventListener(
  708. goog.events.EventType.BLUR, this.boundBlurListenerOpera_, false);
  709. } else {
  710. if (goog.editor.BrowserFeature.SUPPORTS_FOCUSIN) {
  711. this.addListener(goog.events.EventType.FOCUS, this.dispatchFocus_);
  712. this.addListener(
  713. goog.events.EventType.FOCUSIN, this.dispatchBeforeFocus_);
  714. } else {
  715. this.addListener(
  716. goog.events.EventType.FOCUS, this.dispatchFocusAndBeforeFocus_);
  717. }
  718. this.addListener(
  719. goog.events.EventType.BLUR, this.dispatchBlur,
  720. goog.editor.BrowserFeature.USE_MUTATION_EVENTS);
  721. }
  722. if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
  723. // Ways to detect changes in Mozilla:
  724. //
  725. // keypress - check event.charCode (only typable characters has a
  726. // charCode), but also keyboard commands lile Ctrl+C will
  727. // return a charCode.
  728. // dragdrop - fires when the user drops something. This does not necessary
  729. // lead to a change but we cannot detect if it will or not
  730. //
  731. // Known Issues: We cannot detect cut and paste using menus
  732. // We cannot detect when someone moves something out of the
  733. // field using drag and drop.
  734. //
  735. this.setupMutationEventHandlersGecko();
  736. } else {
  737. // Ways to detect that a change is about to happen in other browsers.
  738. // (IE and Safari have these events. Opera appears to work, but we haven't
  739. // researched it.)
  740. //
  741. // onbeforepaste
  742. // onbeforecut
  743. // ondrop - happens when the user drops something on the editable text
  744. // field the value at this time does not contain the dropped text
  745. // ondragleave - when the user drags something from the current document.
  746. // This might not cause a change if the action was copy
  747. // instead of move
  748. // onkeypress - IE only fires keypress events if the key will generate
  749. // output. It will not trigger for delete and backspace
  750. // onkeydown - For delete and backspace
  751. //
  752. // known issues: IE triggers beforepaste just by opening the edit menu
  753. // delete at the end should not cause beforechange
  754. // backspace at the beginning should not cause beforechange
  755. // see above in ondragleave
  756. // TODO(user): Why don't we dispatchBeforeChange from the
  757. // handleDrop event for all browsers?
  758. this.addListener(
  759. ['beforecut', 'beforepaste', 'drop', 'dragend'],
  760. this.dispatchBeforeChange);
  761. this.addListener(
  762. ['cut', 'paste'], goog.functions.lock(this.dispatchChange));
  763. this.addListener('drop', this.handleDrop_);
  764. }
  765. // TODO(user): Figure out why we use dragend vs dragdrop and
  766. // document this better.
  767. var dropEventName = goog.userAgent.WEBKIT ? 'dragend' : 'dragdrop';
  768. this.addListener(dropEventName, this.handleDrop_);
  769. this.addListener(goog.events.EventType.KEYDOWN, this.handleKeyDown_);
  770. this.addListener(goog.events.EventType.KEYPRESS, this.handleKeyPress_);
  771. this.addListener(goog.events.EventType.KEYUP, this.handleKeyUp_);
  772. this.selectionChangeTimer_ = new goog.async.Delay(
  773. this.handleSelectionChangeTimer_,
  774. goog.editor.Field.SELECTION_CHANGE_FREQUENCY_, this);
  775. if (this.followLinkInNewWindow_) {
  776. this.addListener(
  777. goog.events.EventType.CLICK, goog.editor.Field.cancelLinkClick_);
  778. }
  779. this.addListener(goog.events.EventType.MOUSEDOWN, this.handleMouseDown_);
  780. if (this.useWindowMouseUp_) {
  781. this.eventRegister.listen(
  782. this.editableDomHelper.getDocument(), goog.events.EventType.MOUSEUP,
  783. this.handleMouseUp_);
  784. this.addListener(goog.events.EventType.DRAGSTART, this.handleDragStart_);
  785. } else {
  786. this.addListener(goog.events.EventType.MOUSEUP, this.handleMouseUp_);
  787. }
  788. };
  789. /**
  790. * Frequency to check for selection changes.
  791. * @type {number}
  792. * @private
  793. */
  794. goog.editor.Field.SELECTION_CHANGE_FREQUENCY_ = 250;
  795. /**
  796. * Stops all listeners and timers.
  797. * @protected
  798. */
  799. goog.editor.Field.prototype.clearListeners = function() {
  800. if (this.eventRegister) {
  801. this.eventRegister.removeAll();
  802. }
  803. if ((goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) &&
  804. this.usesIframe() && this.shouldRefocusOnInputMobileSafari()) {
  805. try {
  806. var editWindow = this.getEditableDomHelper().getWindow();
  807. editWindow.removeEventListener(
  808. goog.events.EventType.KEYDOWN, this.boundRefocusListenerMobileSafari_,
  809. false);
  810. editWindow.removeEventListener(
  811. goog.events.EventType.TOUCHEND,
  812. this.boundRefocusListenerMobileSafari_, false);
  813. } catch (e) {
  814. // The editWindow no longer exists, or has been navigated to a different-
  815. // origin URL. Either way, the event listeners have already been removed
  816. // for us.
  817. }
  818. delete this.boundRefocusListenerMobileSafari_;
  819. }
  820. if (goog.userAgent.OPERA && this.usesIframe()) {
  821. try {
  822. var editWindow = this.getEditableDomHelper().getWindow();
  823. editWindow.removeEventListener(
  824. goog.events.EventType.FOCUS, this.boundFocusListenerOpera_, false);
  825. editWindow.removeEventListener(
  826. goog.events.EventType.BLUR, this.boundBlurListenerOpera_, false);
  827. } catch (e) {
  828. // The editWindow no longer exists, or has been navigated to a different-
  829. // origin URL. Either way, the event listeners have already been removed
  830. // for us.
  831. }
  832. delete this.boundFocusListenerOpera_;
  833. delete this.boundBlurListenerOpera_;
  834. }
  835. if (this.changeTimerGecko_) {
  836. this.changeTimerGecko_.stop();
  837. }
  838. this.delayedChangeTimer_.stop();
  839. };
  840. /** @override */
  841. goog.editor.Field.prototype.disposeInternal = function() {
  842. if (this.isLoading() || this.isLoaded()) {
  843. goog.log.warning(this.logger, 'Disposing a field that is in use.');
  844. }
  845. if (this.getOriginalElement()) {
  846. this.execCommand(goog.editor.Command.CLEAR_LOREM);
  847. }
  848. this.tearDownFieldObject_();
  849. this.clearListeners();
  850. this.clearFieldLoadListener_();
  851. this.originalDomHelper = null;
  852. if (this.eventRegister) {
  853. this.eventRegister.dispose();
  854. this.eventRegister = null;
  855. }
  856. this.removeAllWrappers();
  857. if (goog.editor.Field.getActiveFieldId() == this.id) {
  858. goog.editor.Field.setActiveFieldId(null);
  859. }
  860. for (var classId in this.plugins_) {
  861. var plugin = this.plugins_[classId];
  862. if (plugin.isAutoDispose()) {
  863. plugin.dispose();
  864. }
  865. }
  866. delete (this.plugins_);
  867. goog.editor.Field.superClass_.disposeInternal.call(this);
  868. };
  869. /**
  870. * Attach an wrapper to this field, to be thrown out when the field
  871. * is disposed.
  872. * @param {goog.Disposable} wrapper The wrapper to attach.
  873. */
  874. goog.editor.Field.prototype.attachWrapper = function(wrapper) {
  875. this.wrappers_.push(wrapper);
  876. };
  877. /**
  878. * Removes all wrappers and destroys them.
  879. */
  880. goog.editor.Field.prototype.removeAllWrappers = function() {
  881. var wrapper;
  882. while (wrapper = this.wrappers_.pop()) {
  883. wrapper.dispose();
  884. }
  885. };
  886. /**
  887. * Sets whether activating a hyperlink in this editable field will open a new
  888. * window or not.
  889. * @param {boolean} followLinkInNewWindow
  890. */
  891. goog.editor.Field.prototype.setFollowLinkInNewWindow = function(
  892. followLinkInNewWindow) {
  893. this.followLinkInNewWindow_ = followLinkInNewWindow;
  894. };
  895. /**
  896. * List of mutation events in Gecko browsers.
  897. * @type {Array<string>}
  898. * @protected
  899. */
  900. goog.editor.Field.MUTATION_EVENTS_GECKO = [
  901. 'DOMNodeInserted', 'DOMNodeRemoved', 'DOMNodeRemovedFromDocument',
  902. 'DOMNodeInsertedIntoDocument', 'DOMCharacterDataModified'
  903. ];
  904. /**
  905. * Mutation events tell us when something has changed for mozilla.
  906. * @protected
  907. */
  908. goog.editor.Field.prototype.setupMutationEventHandlersGecko = function() {
  909. // Always use DOMSubtreeModified on Gecko when not using an iframe so that
  910. // DOM mutations outside the Field do not trigger handleMutationEventGecko_.
  911. if (goog.editor.BrowserFeature.HAS_DOM_SUBTREE_MODIFIED_EVENT ||
  912. !this.usesIframe()) {
  913. this.eventRegister.listen(
  914. this.getElement(), 'DOMSubtreeModified',
  915. this.handleMutationEventGecko_);
  916. } else {
  917. var doc = this.getEditableDomHelper().getDocument();
  918. this.eventRegister.listen(
  919. doc, goog.editor.Field.MUTATION_EVENTS_GECKO,
  920. this.handleMutationEventGecko_, true);
  921. // DOMAttrModified fires for a lot of events we want to ignore. This goes
  922. // through a different handler so that we can ignore many of these.
  923. this.eventRegister.listen(
  924. doc, 'DOMAttrModified',
  925. goog.bind(
  926. this.handleDomAttrChange, this, this.handleMutationEventGecko_),
  927. true);
  928. }
  929. };
  930. /**
  931. * Handle before change key events and fire the beforetab event if appropriate.
  932. * This needs to happen on keydown in IE and keypress in FF.
  933. * @param {goog.events.BrowserEvent} e The browser event.
  934. * @return {boolean} Whether to still perform the default key action. Only set
  935. * to true if the actual event has already been canceled.
  936. * @private
  937. */
  938. goog.editor.Field.prototype.handleBeforeChangeKeyEvent_ = function(e) {
  939. // There are two reasons to block a key:
  940. var block =
  941. // #1: to intercept a tab
  942. // TODO: possibly don't allow clients to intercept tabs outside of LIs and
  943. // maybe tables as well?
  944. (e.keyCode == goog.events.KeyCodes.TAB && !this.dispatchBeforeTab_(e)) ||
  945. // #2: to block a Firefox-specific bug where Macs try to navigate
  946. // back a page when you hit command+left arrow or comamnd-right arrow.
  947. // See https://bugzilla.mozilla.org/show_bug.cgi?id=341886
  948. // This was fixed in Firefox 29, but still exists in older versions.
  949. (goog.userAgent.GECKO && e.metaKey &&
  950. !goog.userAgent.isVersionOrHigher(29) &&
  951. (e.keyCode == goog.events.KeyCodes.LEFT ||
  952. e.keyCode == goog.events.KeyCodes.RIGHT));
  953. if (block) {
  954. e.preventDefault();
  955. return false;
  956. } else {
  957. // In Gecko we have both keyCode and charCode. charCode is for human
  958. // readable characters like a, b and c. However pressing ctrl+c and so on
  959. // also causes charCode to be set.
  960. // TODO(arv): Del at end of field or backspace at beginning should be
  961. // ignored.
  962. this.gotGeneratingKey_ = e.charCode ||
  963. goog.editor.Field.isGeneratingKey_(e, goog.userAgent.GECKO);
  964. if (this.gotGeneratingKey_) {
  965. this.dispatchBeforeChange();
  966. // TODO(robbyw): Should we return the value of the above?
  967. }
  968. }
  969. return true;
  970. };
  971. /**
  972. * Keycodes that result in a selectionchange event (e.g. the cursor moving).
  973. * @type {!Object<number, number>}
  974. */
  975. goog.editor.Field.SELECTION_CHANGE_KEYCODES = {
  976. 8: 1, // backspace
  977. 9: 1, // tab
  978. 13: 1, // enter
  979. 33: 1, // page up
  980. 34: 1, // page down
  981. 35: 1, // end
  982. 36: 1, // home
  983. 37: 1, // left
  984. 38: 1, // up
  985. 39: 1, // right
  986. 40: 1, // down
  987. 46: 1 // delete
  988. };
  989. /**
  990. * Map of keyCodes (not charCodes) that when used in conjunction with the
  991. * Ctrl key cause selection changes in the field contents. These are the keys
  992. * that are not handled by the basic formatting trogedit plugins. Note that
  993. * combinations like Ctrl-left etc are already handled in
  994. * SELECTION_CHANGE_KEYCODES
  995. * @type {Object}
  996. * @private
  997. */
  998. goog.editor.Field.CTRL_KEYS_CAUSING_SELECTION_CHANGES_ = {
  999. 65: true, // A
  1000. 86: true, // V
  1001. 88: true // X
  1002. };
  1003. /**
  1004. * Map of keyCodes (not charCodes) that might need to be handled as a keyboard
  1005. * shortcut (even when ctrl/meta key is not pressed) by some plugin. Currently
  1006. * it is a small list. If it grows too big we can optimize it by using ranges
  1007. * or extending it from SELECTION_CHANGE_KEYCODES
  1008. * @type {Object}
  1009. * @private
  1010. */
  1011. goog.editor.Field.POTENTIAL_SHORTCUT_KEYCODES_ = {
  1012. 8: 1, // backspace
  1013. 9: 1, // tab
  1014. 13: 1, // enter
  1015. 27: 1, // esc
  1016. 33: 1, // page up
  1017. 34: 1, // page down
  1018. 37: 1, // left
  1019. 38: 1, // up
  1020. 39: 1, // right
  1021. 40: 1 // down
  1022. };
  1023. /**
  1024. * Calls all the plugins of the given operation, in sequence, with the
  1025. * given arguments. This is short-circuiting: once one plugin cancels
  1026. * the event, no more plugins will be invoked.
  1027. * @param {goog.editor.Plugin.Op} op A plugin op.
  1028. * @param {...*} var_args The arguments to the plugin.
  1029. * @return {boolean} True if one of the plugins cancel the event, false
  1030. * otherwise.
  1031. * @private
  1032. */
  1033. goog.editor.Field.prototype.invokeShortCircuitingOp_ = function(op, var_args) {
  1034. var plugins = this.indexedPlugins_[op];
  1035. var argList = goog.array.slice(arguments, 1);
  1036. for (var i = 0; i < plugins.length; ++i) {
  1037. // If the plugin returns true, that means it handled the event and
  1038. // we shouldn't propagate to the other plugins.
  1039. var plugin = plugins[i];
  1040. if ((plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) &&
  1041. plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList)) {
  1042. // Only one plugin is allowed to handle the event. If for some reason
  1043. // a plugin wants to handle it and still allow other plugins to handle
  1044. // it, it shouldn't return true.
  1045. return true;
  1046. }
  1047. }
  1048. return false;
  1049. };
  1050. /**
  1051. * Invoke this operation on all plugins with the given arguments.
  1052. * @param {goog.editor.Plugin.Op} op A plugin op.
  1053. * @param {...*} var_args The arguments to the plugin.
  1054. * @private
  1055. */
  1056. goog.editor.Field.prototype.invokeOp_ = function(op, var_args) {
  1057. var plugins = this.indexedPlugins_[op];
  1058. var argList = goog.array.slice(arguments, 1);
  1059. for (var i = 0; i < plugins.length; ++i) {
  1060. var plugin = plugins[i];
  1061. if (plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) {
  1062. plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList);
  1063. }
  1064. }
  1065. };
  1066. /**
  1067. * Reduce this argument over all plugins. The result of each plugin invocation
  1068. * will be passed to the next plugin invocation. See goog.array.reduce.
  1069. * @param {goog.editor.Plugin.Op} op A plugin op.
  1070. * @param {string} arg The argument to reduce. For now, we assume it's a
  1071. * string, but we should widen this later if there are reducing
  1072. * plugins that don't operate on strings.
  1073. * @param {...*} var_args Any extra arguments to pass to the plugin. These args
  1074. * will not be reduced.
  1075. * @return {string} The reduced argument.
  1076. * @private
  1077. */
  1078. goog.editor.Field.prototype.reduceOp_ = function(op, arg, var_args) {
  1079. var plugins = this.indexedPlugins_[op];
  1080. var argList = goog.array.slice(arguments, 1);
  1081. for (var i = 0; i < plugins.length; ++i) {
  1082. var plugin = plugins[i];
  1083. if (plugin.isEnabled(this) || goog.editor.Plugin.IRREPRESSIBLE_OPS[op]) {
  1084. argList[0] = plugin[goog.editor.Plugin.OPCODE[op]].apply(plugin, argList);
  1085. }
  1086. }
  1087. return argList[0];
  1088. };
  1089. /**
  1090. * Prepare the given contents, then inject them into the editable field.
  1091. * @param {?string} contents The contents to prepare.
  1092. * @param {Element} field The field element.
  1093. * @protected
  1094. */
  1095. goog.editor.Field.prototype.injectContents = function(contents, field) {
  1096. var styles = {};
  1097. var newHtml = this.getInjectableContents(contents, styles);
  1098. goog.style.setStyle(field, styles);
  1099. goog.editor.node.replaceInnerHtml(field, newHtml);
  1100. };
  1101. /**
  1102. * Returns prepared contents that can be injected into the editable field.
  1103. * @param {?string} contents The contents to prepare.
  1104. * @param {Object} styles A map that will be populated with styles that should
  1105. * be applied to the field element together with the contents.
  1106. * @return {string} The prepared contents.
  1107. */
  1108. goog.editor.Field.prototype.getInjectableContents = function(contents, styles) {
  1109. return this.reduceOp_(
  1110. goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, contents || '', styles);
  1111. };
  1112. /**
  1113. * Handles keydown on the field.
  1114. * @param {goog.events.BrowserEvent} e The browser event.
  1115. * @private
  1116. */
  1117. goog.editor.Field.prototype.handleKeyDown_ = function(e) {
  1118. // Mac only fires Cmd+A for keydown, not keyup: b/22407515.
  1119. if (goog.userAgent.MAC && e.keyCode == goog.events.KeyCodes.A) {
  1120. this.maybeStartSelectionChangeTimer_(e);
  1121. }
  1122. if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
  1123. if (!this.handleBeforeChangeKeyEvent_(e)) {
  1124. return;
  1125. }
  1126. }
  1127. if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYDOWN, e) &&
  1128. goog.editor.BrowserFeature.USES_KEYDOWN) {
  1129. this.handleKeyboardShortcut_(e);
  1130. }
  1131. };
  1132. /**
  1133. * Handles keypress on the field.
  1134. * @param {goog.events.BrowserEvent} e The browser event.
  1135. * @private
  1136. */
  1137. goog.editor.Field.prototype.handleKeyPress_ = function(e) {
  1138. if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
  1139. if (!this.handleBeforeChangeKeyEvent_(e)) {
  1140. return;
  1141. }
  1142. } else {
  1143. // In IE only keys that generate output trigger keypress
  1144. // In Mozilla charCode is set for keys generating content.
  1145. this.gotGeneratingKey_ = true;
  1146. this.dispatchBeforeChange();
  1147. }
  1148. if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYPRESS, e) &&
  1149. !goog.editor.BrowserFeature.USES_KEYDOWN) {
  1150. this.handleKeyboardShortcut_(e);
  1151. }
  1152. };
  1153. /**
  1154. * Handles keyup on the field.
  1155. * @param {!goog.events.BrowserEvent} e The browser event.
  1156. * @private
  1157. */
  1158. goog.editor.Field.prototype.handleKeyUp_ = function(e) {
  1159. if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS &&
  1160. (this.gotGeneratingKey_ ||
  1161. goog.editor.Field.isSpecialGeneratingKey_(e))) {
  1162. // The special keys won't have set the gotGeneratingKey flag, so we check
  1163. // for them explicitly
  1164. this.handleChange();
  1165. }
  1166. this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYUP, e);
  1167. this.maybeStartSelectionChangeTimer_(e);
  1168. };
  1169. /**
  1170. * Fires {@code BEFORESELECTIONCHANGE} and starts the selection change timer
  1171. * (which will fire {@code SELECTIONCHANGE}) if the given event is a key event
  1172. * that causes a selection change.
  1173. * @param {!goog.events.BrowserEvent} e The browser event.
  1174. * @private
  1175. */
  1176. goog.editor.Field.prototype.maybeStartSelectionChangeTimer_ = function(e) {
  1177. if (this.isEventStopped(goog.editor.Field.EventType.SELECTIONCHANGE)) {
  1178. return;
  1179. }
  1180. if (goog.editor.Field.SELECTION_CHANGE_KEYCODES[e.keyCode] ||
  1181. ((e.ctrlKey || e.metaKey) &&
  1182. goog.editor.Field.CTRL_KEYS_CAUSING_SELECTION_CHANGES_[e.keyCode])) {
  1183. this.dispatchEvent(goog.editor.Field.EventType.BEFORESELECTIONCHANGE);
  1184. this.selectionChangeTimer_.start();
  1185. }
  1186. };
  1187. /**
  1188. * Handles keyboard shortcuts on the field. Note that we bake this into our
  1189. * handleKeyPress/handleKeyDown rather than using goog.events.KeyHandler or
  1190. * goog.ui.KeyboardShortcutHandler for performance reasons. Since these
  1191. * are handled on every key stroke, we do not want to be going out to the
  1192. * event system every time.
  1193. * @param {goog.events.BrowserEvent} e The browser event.
  1194. * @private
  1195. */
  1196. goog.editor.Field.prototype.handleKeyboardShortcut_ = function(e) {
  1197. // Alt key is used for i18n languages to enter certain characters. like
  1198. // control + alt + z (used for IMEs) and control + alt + s for Polish.
  1199. // So we don't invoke handleKeyboardShortcut at all for alt keys.
  1200. if (e.altKey) {
  1201. return;
  1202. }
  1203. var isModifierPressed = goog.userAgent.MAC ? e.metaKey : e.ctrlKey;
  1204. if (isModifierPressed ||
  1205. goog.editor.Field.POTENTIAL_SHORTCUT_KEYCODES_[e.keyCode]) {
  1206. // TODO(user): goog.events.KeyHandler uses much more complicated logic
  1207. // to determine key. Consider changing to what they do.
  1208. var key = e.charCode || e.keyCode;
  1209. if (key == 17) { // Ctrl key
  1210. // In IE and Webkit pressing Ctrl key itself results in this event.
  1211. return;
  1212. }
  1213. var stringKey = String.fromCharCode(key).toLowerCase();
  1214. // Ctrl+Cmd+Space generates a charCode for a backtick on Mac Firefox, but
  1215. // has the correct string key in the browser event.
  1216. if (goog.userAgent.MAC && goog.userAgent.GECKO && stringKey == '`' &&
  1217. e.getBrowserEvent().key == ' ') {
  1218. stringKey = ' ';
  1219. }
  1220. // Converting the keyCode for "\" using fromCharCode creates "u", so we need
  1221. // to look out for it specifically.
  1222. if (e.keyCode == goog.events.KeyCodes.BACKSLASH) {
  1223. stringKey = '\\';
  1224. }
  1225. if (this.invokeShortCircuitingOp_(
  1226. goog.editor.Plugin.Op.SHORTCUT, e, stringKey, isModifierPressed)) {
  1227. e.preventDefault();
  1228. // We don't call stopPropagation as some other handler outside of
  1229. // trogedit might need it.
  1230. }
  1231. }
  1232. };
  1233. /**
  1234. * Executes an editing command as per the registered plugins.
  1235. * @param {string} command The command to execute.
  1236. * @param {...*} var_args Any additional parameters needed to execute the
  1237. * command.
  1238. * @return {*} False if the command wasn't handled, otherwise, the result of
  1239. * the command.
  1240. */
  1241. goog.editor.Field.prototype.execCommand = function(command, var_args) {
  1242. var args = arguments;
  1243. var result;
  1244. var plugins = this.indexedPlugins_[goog.editor.Plugin.Op.EXEC_COMMAND];
  1245. for (var i = 0; i < plugins.length; ++i) {
  1246. // If the plugin supports the command, that means it handled the
  1247. // event and we shouldn't propagate to the other plugins.
  1248. var plugin = plugins[i];
  1249. if (plugin.isEnabled(this) && plugin.isSupportedCommand(command)) {
  1250. result = plugin.execCommand.apply(plugin, args);
  1251. break;
  1252. }
  1253. }
  1254. return result;
  1255. };
  1256. /**
  1257. * Gets the value of command(s).
  1258. * @param {string|Array<string>} commands String name(s) of the command.
  1259. * @return {*} Value of each command. Returns false (or array of falses)
  1260. * if designMode is off or the field is otherwise uneditable, and
  1261. * there are no activeOnUneditable plugins for the command.
  1262. */
  1263. goog.editor.Field.prototype.queryCommandValue = function(commands) {
  1264. var isEditable = this.isLoaded() && this.isSelectionEditable();
  1265. if (goog.isString(commands)) {
  1266. return this.queryCommandValueInternal_(commands, isEditable);
  1267. } else {
  1268. var state = {};
  1269. for (var i = 0; i < commands.length; i++) {
  1270. state[commands[i]] =
  1271. this.queryCommandValueInternal_(commands[i], isEditable);
  1272. }
  1273. return state;
  1274. }
  1275. };
  1276. /**
  1277. * Gets the value of this command.
  1278. * @param {string} command The command to check.
  1279. * @param {boolean} isEditable Whether the field is currently editable.
  1280. * @return {*} The state of this command. Null if not handled.
  1281. * False if the field is uneditable and there are no handlers for
  1282. * uneditable commands.
  1283. * @private
  1284. */
  1285. goog.editor.Field.prototype.queryCommandValueInternal_ = function(
  1286. command, isEditable) {
  1287. var plugins = this.indexedPlugins_[goog.editor.Plugin.Op.QUERY_COMMAND];
  1288. for (var i = 0; i < plugins.length; ++i) {
  1289. var plugin = plugins[i];
  1290. if (plugin.isEnabled(this) && plugin.isSupportedCommand(command) &&
  1291. (isEditable || plugin.activeOnUneditableFields())) {
  1292. return plugin.queryCommandValue(command);
  1293. }
  1294. }
  1295. return isEditable ? null : false;
  1296. };
  1297. /**
  1298. * Fires a change event only if the attribute change effects the editiable
  1299. * field. We ignore events that are internal browser events (ie scrollbar
  1300. * state change)
  1301. * @param {Function} handler The function to call if this is not an internal
  1302. * browser event.
  1303. * @param {goog.events.BrowserEvent} browserEvent The browser event.
  1304. * @protected
  1305. */
  1306. goog.editor.Field.prototype.handleDomAttrChange = function(
  1307. handler, browserEvent) {
  1308. if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) {
  1309. return;
  1310. }
  1311. var e = browserEvent.getBrowserEvent();
  1312. // For XUL elements, since we don't care what they are doing
  1313. try {
  1314. if (e.originalTarget.prefix ||
  1315. /** @type {!Element} */ (e.originalTarget).nodeName == 'scrollbar') {
  1316. return;
  1317. }
  1318. } catch (ex1) {
  1319. // Some XUL nodes don't like you reading their properties. If we got
  1320. // the exception, this implies a XUL node so we can return.
  1321. return;
  1322. }
  1323. // Check if prev and new values are different, sometimes this fires when
  1324. // nothing has really changed.
  1325. if (e.prevValue == e.newValue) {
  1326. return;
  1327. }
  1328. handler.call(this, e);
  1329. };
  1330. /**
  1331. * Handle a mutation event.
  1332. * @param {goog.events.BrowserEvent|Event} e The browser event.
  1333. * @private
  1334. */
  1335. goog.editor.Field.prototype.handleMutationEventGecko_ = function(e) {
  1336. if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) {
  1337. return;
  1338. }
  1339. e = e.getBrowserEvent ? e.getBrowserEvent() : e;
  1340. // For people with firebug, firebug sets this property on elements it is
  1341. // inserting into the dom.
  1342. if (e.target.firebugIgnore) {
  1343. return;
  1344. }
  1345. this.isModified_ = true;
  1346. this.isEverModified_ = true;
  1347. this.changeTimerGecko_.start();
  1348. };
  1349. /**
  1350. * Handle drop events. Deal with focus/selection issues and set the document
  1351. * as changed.
  1352. * @param {goog.events.BrowserEvent} e The browser event.
  1353. * @private
  1354. */
  1355. goog.editor.Field.prototype.handleDrop_ = function(e) {
  1356. if (goog.userAgent.IE) {
  1357. // TODO(user): This should really be done in the loremipsum plugin.
  1358. this.execCommand(goog.editor.Command.CLEAR_LOREM, true);
  1359. }
  1360. // TODO(user): I just moved this code to this location, but I wonder why
  1361. // it is only done for this case. Investigate.
  1362. if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
  1363. this.dispatchFocusAndBeforeFocus_();
  1364. }
  1365. this.dispatchChange();
  1366. };
  1367. /**
  1368. * @return {HTMLIFrameElement} The iframe that's body is editable.
  1369. * @protected
  1370. */
  1371. goog.editor.Field.prototype.getEditableIframe = function() {
  1372. var dh;
  1373. if (this.usesIframe() && (dh = this.getEditableDomHelper())) {
  1374. // If the iframe has been destroyed, the dh could still exist since the
  1375. // node may not be gc'ed, but fetching the window can fail.
  1376. var win = dh.getWindow();
  1377. return /** @type {HTMLIFrameElement} */ (win && win.frameElement);
  1378. }
  1379. return null;
  1380. };
  1381. /**
  1382. * @return {goog.dom.DomHelper?} The dom helper for the editable node.
  1383. */
  1384. goog.editor.Field.prototype.getEditableDomHelper = function() {
  1385. return this.editableDomHelper;
  1386. };
  1387. /**
  1388. * @return {goog.dom.AbstractRange?} Closure range object wrapping the selection
  1389. * in this field or null if this field is not currently editable.
  1390. */
  1391. goog.editor.Field.prototype.getRange = function() {
  1392. var win = this.editableDomHelper && this.editableDomHelper.getWindow();
  1393. return win && goog.dom.Range.createFromWindow(win);
  1394. };
  1395. /**
  1396. * Dispatch a selection change event, optionally caused by the given browser
  1397. * event or selecting the given target.
  1398. * @param {goog.events.BrowserEvent=} opt_e Optional browser event causing this
  1399. * event.
  1400. * @param {Node=} opt_target The node the selection changed to.
  1401. */
  1402. goog.editor.Field.prototype.dispatchSelectionChangeEvent = function(
  1403. opt_e, opt_target) {
  1404. if (this.isEventStopped(goog.editor.Field.EventType.SELECTIONCHANGE)) {
  1405. return;
  1406. }
  1407. // The selection is editable only if the selection is inside the
  1408. // editable field.
  1409. var range = this.getRange();
  1410. var rangeContainer = range && range.getContainerElement();
  1411. this.isSelectionEditable_ =
  1412. !!rangeContainer && goog.dom.contains(this.getElement(), rangeContainer);
  1413. this.dispatchCommandValueChange();
  1414. this.dispatchEvent({
  1415. type: goog.editor.Field.EventType.SELECTIONCHANGE,
  1416. originalType: opt_e && opt_e.type
  1417. });
  1418. this.invokeShortCircuitingOp_(
  1419. goog.editor.Plugin.Op.SELECTION, opt_e, opt_target);
  1420. };
  1421. /**
  1422. * Dispatch a selection change event using a browser event that was
  1423. * asynchronously saved earlier.
  1424. * @private
  1425. */
  1426. goog.editor.Field.prototype.handleSelectionChangeTimer_ = function() {
  1427. var t = this.selectionChangeTarget_;
  1428. this.selectionChangeTarget_ = null;
  1429. this.dispatchSelectionChangeEvent(undefined, t);
  1430. };
  1431. /**
  1432. * This dispatches the beforechange event on the editable field
  1433. */
  1434. goog.editor.Field.prototype.dispatchBeforeChange = function() {
  1435. if (this.isEventStopped(goog.editor.Field.EventType.BEFORECHANGE)) {
  1436. return;
  1437. }
  1438. this.dispatchEvent(goog.editor.Field.EventType.BEFORECHANGE);
  1439. };
  1440. /**
  1441. * This dispatches the beforetab event on the editable field. If this event is
  1442. * cancelled, then the default tab behavior is prevented.
  1443. * @param {goog.events.BrowserEvent} e The tab event.
  1444. * @private
  1445. * @return {boolean} The result of dispatchEvent.
  1446. */
  1447. goog.editor.Field.prototype.dispatchBeforeTab_ = function(e) {
  1448. return this.dispatchEvent({
  1449. type: goog.editor.Field.EventType.BEFORETAB,
  1450. shiftKey: e.shiftKey,
  1451. altKey: e.altKey,
  1452. ctrlKey: e.ctrlKey
  1453. });
  1454. };
  1455. /**
  1456. * Temporarily ignore change events. If the time has already been set, it will
  1457. * fire immediately now. Further setting of the timer is stopped and
  1458. * dispatching of events is stopped until startChangeEvents is called.
  1459. * @param {boolean=} opt_stopChange Whether to ignore base change events.
  1460. * @param {boolean=} opt_stopDelayedChange Whether to ignore delayed change
  1461. * events.
  1462. */
  1463. goog.editor.Field.prototype.stopChangeEvents = function(
  1464. opt_stopChange, opt_stopDelayedChange) {
  1465. if (opt_stopChange) {
  1466. if (this.changeTimerGecko_) {
  1467. this.changeTimerGecko_.fireIfActive();
  1468. }
  1469. this.stopEvent(goog.editor.Field.EventType.CHANGE);
  1470. }
  1471. if (opt_stopDelayedChange) {
  1472. this.clearDelayedChange();
  1473. this.stopEvent(goog.editor.Field.EventType.DELAYEDCHANGE);
  1474. }
  1475. };
  1476. /**
  1477. * Start change events again and fire once if desired.
  1478. * @param {boolean=} opt_fireChange Whether to fire the change event
  1479. * immediately.
  1480. * @param {boolean=} opt_fireDelayedChange Whether to fire the delayed change
  1481. * event immediately.
  1482. */
  1483. goog.editor.Field.prototype.startChangeEvents = function(
  1484. opt_fireChange, opt_fireDelayedChange) {
  1485. if (!opt_fireChange && this.changeTimerGecko_) {
  1486. // In the case where change events were stopped and we're not firing
  1487. // them on start, the user was trying to suppress all change or delayed
  1488. // change events. Clear the change timer now while the events are still
  1489. // stopped so that its firing doesn't fire a stopped change event, or
  1490. // queue up a delayed change event that we were trying to stop.
  1491. this.changeTimerGecko_.fireIfActive();
  1492. }
  1493. this.startEvent(goog.editor.Field.EventType.CHANGE);
  1494. this.startEvent(goog.editor.Field.EventType.DELAYEDCHANGE);
  1495. if (opt_fireChange) {
  1496. this.handleChange();
  1497. }
  1498. if (opt_fireDelayedChange) {
  1499. this.dispatchDelayedChange_();
  1500. }
  1501. };
  1502. /**
  1503. * Stops the event of the given type from being dispatched.
  1504. * @param {goog.editor.Field.EventType} eventType type of event to stop.
  1505. */
  1506. goog.editor.Field.prototype.stopEvent = function(eventType) {
  1507. this.stoppedEvents_[eventType] = 1;
  1508. };
  1509. /**
  1510. * Re-starts the event of the given type being dispatched, if it had
  1511. * previously been stopped with stopEvent().
  1512. * @param {goog.editor.Field.EventType} eventType type of event to start.
  1513. */
  1514. goog.editor.Field.prototype.startEvent = function(eventType) {
  1515. // Toggling this bit on/off instead of deleting it/re-adding it
  1516. // saves array allocations.
  1517. this.stoppedEvents_[eventType] = 0;
  1518. };
  1519. /**
  1520. * Block an event for a short amount of time. Intended
  1521. * for the situation where an event pair fires in quick succession
  1522. * (e.g., mousedown/mouseup, keydown/keyup, focus/blur),
  1523. * and we want the second event in the pair to get "debounced."
  1524. *
  1525. * WARNING: This should never be used to solve race conditions or for
  1526. * mission-critical actions. It should only be used for UI improvements,
  1527. * where it's okay if the behavior is non-deterministic.
  1528. *
  1529. * @param {goog.editor.Field.EventType} eventType type of event to debounce.
  1530. */
  1531. goog.editor.Field.prototype.debounceEvent = function(eventType) {
  1532. this.debouncedEvents_[eventType] = goog.now();
  1533. };
  1534. /**
  1535. * Checks if the event of the given type has stopped being dispatched
  1536. * @param {goog.editor.Field.EventType} eventType type of event to check.
  1537. * @return {boolean} true if the event has been stopped with stopEvent().
  1538. * @protected
  1539. */
  1540. goog.editor.Field.prototype.isEventStopped = function(eventType) {
  1541. return !!this.stoppedEvents_[eventType] ||
  1542. (this.debouncedEvents_[eventType] &&
  1543. (goog.now() - this.debouncedEvents_[eventType] <=
  1544. goog.editor.Field.DEBOUNCE_TIME_MS_));
  1545. };
  1546. /**
  1547. * Calls a function to manipulate the dom of this field. This method should be
  1548. * used whenever Trogedit clients need to modify the dom of the field, so that
  1549. * delayed change events are handled appropriately. Extra delayed change events
  1550. * will cause undesired states to be added to the undo-redo stack. This method
  1551. * will always fire at most one delayed change event, depending on the value of
  1552. * {@code opt_preventDelayedChange}.
  1553. *
  1554. * @param {function()} func The function to call that will manipulate the dom.
  1555. * @param {boolean=} opt_preventDelayedChange Whether delayed change should be
  1556. * prevented after calling {@code func}. Defaults to always firing
  1557. * delayed change.
  1558. * @param {Object=} opt_handler Object in whose scope to call the listener.
  1559. */
  1560. goog.editor.Field.prototype.manipulateDom = function(
  1561. func, opt_preventDelayedChange, opt_handler) {
  1562. this.stopChangeEvents(true, true);
  1563. // We don't want any problems with the passed in function permanently
  1564. // stopping change events. That would break Trogedit.
  1565. try {
  1566. func.call(opt_handler);
  1567. } finally {
  1568. // If the field isn't loaded then change and delayed change events will be
  1569. // started as part of the onload behavior.
  1570. if (this.isLoaded()) {
  1571. // We assume that func always modified the dom and so fire a single change
  1572. // event. Delayed change is only fired if not prevented by the user.
  1573. if (opt_preventDelayedChange) {
  1574. this.startEvent(goog.editor.Field.EventType.CHANGE);
  1575. this.handleChange();
  1576. this.startEvent(goog.editor.Field.EventType.DELAYEDCHANGE);
  1577. } else {
  1578. this.dispatchChange();
  1579. }
  1580. }
  1581. }
  1582. };
  1583. /**
  1584. * Dispatches a command value change event.
  1585. * @param {Array<string>=} opt_commands Commands whose state has
  1586. * changed.
  1587. */
  1588. goog.editor.Field.prototype.dispatchCommandValueChange = function(
  1589. opt_commands) {
  1590. if (opt_commands) {
  1591. this.dispatchEvent({
  1592. type: goog.editor.Field.EventType.COMMAND_VALUE_CHANGE,
  1593. commands: opt_commands
  1594. });
  1595. } else {
  1596. this.dispatchEvent(goog.editor.Field.EventType.COMMAND_VALUE_CHANGE);
  1597. }
  1598. };
  1599. /**
  1600. * Dispatches the appropriate set of change events. This only fires
  1601. * synchronous change events in blended-mode, iframe-using mozilla. It just
  1602. * starts the appropriate timer for goog.editor.Field.EventType.DELAYEDCHANGE.
  1603. * This also starts up change events again if they were stopped.
  1604. *
  1605. * @param {boolean=} opt_noDelay True if
  1606. * goog.editor.Field.EventType.DELAYEDCHANGE should be fired syncronously.
  1607. */
  1608. goog.editor.Field.prototype.dispatchChange = function(opt_noDelay) {
  1609. this.startChangeEvents(true, opt_noDelay);
  1610. };
  1611. /**
  1612. * Handle a change in the Editable Field. Marks the field has modified,
  1613. * dispatches the change event on the editable field (moz only), starts the
  1614. * timer for the delayed change event. Note that these actions only occur if
  1615. * the proper events are not stopped.
  1616. */
  1617. goog.editor.Field.prototype.handleChange = function() {
  1618. if (this.isEventStopped(goog.editor.Field.EventType.CHANGE)) {
  1619. return;
  1620. }
  1621. // Clear the changeTimerGecko_ if it's active, since any manual call to
  1622. // handle change is equiavlent to changeTimerGecko_.fire().
  1623. if (this.changeTimerGecko_) {
  1624. this.changeTimerGecko_.stop();
  1625. }
  1626. this.isModified_ = true;
  1627. this.isEverModified_ = true;
  1628. if (this.isEventStopped(goog.editor.Field.EventType.DELAYEDCHANGE)) {
  1629. return;
  1630. }
  1631. this.delayedChangeTimer_.start();
  1632. };
  1633. /**
  1634. * Dispatch a delayed change event.
  1635. * @private
  1636. */
  1637. goog.editor.Field.prototype.dispatchDelayedChange_ = function() {
  1638. if (this.isEventStopped(goog.editor.Field.EventType.DELAYEDCHANGE)) {
  1639. return;
  1640. }
  1641. // Clear the delayedChangeTimer_ if it's active, since any manual call to
  1642. // dispatchDelayedChange_ is equivalent to delayedChangeTimer_.fire().
  1643. this.delayedChangeTimer_.stop();
  1644. this.isModified_ = false;
  1645. this.dispatchEvent(goog.editor.Field.EventType.DELAYEDCHANGE);
  1646. };
  1647. /**
  1648. * Don't wait for the timer and just fire the delayed change event if it's
  1649. * pending.
  1650. */
  1651. goog.editor.Field.prototype.clearDelayedChange = function() {
  1652. // The changeTimerGecko_ will queue up a delayed change so to fully clear
  1653. // delayed change we must also clear this timer.
  1654. if (this.changeTimerGecko_) {
  1655. this.changeTimerGecko_.fireIfActive();
  1656. }
  1657. this.delayedChangeTimer_.fireIfActive();
  1658. };
  1659. /**
  1660. * Dispatch beforefocus and focus for FF. Note that both of these actually
  1661. * happen in the document's "focus" event. Unfortunately, we don't actually
  1662. * have a way of getting in before the focus event in FF (boo! hiss!).
  1663. * In IE, we use onfocusin for before focus and onfocus for focus.
  1664. * @private
  1665. */
  1666. goog.editor.Field.prototype.dispatchFocusAndBeforeFocus_ = function() {
  1667. this.dispatchBeforeFocus_();
  1668. this.dispatchFocus_();
  1669. };
  1670. /**
  1671. * Dispatches a before focus event.
  1672. * @private
  1673. */
  1674. goog.editor.Field.prototype.dispatchBeforeFocus_ = function() {
  1675. if (this.isEventStopped(goog.editor.Field.EventType.BEFOREFOCUS)) {
  1676. return;
  1677. }
  1678. this.execCommand(goog.editor.Command.CLEAR_LOREM, true);
  1679. this.dispatchEvent(goog.editor.Field.EventType.BEFOREFOCUS);
  1680. };
  1681. /**
  1682. * Dispatches a focus event.
  1683. * @private
  1684. */
  1685. goog.editor.Field.prototype.dispatchFocus_ = function() {
  1686. if (this.isEventStopped(goog.editor.Field.EventType.FOCUS)) {
  1687. return;
  1688. }
  1689. goog.editor.Field.setActiveFieldId(this.id);
  1690. this.isSelectionEditable_ = true;
  1691. this.dispatchEvent(goog.editor.Field.EventType.FOCUS);
  1692. if (goog.editor.BrowserFeature
  1693. .PUTS_CURSOR_BEFORE_FIRST_BLOCK_ELEMENT_ON_FOCUS) {
  1694. // If the cursor is at the beginning of the field, make sure that it is
  1695. // in the first user-visible line break, e.g.,
  1696. // no selection: <div><p>...</p></div> --> <div><p>|cursor|...</p></div>
  1697. // <div>|cursor|<p>...</p></div> --> <div><p>|cursor|...</p></div>
  1698. // <body>|cursor|<p>...</p></body> --> <body><p>|cursor|...</p></body>
  1699. var field = this.getElement();
  1700. var range = this.getRange();
  1701. if (range) {
  1702. var focusNode = /** @type {!Element} */ (range.getFocusNode());
  1703. if (range.getFocusOffset() == 0 &&
  1704. (!focusNode || focusNode == field ||
  1705. focusNode.tagName == goog.dom.TagName.BODY)) {
  1706. goog.editor.range.selectNodeStart(field);
  1707. }
  1708. }
  1709. }
  1710. if (!goog.editor.BrowserFeature.CLEARS_SELECTION_WHEN_FOCUS_LEAVES &&
  1711. this.usesIframe()) {
  1712. var parent = this.getEditableDomHelper().getWindow().parent;
  1713. parent.getSelection().removeAllRanges();
  1714. }
  1715. };
  1716. /**
  1717. * Dispatches a blur event.
  1718. * @protected
  1719. */
  1720. goog.editor.Field.prototype.dispatchBlur = function() {
  1721. if (this.isEventStopped(goog.editor.Field.EventType.BLUR)) {
  1722. return;
  1723. }
  1724. // Another field may have already been registered as active, so only
  1725. // clear out the active field id if we still think this field is active.
  1726. if (goog.editor.Field.getActiveFieldId() == this.id) {
  1727. goog.editor.Field.setActiveFieldId(null);
  1728. }
  1729. this.isSelectionEditable_ = false;
  1730. this.dispatchEvent(goog.editor.Field.EventType.BLUR);
  1731. };
  1732. /**
  1733. * @return {boolean} Whether the selection is editable.
  1734. */
  1735. goog.editor.Field.prototype.isSelectionEditable = function() {
  1736. return this.isSelectionEditable_;
  1737. };
  1738. /**
  1739. * Event handler for clicks in browsers that will follow a link when the user
  1740. * clicks, even if it's editable. We stop the click manually
  1741. * @param {goog.events.BrowserEvent} e The event.
  1742. * @private
  1743. */
  1744. goog.editor.Field.cancelLinkClick_ = function(e) {
  1745. if (goog.dom.getAncestorByTagNameAndClass(
  1746. /** @type {Node} */ (e.target), goog.dom.TagName.A)) {
  1747. e.preventDefault();
  1748. }
  1749. };
  1750. /**
  1751. * Handle mouse down inside the editable field.
  1752. * @param {goog.events.BrowserEvent} e The event.
  1753. * @private
  1754. */
  1755. goog.editor.Field.prototype.handleMouseDown_ = function(e) {
  1756. goog.editor.Field.setActiveFieldId(this.id);
  1757. // Open links in a new window if the user control + clicks.
  1758. if (goog.userAgent.IE) {
  1759. var targetElement = e.target;
  1760. if (targetElement &&
  1761. /** @type {!Element} */ (targetElement).tagName == goog.dom.TagName.A &&
  1762. e.ctrlKey) {
  1763. this.originalDomHelper.getWindow().open(targetElement.href);
  1764. }
  1765. }
  1766. this.waitingForMouseUp_ = true;
  1767. };
  1768. /**
  1769. * Handle drag start. Needs to cancel listening for the mouse up event on the
  1770. * window.
  1771. * @param {goog.events.BrowserEvent} e The event.
  1772. * @private
  1773. */
  1774. goog.editor.Field.prototype.handleDragStart_ = function(e) {
  1775. this.waitingForMouseUp_ = false;
  1776. };
  1777. /**
  1778. * Handle mouse up inside the editable field.
  1779. * @param {goog.events.BrowserEvent} e The event.
  1780. * @private
  1781. */
  1782. goog.editor.Field.prototype.handleMouseUp_ = function(e) {
  1783. if (this.useWindowMouseUp_ && !this.waitingForMouseUp_) {
  1784. return;
  1785. }
  1786. this.waitingForMouseUp_ = false;
  1787. /*
  1788. * We fire a selection change event immediately for listeners that depend on
  1789. * the native browser event object (e). On IE, a listener that tries to
  1790. * retrieve the selection with goog.dom.Range may see an out-of-date
  1791. * selection range.
  1792. */
  1793. this.dispatchEvent(goog.editor.Field.EventType.BEFORESELECTIONCHANGE);
  1794. this.dispatchSelectionChangeEvent(e);
  1795. if (goog.userAgent.IE) {
  1796. /*
  1797. * Fire a second selection change event for listeners that need an
  1798. * up-to-date selection range. Save the event's target to be sent with it
  1799. * (it's safer than saving a copy of the event itself).
  1800. */
  1801. this.selectionChangeTarget_ = /** @type {Node} */ (e.target);
  1802. this.selectionChangeTimer_.start();
  1803. }
  1804. };
  1805. /**
  1806. * Retrieve the HTML contents of a field.
  1807. *
  1808. * Do NOT just get the innerHTML of a field directly--there's a lot of
  1809. * processing that needs to happen.
  1810. * @return {string} The scrubbed contents of the field.
  1811. */
  1812. goog.editor.Field.prototype.getCleanContents = function() {
  1813. if (this.queryCommandValue(goog.editor.Command.USING_LOREM)) {
  1814. return goog.string.Unicode.NBSP;
  1815. }
  1816. if (!this.isLoaded()) {
  1817. // The field is uneditable, so it's ok to read contents directly.
  1818. var elem = this.getOriginalElement();
  1819. if (!elem) {
  1820. goog.log.log(
  1821. this.logger, goog.log.Level.SHOUT,
  1822. "Couldn't get the field element to read the contents");
  1823. }
  1824. return elem.innerHTML;
  1825. }
  1826. var fieldCopy = this.getFieldCopy();
  1827. // Allow the plugins to handle their cleanup.
  1828. this.invokeOp_(goog.editor.Plugin.Op.CLEAN_CONTENTS_DOM, fieldCopy);
  1829. return this.reduceOp_(
  1830. goog.editor.Plugin.Op.CLEAN_CONTENTS_HTML, fieldCopy.innerHTML);
  1831. };
  1832. /**
  1833. * Get the copy of the editable field element, which has the innerHTML set
  1834. * correctly.
  1835. * @return {!Element} The copy of the editable field.
  1836. * @protected
  1837. */
  1838. goog.editor.Field.prototype.getFieldCopy = function() {
  1839. var field = this.getElement();
  1840. // Deep cloneNode strips some script tag contents in IE, so we do this.
  1841. var fieldCopy = /** @type {Element} */ (field.cloneNode(false));
  1842. // For some reason, when IE sets innerHtml of the cloned node, it strips
  1843. // script tags that fall at the beginning of an element. Appending a
  1844. // non-breaking space prevents this.
  1845. var html = field.innerHTML;
  1846. if (goog.userAgent.IE && html.match(/^\s*<script/i)) {
  1847. html = goog.string.Unicode.NBSP + html;
  1848. }
  1849. fieldCopy.innerHTML = html;
  1850. return fieldCopy;
  1851. };
  1852. /**
  1853. * Sets the contents of the field.
  1854. * @param {boolean} addParas Boolean to specify whether to add paragraphs
  1855. * to long fields.
  1856. * @param {?string} html html to insert. If html=null, then this defaults
  1857. * to a nbsp for mozilla and an empty string for IE.
  1858. * @param {boolean=} opt_dontFireDelayedChange True to make this content change
  1859. * not fire a delayed change event.
  1860. * @param {boolean=} opt_applyLorem Whether to apply lorem ipsum styles.
  1861. * @deprecated Use setSafeHtml instead.
  1862. */
  1863. goog.editor.Field.prototype.setHtml = function(
  1864. addParas, html, opt_dontFireDelayedChange, opt_applyLorem) {
  1865. var safeHtml =
  1866. html ? goog.html.legacyconversions.safeHtmlFromString(html) : null;
  1867. this.setSafeHtml(
  1868. addParas, safeHtml, opt_dontFireDelayedChange, opt_applyLorem);
  1869. };
  1870. /**
  1871. * Sets the contents of the field.
  1872. * @param {boolean} addParas Boolean to specify whether to add paragraphs
  1873. * to long fields.
  1874. * @param {?goog.html.SafeHtml} html html to insert. If html=null, then this
  1875. * defaults to a nsbp for mozilla and an empty string for IE.
  1876. * @param {boolean=} opt_dontFireDelayedChange True to make this content change
  1877. * not fire a delayed change event.
  1878. * @param {boolean=} opt_applyLorem Whether to apply lorem ipsum styles.
  1879. */
  1880. goog.editor.Field.prototype.setSafeHtml = function(
  1881. addParas, html, opt_dontFireDelayedChange, opt_applyLorem) {
  1882. if (this.isLoading()) {
  1883. goog.log.error(this.logger, "Can't set html while loading Trogedit");
  1884. return;
  1885. }
  1886. // Clear the lorem ipsum style, always.
  1887. if (opt_applyLorem) {
  1888. this.execCommand(goog.editor.Command.CLEAR_LOREM);
  1889. }
  1890. if (html && addParas) {
  1891. html = goog.html.SafeHtml.create('p', {}, html);
  1892. }
  1893. // If we don't want change events to fire, we have to turn off change events
  1894. // before setting the field contents, since that causes mutation events.
  1895. if (opt_dontFireDelayedChange) {
  1896. this.stopChangeEvents(false, true);
  1897. }
  1898. this.setInnerHtml_(html);
  1899. // Set the lorem ipsum style, if the element is empty.
  1900. if (opt_applyLorem) {
  1901. this.execCommand(goog.editor.Command.UPDATE_LOREM);
  1902. }
  1903. // TODO(user): This check should probably be moved to isEventStopped and
  1904. // startEvent.
  1905. if (this.isLoaded()) {
  1906. if (opt_dontFireDelayedChange) { // Turn back on change events
  1907. // We must fire change timer if necessary before restarting change events!
  1908. // Otherwise, the change timer firing after we restart events will cause
  1909. // the delayed change we were trying to stop. Flow:
  1910. // Stop delayed change
  1911. // setInnerHtml_, this starts the change timer
  1912. // start delayed change
  1913. // change timer fires
  1914. // starts delayed change timer since event was not stopped
  1915. // delayed change fires for the delayed change we tried to stop.
  1916. if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
  1917. this.changeTimerGecko_.fireIfActive();
  1918. }
  1919. this.startChangeEvents();
  1920. } else { // Mark the document as changed and fire change events.
  1921. this.dispatchChange();
  1922. }
  1923. }
  1924. };
  1925. /**
  1926. * Sets the inner HTML of the field. Works on both editable and
  1927. * uneditable fields.
  1928. * @param {?goog.html.SafeHtml} html The new inner HTML of the field.
  1929. * @private
  1930. */
  1931. goog.editor.Field.prototype.setInnerHtml_ = function(html) {
  1932. var field = this.getElement();
  1933. if (field) {
  1934. // Safari will put <style> tags into *new* <head> elements. When setting
  1935. // HTML, we need to remove these spare <head>s to make sure there's a
  1936. // clean slate, but keep the first <head>.
  1937. // Note: We punt on this issue for the non iframe case since
  1938. // we don't want to screw with the main document.
  1939. if (this.usesIframe() && goog.editor.BrowserFeature.MOVES_STYLE_TO_HEAD) {
  1940. var heads = goog.dom.getElementsByTagName(
  1941. goog.dom.TagName.HEAD, goog.asserts.assert(field.ownerDocument));
  1942. for (var i = heads.length - 1; i >= 1; --i) {
  1943. heads[i].parentNode.removeChild(heads[i]);
  1944. }
  1945. }
  1946. } else {
  1947. field = this.getOriginalElement();
  1948. }
  1949. if (field) {
  1950. this.injectContents(html && goog.html.SafeHtml.unwrap(html), field);
  1951. }
  1952. };
  1953. /**
  1954. * Attemps to turn on designMode for a document. This function can fail under
  1955. * certain circumstances related to the load event, and will throw an exception.
  1956. * @protected
  1957. */
  1958. goog.editor.Field.prototype.turnOnDesignModeGecko = function() {
  1959. var doc = this.getEditableDomHelper().getDocument();
  1960. // NOTE(nicksantos): This will fail under certain conditions, like
  1961. // when the node has display: none. It's up to clients to ensure that
  1962. // their fields are valid when they try to make them editable.
  1963. doc.designMode = 'on';
  1964. if (goog.editor.BrowserFeature.HAS_STYLE_WITH_CSS) {
  1965. doc.execCommand('styleWithCSS', false, false);
  1966. }
  1967. };
  1968. /**
  1969. * Installs styles if needed. Only writes styles when they can't be written
  1970. * inline directly into the field.
  1971. * @protected
  1972. */
  1973. goog.editor.Field.prototype.installStyles = function() {
  1974. if (this.cssStyles && this.shouldLoadAsynchronously()) {
  1975. goog.style.installSafeStyleSheet(
  1976. goog.html.legacyconversions.safeStyleSheetFromString(this.cssStyles),
  1977. this.getElement());
  1978. }
  1979. };
  1980. /**
  1981. * Signal that the field is loaded and ready to use. Change events now are
  1982. * in effect.
  1983. * @private
  1984. */
  1985. goog.editor.Field.prototype.dispatchLoadEvent_ = function() {
  1986. this.getElement();
  1987. this.installStyles();
  1988. this.startChangeEvents();
  1989. goog.log.info(this.logger, 'Dispatching load ' + this.id);
  1990. this.dispatchEvent(goog.editor.Field.EventType.LOAD);
  1991. };
  1992. /**
  1993. * @return {boolean} Whether the field is uneditable.
  1994. */
  1995. goog.editor.Field.prototype.isUneditable = function() {
  1996. return this.loadState_ == goog.editor.Field.LoadState_.UNEDITABLE;
  1997. };
  1998. /**
  1999. * @return {boolean} Whether the field has finished loading.
  2000. */
  2001. goog.editor.Field.prototype.isLoaded = function() {
  2002. return this.loadState_ == goog.editor.Field.LoadState_.EDITABLE;
  2003. };
  2004. /**
  2005. * @return {boolean} Whether the field is in the process of loading.
  2006. */
  2007. goog.editor.Field.prototype.isLoading = function() {
  2008. return this.loadState_ == goog.editor.Field.LoadState_.LOADING;
  2009. };
  2010. /**
  2011. * Gives the field focus.
  2012. */
  2013. goog.editor.Field.prototype.focus = function() {
  2014. if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE && this.usesIframe()) {
  2015. // In designMode, only the window itself can be focused; not the element.
  2016. this.getEditableDomHelper().getWindow().focus();
  2017. } else {
  2018. if (goog.userAgent.OPERA) {
  2019. // Opera will scroll to the bottom of the focused document, even
  2020. // if it is contained in an iframe that is scrolled to the top and
  2021. // the bottom flows past the end of it. To prevent this,
  2022. // save the scroll position of the document containing the editor
  2023. // iframe, then restore it after the focus.
  2024. var scrollX = this.appWindow_.pageXOffset;
  2025. var scrollY = this.appWindow_.pageYOffset;
  2026. }
  2027. this.getElement().focus();
  2028. if (goog.userAgent.OPERA) {
  2029. this.appWindow_.scrollTo(
  2030. /** @type {number} */ (scrollX), /** @type {number} */ (scrollY));
  2031. }
  2032. }
  2033. };
  2034. /**
  2035. * Gives the field focus and places the cursor at the start of the field.
  2036. */
  2037. goog.editor.Field.prototype.focusAndPlaceCursorAtStart = function() {
  2038. // NOTE(user): Excluding Gecko to maintain existing behavior post refactoring
  2039. // placeCursorAtStart into its own method. In Gecko browsers that currently
  2040. // have a selection the existing selection will be restored, otherwise it
  2041. // will go to the start.
  2042. // TODO(user): Refactor the code using this and related methods. We should
  2043. // only mess with the selection in the case where there is not an existing
  2044. // selection in the field.
  2045. if (goog.editor.BrowserFeature.HAS_IE_RANGES || !goog.userAgent.GECKO) {
  2046. this.placeCursorAtStart();
  2047. }
  2048. this.focus();
  2049. };
  2050. /**
  2051. * Place the cursor at the start of this field. It's recommended that you only
  2052. * use this method (and manipulate the selection in general) when there is not
  2053. * an existing selection in the field.
  2054. */
  2055. goog.editor.Field.prototype.placeCursorAtStart = function() {
  2056. this.placeCursorAtStartOrEnd_(true);
  2057. };
  2058. /**
  2059. * Place the cursor at the start of this field. It's recommended that you only
  2060. * use this method (and manipulate the selection in general) when there is not
  2061. * an existing selection in the field.
  2062. */
  2063. goog.editor.Field.prototype.placeCursorAtEnd = function() {
  2064. this.placeCursorAtStartOrEnd_(false);
  2065. };
  2066. /**
  2067. * Helper method to place the cursor at the start or end of this field.
  2068. * @param {boolean} isStart True for start, false for end.
  2069. * @private
  2070. */
  2071. goog.editor.Field.prototype.placeCursorAtStartOrEnd_ = function(isStart) {
  2072. var field = this.getElement();
  2073. if (field) {
  2074. var cursorPosition = isStart ? goog.editor.node.getLeftMostLeaf(field) :
  2075. goog.editor.node.getRightMostLeaf(field);
  2076. if (field == cursorPosition) {
  2077. // The rightmost leaf we found was the field element itself (which likely
  2078. // means the field element is empty). We can't place the cursor next to
  2079. // the field element, so just place it at the beginning.
  2080. goog.dom.Range.createCaret(field, 0).select();
  2081. } else {
  2082. goog.editor.range.placeCursorNextTo(cursorPosition, isStart);
  2083. }
  2084. this.dispatchSelectionChangeEvent();
  2085. }
  2086. };
  2087. /**
  2088. * Restore a saved range, and set the focus on the field.
  2089. * If no range is specified, we simply set the focus.
  2090. * @param {goog.dom.SavedRange=} opt_range A previously saved selected range.
  2091. */
  2092. goog.editor.Field.prototype.restoreSavedRange = function(opt_range) {
  2093. if (opt_range) {
  2094. opt_range.restore();
  2095. }
  2096. this.focus();
  2097. };
  2098. /**
  2099. * Makes a field editable.
  2100. *
  2101. * @param {!goog.html.TrustedResourceUrl|string=} opt_iframeSrc URL to set the
  2102. * iframe src to if necessary.
  2103. */
  2104. goog.editor.Field.prototype.makeEditable = function(opt_iframeSrc) {
  2105. this.loadState_ = goog.editor.Field.LoadState_.LOADING;
  2106. var field = this.getOriginalElement();
  2107. // TODO: In the fieldObj, save the field's id, className, cssText
  2108. // in order to reset it on closeField. That way, we can muck with the field's
  2109. // css, id, class and restore to how it was at the end.
  2110. this.nodeName = field.nodeName;
  2111. this.savedClassName_ = field.className;
  2112. this.setInitialStyle(field.style.cssText);
  2113. goog.dom.classlist.add(field, 'editable');
  2114. var iframeSrc;
  2115. if (goog.isString(opt_iframeSrc)) {
  2116. iframeSrc =
  2117. goog.html.legacyconversions.trustedResourceUrlFromString(opt_iframeSrc);
  2118. } else {
  2119. iframeSrc = opt_iframeSrc;
  2120. }
  2121. this.makeEditableInternal(iframeSrc);
  2122. };
  2123. /**
  2124. * Handles actually making something editable - creating necessary nodes,
  2125. * injecting content, etc.
  2126. * @param {!goog.html.TrustedResourceUrl=} opt_iframeSrc URL to set the iframe
  2127. * src to if necessary.
  2128. * @protected
  2129. */
  2130. goog.editor.Field.prototype.makeEditableInternal = function(opt_iframeSrc) {
  2131. this.makeIframeField_(opt_iframeSrc);
  2132. };
  2133. /**
  2134. * Handle the loading of the field (e.g. once the field is ready to setup).
  2135. * TODO(user): this should probably just be moved into dispatchLoadEvent_.
  2136. * @protected
  2137. */
  2138. goog.editor.Field.prototype.handleFieldLoad = function() {
  2139. if (goog.userAgent.IE) {
  2140. // This sometimes fails if the selection is invalid. This can happen, for
  2141. // example, if you attach a CLICK handler to the field that causes the
  2142. // field to be removed from the DOM and replaced with an editor
  2143. // -- however, listening to another event like MOUSEDOWN does not have this
  2144. // issue since no mouse selection has happened at that time.
  2145. goog.dom.Range.clearSelection(this.editableDomHelper.getWindow());
  2146. }
  2147. if (goog.editor.Field.getActiveFieldId() != this.id) {
  2148. this.execCommand(goog.editor.Command.UPDATE_LOREM);
  2149. }
  2150. this.setupChangeListeners_();
  2151. this.dispatchLoadEvent_();
  2152. // Enabling plugins after we fire the load event so that clients have a
  2153. // chance to set initial field contents before we start mucking with
  2154. // everything.
  2155. for (var classId in this.plugins_) {
  2156. this.plugins_[classId].enable(this);
  2157. }
  2158. };
  2159. /**
  2160. * Closes the field and cancels all pending change timers. Note that this
  2161. * means that if a change event has not fired yet, it will not fire. Clients
  2162. * should check fieldOj.isModified() if they depend on the final change event.
  2163. * Throws an error if the field is already uneditable.
  2164. *
  2165. * @param {boolean=} opt_skipRestore True to prevent copying of editable field
  2166. * contents back into the original node.
  2167. */
  2168. goog.editor.Field.prototype.makeUneditable = function(opt_skipRestore) {
  2169. if (this.isUneditable()) {
  2170. throw Error('makeUneditable: Field is already uneditable');
  2171. }
  2172. // Fire any events waiting on a timeout.
  2173. // Clearing delayed change also clears changeTimerGecko_.
  2174. this.clearDelayedChange();
  2175. this.selectionChangeTimer_.fireIfActive();
  2176. this.execCommand(goog.editor.Command.CLEAR_LOREM);
  2177. var html = null;
  2178. if (!opt_skipRestore && this.getElement()) {
  2179. // Rest of cleanup is simpler if field was never initialized.
  2180. html = this.getCleanContents();
  2181. }
  2182. // First clean up anything that happens in makeFieldEditable
  2183. // (i.e. anything that needs cleanup even if field has not loaded).
  2184. this.clearFieldLoadListener_();
  2185. var field = this.getOriginalElement();
  2186. if (goog.editor.Field.getActiveFieldId() == field.id) {
  2187. goog.editor.Field.setActiveFieldId(null);
  2188. }
  2189. // Clear all listeners before removing the nodes from the dom - if
  2190. // there are listeners on the iframe window, Firefox throws errors trying
  2191. // to unlisten once the iframe is no longer in the dom.
  2192. this.clearListeners();
  2193. // For fields that have loaded, clean up anything that happened in
  2194. // handleFieldOpen or later.
  2195. // If html is provided, copy it back and reset the properties on the field
  2196. // so that the original node will have the same properties as it did before
  2197. // it was made editable.
  2198. if (goog.isString(html)) {
  2199. goog.editor.node.replaceInnerHtml(field, html);
  2200. this.resetOriginalElemProperties();
  2201. }
  2202. this.restoreDom();
  2203. this.tearDownFieldObject_();
  2204. // On Safari, make sure to un-focus the field so that the
  2205. // native "current field" highlight style gets removed.
  2206. if (goog.userAgent.WEBKIT) {
  2207. field.blur();
  2208. }
  2209. this.execCommand(goog.editor.Command.UPDATE_LOREM);
  2210. this.dispatchEvent(goog.editor.Field.EventType.UNLOAD);
  2211. };
  2212. /**
  2213. * Restores the dom to how it was before being made editable.
  2214. * @protected
  2215. */
  2216. goog.editor.Field.prototype.restoreDom = function() {
  2217. // TODO(user): Consider only removing the iframe if we are
  2218. // restoring the original node, aka, if opt_html.
  2219. var field = this.getOriginalElement();
  2220. // TODO(robbyw): Consider throwing an error if !field.
  2221. if (field) {
  2222. // If the field is in the process of loading when it starts getting torn
  2223. // up, the iframe will not exist.
  2224. var iframe = this.getEditableIframe();
  2225. if (iframe) {
  2226. goog.dom.replaceNode(field, iframe);
  2227. }
  2228. }
  2229. };
  2230. /**
  2231. * Returns true if the field needs to be loaded asynchrnously.
  2232. * @return {boolean} True if loads are async.
  2233. * @protected
  2234. */
  2235. goog.editor.Field.prototype.shouldLoadAsynchronously = function() {
  2236. if (!goog.isDef(this.isHttps_)) {
  2237. this.isHttps_ = false;
  2238. if (goog.userAgent.IE && this.usesIframe()) {
  2239. // IE iframes need to load asynchronously if they are in https as we need
  2240. // to set an actual src on the iframe and wait for it to load.
  2241. // Find the top-most window we have access to and see if it's https.
  2242. // Technically this could fail if we have an http frame in an https frame
  2243. // on the same domain (or vice versa), but walking up the window hierarchy
  2244. // to find the first window that has an http* protocol seems like
  2245. // overkill.
  2246. var win = this.originalDomHelper.getWindow();
  2247. while (win != win.parent) {
  2248. try {
  2249. win = win.parent;
  2250. } catch (e) {
  2251. break;
  2252. }
  2253. }
  2254. var loc = win.location;
  2255. this.isHttps_ =
  2256. loc.protocol == 'https:' && loc.search.indexOf('nocheckhttps') == -1;
  2257. }
  2258. }
  2259. return this.isHttps_;
  2260. };
  2261. /**
  2262. * Start the editable iframe creation process for Mozilla or IE whitebox.
  2263. * The iframes load asynchronously.
  2264. *
  2265. * @param {!goog.html.TrustedResourceUrl=} opt_iframeSrc URL to set the iframe
  2266. * src to if necessary.
  2267. * @private
  2268. */
  2269. goog.editor.Field.prototype.makeIframeField_ = function(opt_iframeSrc) {
  2270. var field = this.getOriginalElement();
  2271. // TODO(robbyw): Consider throwing an error if !field.
  2272. if (field) {
  2273. var html = field.innerHTML;
  2274. // Invoke prepareContentsHtml on all plugins to prepare html for editing.
  2275. // Make sure this is done before calling this.attachFrame which removes the
  2276. // original element from DOM tree. Plugins may assume that the original
  2277. // element is still in its original position in DOM.
  2278. var styles = {};
  2279. html = this.reduceOp_(
  2280. goog.editor.Plugin.Op.PREPARE_CONTENTS_HTML, html, styles);
  2281. var iframe = this.originalDomHelper.createDom(
  2282. goog.dom.TagName.IFRAME, this.getIframeAttributes());
  2283. // TODO(nicksantos): Figure out if this is ever needed in SAFARI?
  2284. // In IE over HTTPS we need to wait for a load event before we set up the
  2285. // iframe, this is to prevent a security prompt or access is denied
  2286. // errors.
  2287. // NOTE(user): This hasn't been confirmed. isHttps_ allows a query
  2288. // param, nocheckhttps, which we can use to ascertain if this is actually
  2289. // needed. It was originally thought to be needed for IE6 SP1, but
  2290. // errors have been seen in IE7 as well.
  2291. if (this.shouldLoadAsynchronously()) {
  2292. // onLoad is the function to call once the iframe is ready to continue
  2293. // loading.
  2294. var onLoad =
  2295. goog.bind(this.iframeFieldLoadHandler, this, iframe, html, styles);
  2296. this.fieldLoadListenerKey_ =
  2297. goog.events.listen(iframe, goog.events.EventType.LOAD, onLoad, true);
  2298. if (opt_iframeSrc) {
  2299. goog.dom.safe.setIframeSrc(iframe, opt_iframeSrc);
  2300. }
  2301. }
  2302. this.attachIframe(iframe);
  2303. // Only continue if its not IE HTTPS in which case we're waiting for load.
  2304. if (!this.shouldLoadAsynchronously()) {
  2305. this.iframeFieldLoadHandler(iframe, html, styles);
  2306. }
  2307. }
  2308. };
  2309. /**
  2310. * Given the original field element, and the iframe that is destined to
  2311. * become the editable field, styles them appropriately and add the iframe
  2312. * to the dom.
  2313. *
  2314. * @param {HTMLIFrameElement} iframe The iframe element.
  2315. * @protected
  2316. */
  2317. goog.editor.Field.prototype.attachIframe = function(iframe) {
  2318. var field = this.getOriginalElement();
  2319. // TODO(user): Why do we do these two lines .. and why whitebox only?
  2320. iframe.className = field.className;
  2321. iframe.id = field.id;
  2322. goog.dom.replaceNode(iframe, field);
  2323. };
  2324. /**
  2325. * @param {Object} extraStyles A map of extra styles.
  2326. * @return {!goog.editor.icontent.FieldFormatInfo} The FieldFormatInfo
  2327. * object for this field's configuration.
  2328. * @protected
  2329. */
  2330. goog.editor.Field.prototype.getFieldFormatInfo = function(extraStyles) {
  2331. var originalElement = this.getOriginalElement();
  2332. var isStandardsMode = goog.editor.node.isStandardsMode(originalElement);
  2333. return new goog.editor.icontent.FieldFormatInfo(
  2334. this.id, isStandardsMode, false, false, extraStyles);
  2335. };
  2336. /**
  2337. * Writes the html content into the iframe. Handles writing any aditional
  2338. * styling as well.
  2339. * @param {HTMLIFrameElement} iframe Iframe to write contents into.
  2340. * @param {string} innerHtml The html content to write into the iframe.
  2341. * @param {Object} extraStyles A map of extra style attributes.
  2342. * @protected
  2343. */
  2344. goog.editor.Field.prototype.writeIframeContent = function(
  2345. iframe, innerHtml, extraStyles) {
  2346. var formatInfo = this.getFieldFormatInfo(extraStyles);
  2347. if (this.shouldLoadAsynchronously()) {
  2348. var doc = goog.dom.getFrameContentDocument(iframe);
  2349. goog.editor.icontent.writeHttpsInitialIframe(formatInfo, doc, innerHtml);
  2350. } else {
  2351. var styleInfo = new goog.editor.icontent.FieldStyleInfo(
  2352. this.getElement(), this.cssStyles);
  2353. goog.editor.icontent.writeNormalInitialIframe(
  2354. formatInfo, innerHtml, styleInfo, iframe);
  2355. }
  2356. };
  2357. /**
  2358. * The function to call when the editable iframe loads.
  2359. *
  2360. * @param {HTMLIFrameElement} iframe Iframe that just loaded.
  2361. * @param {string} innerHtml Html to put inside the body of the iframe.
  2362. * @param {Object} styles Property-value map of CSS styles to install on
  2363. * editable field.
  2364. * @protected
  2365. */
  2366. goog.editor.Field.prototype.iframeFieldLoadHandler = function(
  2367. iframe, innerHtml, styles) {
  2368. this.clearFieldLoadListener_();
  2369. iframe.allowTransparency = 'true';
  2370. this.writeIframeContent(iframe, innerHtml, styles);
  2371. var doc = goog.dom.getFrameContentDocument(iframe);
  2372. // Make sure to get this pointer after the doc.write as the doc.write
  2373. // clobbers all the document contents.
  2374. var body = doc.body;
  2375. this.setupFieldObject(body);
  2376. if (!goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE && this.usesIframe()) {
  2377. this.turnOnDesignModeGecko();
  2378. }
  2379. this.handleFieldLoad();
  2380. };
  2381. /**
  2382. * Clears fieldLoadListener for a field. Must be called even (especially?) if
  2383. * the field is not yet loaded and therefore not in this.fieldMap_
  2384. * @private
  2385. */
  2386. goog.editor.Field.prototype.clearFieldLoadListener_ = function() {
  2387. if (this.fieldLoadListenerKey_) {
  2388. goog.events.unlistenByKey(this.fieldLoadListenerKey_);
  2389. this.fieldLoadListenerKey_ = null;
  2390. }
  2391. };
  2392. /**
  2393. * @return {!Object} Get the HTML attributes for this field's iframe.
  2394. * @protected
  2395. */
  2396. goog.editor.Field.prototype.getIframeAttributes = function() {
  2397. var iframeStyle = 'padding:0;' + this.getOriginalElement().style.cssText;
  2398. if (!goog.string.endsWith(iframeStyle, ';')) {
  2399. iframeStyle += ';';
  2400. }
  2401. iframeStyle += 'background-color:white;';
  2402. // Ensure that the iframe has default overflow styling. If overflow is
  2403. // set to auto, an IE rendering bug can occur when it tries to render a
  2404. // table at the very bottom of the field, such that the table would cause
  2405. // a scrollbar, that makes the entire field go blank.
  2406. if (goog.userAgent.IE) {
  2407. iframeStyle += 'overflow:visible;';
  2408. }
  2409. return {'frameBorder': 0, 'style': iframeStyle};
  2410. };