project_property_decimal_view.js 6.8 KB

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