Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

371 linhas
8.1 KiB

  1. Javascript Utilities
  2. ====================
  3. AJAX - Server Calls
  4. -------------------
  5. .. function:: $c(command, args, call_back, on_timeout, no_spinner, freeze_msg)
  6. call a function on the server, where
  7. * `cmd` is the command to the handler
  8. * `args` dictionary of arguments (form data)
  9. * `call_back` - function to be called on complete
  10. * `on_timeout` - function to be called on timeout
  11. * `no_spinner` - do not show the "Loading..." spinner
  12. * `freeze_msg` - freeze the user page while showing with the given message
  13. .. function:: $c_obj(doclist, method, arg, call_back, no_spinner, freeze_msg)
  14. call an object on the server, where:
  15. * `doclist` - the doclist of the calling doc or a string with the name of a Control Doctype
  16. * `method` - method to be called
  17. * `arg` - string argument
  18. * `call_back` - function to be called on completion
  19. * `no_spinner` - do not show the "Loading..." spinner
  20. * `freeze_msg` - freeze the user page while showing with the given message
  21. .. function:: $c_obj_csv(doclist, method, arg, call_back, no_spinner, freeze_msg)
  22. call an object on the server and return the output as CSV.
  23. *Note:* There will be no callback. The output must be a list-in-a-list
  24. .. function:: $c_js(path, callback)
  25. Load a Javascript library. Path must be relative to the js folder. For example: `widgets/calendar.js`
  26. Title
  27. -----
  28. .. data:: title_prefix
  29. Standard prefix to the title
  30. .. function:: set_title(t)
  31. set Page `title`, if `title_prefix` is set, then appends it to the prefix
  32. Events
  33. ------
  34. .. function:: addEvent(ev, fn) {
  35. Add a listener to the given event. Example::
  36. addEvent('click', function(e, target) { .. });
  37. String
  38. ------
  39. .. function:: clean_smart_quotes(s)
  40. Returns string with MS Word "Smart" quotes removed
  41. .. function:: replace_newlines(t)
  42. Replaces newline charcter \\n with '<br>'
  43. .. function:: esc_quotes(s)
  44. Returns string with single quote ' escaped
  45. .. function:: strip(s, chars)
  46. Python-like function returns string with leading and lagging characters from `chars` removed.
  47. If `chars` is null, removes whitespace.
  48. .. function:: lstrip(s, chars)
  49. Strips `chars` from left side
  50. .. function:: rstrip(s, chars)
  51. Strips `chars` from right side
  52. .. function:: repl_all(s, s1, s2) {
  53. Replaces all `s1` to `s2` in `s`
  54. .. function:: repl(s, dict)
  55. Python-like string replacement. Example::
  56. s = repl("Hello %(name)s, welcome to %(location)s", {name:'ABC', location:'Mumbai'});
  57. .. function:: esc_quotes(s)
  58. Returns string with single quote ' escaped
  59. .. function:: strip(s, chars)
  60. Python-like function returns string with leading and lagging characters from `chars` removed.
  61. If `chars` is null, removes whitespace.
  62. .. function:: lstrip(s, chars)
  63. Strips `chars` from left side
  64. .. function:: rstrip(s, chars)
  65. Strips `chars` from right side
  66. .. function:: repl_all(s, s1, s2) {
  67. Replaces all `s1` to `s2` in `s`
  68. .. function:: repl(s, dict)
  69. Python-like string replacement. Example::
  70. s = repl("Hello %(name)s, welcome to %(location)s", {name:'ABC', location:'Mumbai'});
  71. Lists
  72. -----
  73. .. function:: in_list(list, item)
  74. Returns true if `item` is in `list`
  75. .. function:: inList(list, item)
  76. Returns true if `item` is in `list`. Same as `in_list`
  77. .. function:: has_common(list1, list2)
  78. Returns true if `list1` and `list2` has common items
  79. .. function add_lists(l1, l2)
  80. Returns `l1` + `l2`
  81. Dictionaries
  82. ------------
  83. .. function:: keys(obj)
  84. Python-like function returns keys of a dictionary
  85. .. function:: values(obj)
  86. Python-like function returns values of a dictionary
  87. .. function:: copy_dict(d)
  88. Makes a copy of the dictionary
  89. .. function docstring(obj)::
  90. Converts a dictionary to string
  91. Datatype Conversion
  92. -------------------
  93. .. function:: cint(v, def)
  94. Convert a value to integer, if NaN, then return `def`
  95. .. function:: cstr(s)
  96. Convet to string
  97. .. function:: flt(v,decimals)
  98. Convert to float, with `decimal` places
  99. .. function:: fmt_money(v)
  100. Convert number to string with commas for thousands, millions etc and 2 decimals. Example::
  101. fmt_money(2324); // equals '2,324.00'
  102. .. function:: is_null(v)
  103. Returns true if value is null or empty string.
  104. Returns false is value is 0
  105. .. function:: d2h(d)
  106. Convert decimal to hex
  107. .. function:: h2d(h)
  108. Convert hex to decimal
  109. DOM Manipulation
  110. ----------------
  111. .. function:: $i(id)
  112. Shortcut for document.getElementById(id). Returns the element of the given ID
  113. .. function:: $a(parent, newtag, className, style)
  114. Add element to the given `parent`. Example::
  115. div = $a(body, 'div', '', {padding: '2px'});
  116. .. function:: $a_input(parent, in_type, attributes, style)
  117. Add and INPUT element to the given parent, with given attributes (Fix for IE6 since it does not accept
  118. `type`). Example::
  119. chk = $a_input(body, 'checkbox', null, {border: '0px'});
  120. Style
  121. -----
  122. .. function:: $y(ele, s)
  123. Set Element style. Example::
  124. $y(div,{height:'4px'});
  125. .. function:: $dh(d)
  126. Hide element, set `display` = 'none'
  127. .. function:: $ds(d)
  128. set `display` = 'block' (Show element)
  129. .. function:: $di(d)
  130. set `display` = 'inline' (Show element)
  131. .. function:: $op(e,w)
  132. Same as :func:`set_opacity`
  133. .. function:: set_style(txt)
  134. Declare css classes in txt. Example::
  135. set_style('div.myclass { width: 400px }');
  136. .. function:: set_opacity(ele, opacity)
  137. Set the opacity property of the element
  138. opacity between 0 and 100
  139. Same As: $op(e,w)
  140. .. function:: animate(ele, style_key, from, to, callback)
  141. Animate transition of style property
  142. .. function:: get_darker_shade(col, factor)
  143. Get a darker shade of the given colour, `col` in HEX, `factor` between 0 and 1
  144. Tables
  145. ------
  146. .. function:: make_table(parent, nr, nc, table_width, widths, cell_style)
  147. Make a new table in parent with
  148. * rows `nr`
  149. * columns `nc`
  150. * with columns with widths `widths`
  151. * cell with default style `cell_style`
  152. Example::
  153. var t = make_table(div, 5, 2, '400px', ['100px', '300px'], {padding: '2px'})
  154. .. function:: append_row(t)
  155. Append a new row to the table with same number of columns as the first row
  156. .. function:: $td(t,r,c)
  157. Returns table cell. Shortcut for t.rows[r].cells[c]
  158. .. function:: $sum(t, cidx) {
  159. Returns sum of values in a table column
  160. .. function:: $yt(tab, r, c, s)
  161. Set style on tables with wildcards, Examples::
  162. // hilight row 3
  163. $yt(t, 3, '*', {backgroundColor:'#EEE'})
  164. // border to all cells
  165. $yt(t, '*', '*', {border:'1px solid #000'})
  166. Select Element
  167. --------------
  168. .. function:: empty_select(s)
  169. Empty all OPTIONs of the SELECT (or SelectWidget) element
  170. .. function:: sel_val(sel)
  171. Returns the selected value of the given SELECT (or SelectWidget) element
  172. .. function:: add_sel_options(s, list, sel_val, o_style)
  173. Adds options to the SELECT (or SelectWidget) element s, where sel_val is the default selected value
  174. Positioning
  175. -----------
  176. .. function:: objpos(obj)
  177. Returns {x: `x-cord`, y:`y-cord`}
  178. co-ordinates of the given object (for absolute positioning)
  179. .. function:: get_screen_dims() {
  180. Returns {w: `width`, h:`height`} of the screen
  181. URL
  182. ---
  183. .. function:: get_url_arg(name)
  184. Return the value of the argument `name` from the URL
  185. User Image
  186. ----------
  187. .. function:: set_user_img(img, username)
  188. Sets the user image or No Image tag to the given `img`
  189. Miscelleanous
  190. -------------
  191. .. function:: $s(ele, v, ftype, fopt)
  192. Add the value to the Element `ele` based on `fieldtype` and `fieldoptions`
  193. * Covnerts `Link` type to hyperlink
  194. * Converts `Date` in user format
  195. * Converts `Check` to image with check
  196. .. function:: validate_email(id)
  197. Returns true for a valid email
  198. .. function ie_refresh(e)::
  199. Hide element `e`, show element `e`
  200. .. function:: DocLink(p, doctype, name, onload)
  201. Creates a hyperlink to load the record (`doctype`, `name`)
  202. .. function:: doc_link(p, doctype, name, onload)
  203. Creates a hyperlink to load the record (`doctype`, `name`). Same as DocLink