MCPcopy
hub / github.com/ajaxorg/ace / setupContainer

Function setupContainer

src/ext/textarea.js:83–164  ·  view source on GitHub ↗
(element, getValue)

Source from the content-addressed store, hash-verified

81}
82
83function setupContainer(element, getValue) {
84 if (element.type != 'textarea') {
85 throw new Error("Textarea required!");
86 }
87
88 var parentNode = element.parentNode;
89
90 // This will hold the editor.
91 var container = document.createElement('div');
92
93 // To put Ace in the place of the textarea, we have to copy a few of the
94 // textarea's style attributes to the div container.
95 //
96 // The problem is that the properties have to get computed (they might be
97 // defined by a CSS file on the page - you can't access such rules that
98 // apply to an element via elm.style). Computed properties are converted to
99 // pixels although the dimension might be given as percentage. When the
100 // window resizes, the dimensions defined by percentages changes, so the
101 // properties have to get recomputed to get the new/true pixels.
102 var resizeEvent = function() {
103 var style = 'position:relative;';
104 [
105 'margin-top', 'margin-left', 'margin-right', 'margin-bottom'
106 ].forEach(function(item) {
107 style += item + ':' +
108 getCSSProperty(element, container, item) + ';';
109 });
110
111 // Calculating the width/height of the textarea is somewhat tricky. To
112 // do it right, you have to include the paddings to the sides as well
113 // (eg. width = width + padding-left, -right). This works well, as
114 // long as the width of the element is not set or given in pixels. In
115 // this case and after the textarea is hidden, getCSSProperty(element,
116 // container, 'width') will still return pixel value. If the element
117 // has realtiv dimensions (e.g. width='95<percent>')
118 // getCSSProperty(...) will return pixel values only as long as the
119 // textarea is visible. After it is hidden getCSSProperty will return
120 // the relative dimensions as they are set on the element (in the case
121 // of width, 95<percent>).
122 // Making the sum of pixel vaules (e.g. padding) and realtive values
123 // (e.g. <percent>) is not possible. As such the padding styles are
124 // ignored.
125
126 // The complete width is the width of the textarea + the padding
127 // to the left and right.
128 var width = getCSSProperty(element, container, 'width') || (element.clientWidth + "px");
129 var height = getCSSProperty(element, container, 'height') || (element.clientHeight + "px");
130 style += 'height:' + height + ';width:' + width + ';';
131
132 // Set the display property to 'inline-block'.
133 style += 'display:inline-block;';
134 container.style.cssText = style;
135 };
136 event.addListener(window, 'resize', resizeEvent);
137
138 // Call the resizeEvent once, so that the size of the container is
139 // calculated.
140 resizeEvent();

Callers 1

textarea.jsFile · 0.85

Calls 4

resizeEventFunction · 0.85
addListenerMethod · 0.80
getValueFunction · 0.50
toUpperCaseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…