You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

164 lines
5.3 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // add a new dom element
  23. wn.provide('wn.dom');
  24. wn.dom = {
  25. id_count: 0,
  26. by_id: function(id) {
  27. return document.getElementById(id);
  28. },
  29. set_unique_id: function(ele) {
  30. var id = 'unique-' + wn.dom.id_count;
  31. if(ele)
  32. ele.setAttribute('id', id);
  33. wn.dom.id_count++;
  34. return id;
  35. },
  36. eval: function(txt) {
  37. if(!txt) return;
  38. var el = document.createElement('script');
  39. el.appendChild(document.createTextNode(txt));
  40. // execute the script globally
  41. document.getElementsByTagName('head')[0].appendChild(el);
  42. },
  43. set_style: function(txt) {
  44. if(!txt) return;
  45. var se = document.createElement('style');
  46. se.type = "text/css";
  47. if (se.styleSheet) {
  48. se.styleSheet.cssText = txt;
  49. } else {
  50. se.appendChild(document.createTextNode(txt));
  51. }
  52. document.getElementsByTagName('head')[0].appendChild(se);
  53. },
  54. add: function(parent, newtag, className, cs, innerHTML, onclick) {
  55. if(parent && parent.substr)parent = wn.dom.by_id(parent);
  56. var c = document.createElement(newtag);
  57. if(parent)
  58. parent.appendChild(c);
  59. // if image, 3rd parameter is source
  60. if(className) {
  61. if(newtag.toLowerCase()=='img')
  62. c.src = className
  63. else
  64. c.className = className;
  65. }
  66. if(cs) wn.dom.css(c,cs);
  67. if(innerHTML) c.innerHTML = innerHTML;
  68. if(onclick) c.onclick = onclick;
  69. return c;
  70. },
  71. css: function(ele, s) {
  72. if(ele && s) {
  73. for(var i in s) ele.style[i]=s[i];
  74. };
  75. return ele;
  76. },
  77. placeholder: function(dim, letter) {
  78. function getsinglecol() {
  79. return Math.min(Math.round(Math.random() * 9) * Math.round(Math.random() * 1) + 3, 9)
  80. }
  81. function getcol() {
  82. return '' + getsinglecol() + getsinglecol() + getsinglecol();
  83. }
  84. args = {
  85. width: Math.round(flt(dim) * 0.7) + 'px',
  86. height: Math.round(flt(dim) * 0.7) + 'px',
  87. padding: Math.round(flt(dim) * 0.15) + 'px',
  88. 'font-size': Math.round(flt(dim) * 0.6) + 'px',
  89. col1: getcol(),
  90. col2: getcol(),
  91. letter: letter.substr(0,1).toUpperCase()
  92. }
  93. return repl('<div style="\
  94. height: %(height)s; \
  95. width: %(width)s; \
  96. font-size: %(font-size)s; \
  97. color: #fff; \
  98. text-align: center; \
  99. padding: %(padding)s; \
  100. background: -moz-linear-gradient(top, #%(col1)s 0%, #%(col2)s 99%); /* FF3.6+ */\
  101. background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#%(col1)s), color-stop(99%,#%(col2)s)); /* Chrome,Safari4+ */\
  102. background: -webkit-linear-gradient(top, #%(col1)s 0%,#%(col2)s 99%); /* Chrome10+,Safari5.1+ */\
  103. background: -o-linear-gradient(top, #%(col1)s 0%,#%(col2)s 99%); /* Opera 11.10+ */\
  104. background: -ms-linear-gradient(top, #%(col1)s 0%,#%(col2)s 99%); /* IE10+ */\
  105. background: linear-gradient(top, #%(col1)s 0%,#%(col2)s 99%); /* W3C */\
  106. filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=\'#%(col1)s\', endColorstr=\'#%(col2)s\',GradientType=0 ); /* IE6-9 */\
  107. ">%(letter)s</div>', args);
  108. }
  109. }
  110. wn.get_cookie = function(c) {
  111. var clist = (document.cookie+'').split(';');
  112. var cookies = {};
  113. for(var i=0;i<clist.length;i++) {
  114. var tmp = clist[i].split('=');
  115. cookies[strip(tmp[0])] = strip(tmp[1]);
  116. }
  117. return cookies[c];
  118. }
  119. wn.dom.set_box_shadow = function(ele, spread) {
  120. $(ele).css('-moz-box-shadow', '0px 0px '+ spread +'px rgba(0,0,0,0.3);')
  121. $(ele).css('-webkit-box-shadow', '0px 0px '+ spread +'px rgba(0,0,0,0.3);')
  122. $(ele).css('-box-shadow', '0px 0px '+ spread +'px rgba(0,0,0,0.3);')
  123. };
  124. // add <option> list to <select>
  125. (function($) {
  126. $.fn.add_options = function(options_list) {
  127. // create options
  128. for(var i=0; i<options_list.length; i++) {
  129. var v = options_list[i];
  130. value = v.value || v;
  131. label = v.label || v;
  132. $('<option>').html(label).attr('value', value).appendTo(this);
  133. }
  134. // select the first option
  135. this.selectedIndex = 0;
  136. return $(this);
  137. }
  138. $.fn.set_working = function() {
  139. var ele = this.get(0);
  140. $(ele).attr('disabled', 'disabled');
  141. if(ele.loading_img) {
  142. $(ele.loading_img).toggle(true);
  143. } else {
  144. ele.loading_img = $('<img src="images/lib/ui/button-load.gif" \
  145. style="margin-left: 4px; margin-bottom: -2px; display: inline;" />')
  146. .insertAfter(ele);
  147. }
  148. }
  149. $.fn.done_working = function() {
  150. var ele = this.get(0);
  151. $(ele).attr('disabled', null);
  152. if(ele.loading_img) {
  153. $(ele.loading_img).toggle(false);
  154. };
  155. }
  156. })(jQuery);