project_property_decimal_view.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Created by Zhong on 2017/11/13.
  3. */
  4. //default setting
  5. let defaultDecimal = {
  6. min: 0,
  7. max: 6,
  8. _def: {//editable: 开放给用户编辑的
  9. bills: {editable: true, data: {unitPrice: 2, totalPrice: 2}},
  10. ration: {editable: true, data: {quantity: 3, unitPrice: 2, totalPrice: 2}},
  11. glj: {editable: true, data: {quantity: 3, unitPrice: 2}},
  12. feeRate: {editable: true, data: 2},
  13. quantity_detail: {editable: false, data: 4},
  14. process: {editable: false, data: 6}
  15. }
  16. };
  17. let decimalObj = Object.create(null);
  18. decimalObj.decimal = function (field, node) {
  19. if(isDef(field)){
  20. if(field === 'feeRate'){
  21. return this[field];
  22. }
  23. else if (field.sameText('unitFee')) field = 'unitPrice'
  24. else if (field.sameText('totalFee')) field = 'totalPrice';
  25. if(isDef(node)){
  26. if(node.sourceType === projectObj.project.Bills.getSourceType()){
  27. if(field === 'quantity'){
  28. return billsQuanDecimal.decimal(node.data.unit);
  29. }
  30. else {
  31. return returnV(this['bills'][field], this.process);
  32. }
  33. }
  34. else if(node.sourceType === projectObj.project.Ration.getSourceType()){
  35. return returnV(this['ration'][field], this.process);
  36. }
  37. else if(node.sourceType === projectObj.project.GLJ.getSourceType()){
  38. return returnV(this['glj'][field], this.process);
  39. }
  40. }
  41. }
  42. return this.process;
  43. };
  44. function returnV(v, r){
  45. if(isDef(v)){
  46. console.log(v);
  47. return v;
  48. }
  49. return r;
  50. }
  51. function isUndef(v) {
  52. return v === undefined || v === null;
  53. }
  54. function isDef(v){
  55. return v !== undefined && v !== null;
  56. }
  57. function isObj(v){
  58. return isDef(v) && typeof v === 'object';
  59. }
  60. function isNum(v){
  61. return isDef(v) && !isNaN(v);
  62. }
  63. function isInt(v){
  64. return isNum(v) && v % 1 === 0;
  65. }
  66. function isValidDigit(v){
  67. return isInt(v) && v >= defaultDecimal.min && v <= defaultDecimal.max;
  68. }
  69. //newV用户可编辑数据
  70. function toUpdateDecimal(orgV, newV){
  71. let rst = false;
  72. let len = Object.keys(newV).length;
  73. reCompare(orgV, newV);
  74. function reCompare(orgV, newV){
  75. for(let attr in newV){
  76. if(Object.keys(newV[attr]).length > 0){
  77. if(isUndef(orgV[attr])){
  78. rst = true;
  79. return;
  80. }
  81. else{
  82. reCompare(orgV[attr], newV[attr]);
  83. }
  84. }
  85. else {
  86. if(isUndef(orgV[attr]) || parseInt(newV[attr]) !== parseInt(orgV[attr])){
  87. rst = true;
  88. return;
  89. }
  90. }
  91. }
  92. }
  93. return rst;
  94. }
  95. function setDecimal(_digits, data){
  96. if(isDef(data)){
  97. for(let attr in data){
  98. _digits[attr] = data[attr] || defaultDecimal['_def'][attr]['data'];
  99. }
  100. }
  101. else {
  102. for(let attr in defaultDecimal['_def']){
  103. _digits[attr] = defaultDecimal['_def'][attr]['data'];
  104. }
  105. }
  106. }
  107. //获取decimalPanel中要展示的数据
  108. function m_getInitData(data){
  109. let rst = Object.create(null);
  110. for(let attr in defaultDecimal['_def']){
  111. if(defaultDecimal['_def'][attr]['editable'] && isDef(data[attr])){
  112. rst[attr] = data[attr];
  113. }
  114. }
  115. return rst;
  116. }
  117. //获取小数位数panel里的数据
  118. function m_getDecimalData(inputs){
  119. let rst = Object.create(null);
  120. for(let i = 0, len = inputs.length ; i < len; i++){
  121. let name = $(inputs[i]).attr('name');
  122. let attrs = name.split('-');
  123. if(attrs.length > 1){
  124. if(isUndef(rst[attrs[0]])){
  125. rst[attrs[0]] = Object.create(null);
  126. }
  127. if(isDef(rst[attrs[0]])) {
  128. rst[attrs[0]][attrs[1]] = parseInt($(inputs[i]).val());
  129. }
  130. }
  131. else {
  132. rst[attrs[0]] = parseInt($(inputs[i]).val());
  133. }
  134. }
  135. for(let attr in defaultDecimal['_def']){
  136. if(!defaultDecimal['_def'][attr]['editable']){
  137. rst[attr] = defaultDecimal['_def'][attr]['data'];
  138. }
  139. }
  140. return rst;
  141. }
  142. function v_initPanel(data){
  143. if(this.isDef(data)){
  144. for(let attr in data){
  145. if(this.isObj(data[attr])){
  146. for(let subAttr in data[attr]){
  147. let str = attr + '-' + subAttr;
  148. let jqs = 'input[name="' + str + '"]';
  149. $(jqs).val(data[attr][subAttr]);
  150. $(jqs).attr('min', defaultDecimal.min);
  151. $(jqs).attr('max', defaultDecimal.max);
  152. }
  153. }
  154. else {
  155. let str = attr + '';
  156. let jqs = 'input[name="' + str + '"]';
  157. $(jqs).val(data[attr]);
  158. $(jqs).attr('min', defaultDecimal.min);
  159. $(jqs).attr('max', defaultDecimal.max);
  160. }
  161. }
  162. }
  163. }
  164. function e_validIn(inputs){
  165. for(let i = 0, len = inputs.length; i < len; i++){
  166. let orgV = $(inputs[i]).val();
  167. $(inputs[i]).keydown(function () {
  168. let v = $(this).val();
  169. if(v.trim().length > 0 && isValidDigit(v)){
  170. orgV = v;
  171. }
  172. });
  173. $(inputs[i]).keyup(function () {
  174. let v = $(this).val();
  175. if(v.trim().length === 0 || !isValidDigit(v)){
  176. alert('小数位数范围在0-6!');
  177. $(this).val(orgV);
  178. }
  179. else{
  180. //let newV = parseInt(v);
  181. }
  182. });
  183. }
  184. }
  185. function e_bindCof(btn){
  186. btn.bind('click', function () {
  187. //获取更新的数据
  188. let updateDecimal = m_getDecimalData($('input', '#poj-settings-decimal'));
  189. if(toUpdateDecimal(decimalObj, updateDecimal)){
  190. a_updateDigits(updateDecimal);
  191. }
  192. });
  193. }
  194. function e_unbindCof(btn){
  195. btn.unbind();
  196. }
  197. function a_updateDigits(updateDecimal){
  198. let url = '/pm/api/updateProjects';
  199. let data = {
  200. updateType: 'update',
  201. updateData: {
  202. ID: parseInt(scUrlUtil.GetQueryString('project')),
  203. 'property.decimal': updateDecimal
  204. }
  205. };
  206. let postData = {
  207. user_id: userID,
  208. updateData: [data]
  209. };
  210. let scCaller = function (resultData) {
  211. //update data
  212. setDecimal(decimalObj, updateDecimal);
  213. let v_data = m_getInitData(decimalObj);
  214. v_initPanel(v_data);
  215. };
  216. let errCaller = function () {
  217. alert('更新小数位数失败!');
  218. let v_data = m_getInitData(decimalObj);
  219. v_initPanel(v_data);
  220. };
  221. CommonAjax.post(url, postData, scCaller, errCaller);
  222. }
  223. $(document).ready(function () {
  224. $('#poj-set').on('shown.bs.modal', function (e) {
  225. let v_data = m_getInitData(decimalObj);
  226. v_initPanel(v_data);
  227. });
  228. //绑定确定按钮
  229. e_bindCof($('#property_ok'));
  230. //绑定小数位数输入控制
  231. e_validIn($('input', '#poj-settings-decimal'));
  232. });